c# Winform richtextbox font discrepancy - c#

The font property of richtextbox doesn't seem to be working.
//
// textBox_rawdata
//
this.textBox_rawdata.DetectUrls = false;
this.textBox_rawdata.Font = new System.Drawing.Font("NSimSun", 9F);
this.textBox_rawdata.HideSelection = false;
this.textBox_rawdata.Location = new System.Drawing.Point(22, 43);
this.textBox_rawdata.Name = "textBox_rawdata";
this.textBox_rawdata.Size = new System.Drawing.Size(368, 68);
this.textBox_rawdata.TabIndex = 2;
this.textBox_rawdata.Text = "AAAAAA";
I want the font of the richtextbox to be NSimSun, 9pt. As you can see in the picture, The first few A's are preset and the last 3 A's are typed in by me. The issues is, the preset characters and any characters generated by the program are correctly displayed as NSimSun, 9pt. But as soon as I start typing in there, the font changes. (Like the last 3 A's)
How can I make the font NSimSun, 9pt for all text?

This might work for you.
this.textBox_rawdata.SelectionFont = new System.Drawing.Font("Tahoma", 12, System.Drawing.FontStyle.Bold)
if you want your font type, size and style to be set once you run your code put this in designer:
this.textBox_rawdata.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

Try to set SelectionFont property of richtextbox to System.Drawing.Font("NSimSun", 9F) also.
From MSDN it is:
A Font that represents the font to apply to the current text selection or to text entered after the insertion point.

Related

Unable to change backcolor property in Winforms app (C#)

I am working on a project where I want to have different select-able themes that change the color of certain UI elements. I am trying to change the BackColor property of different controls (buttons, labels, panels, etc), but I an unable to actually change the BackColor. My buttons are on FlatUI, don't have a background image, and UseVisualBackgroundStyle is false. I don't get any errors when I change the BackColor but I can't see it actually changing anything.
My code for creating the one of the buttons:
this.scriptHubButton.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(50)))), ((int)(((byte)(50)))), ((int)(((byte)(50)))));
this.scriptHubButton.FlatAppearance.BorderSize = 0;
this.scriptHubButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.scriptHubButton.Font = new System.Drawing.Font("Showcard Gothic", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.scriptHubButton.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.scriptHubButton.Location = new System.Drawing.Point(761, 416);
this.scriptHubButton.Name = "scriptHubButton";
this.scriptHubButton.Size = new System.Drawing.Size(85, 36);
this.scriptHubButton.TabIndex = 11;
this.scriptHubButton.Text = "More";
this.scriptHubButton.UseVisualStyleBackColor = false;
this.scriptHubButton.Click += new System.EventHandler(this.scriptHubButton_Click);
And here's the idea of how I am attempting to change the backcolor:
public void changeTheme(Color c)
{
scriptHubButton.BackColor = c;
}
Edit: I am trying to change this is the main form of my app while it is already being displayed...

Horizontal scrollbar not showing on my textbox

On a Winform C# application, i display a textbox on my form.
This textbox will display one line, just one.
I would like to show and be abe to use an horizontal scrollbar.
I set the property "scrollbar" to horizontal : ScrollBar doesn't show.
I add WordWrap to false : ScrollBar doesn't show.
I add MultiLine to true ( even if one ligne ) : ScrollBar doesn't show.
My line displayed is a loter "longer" than the contrôle, so i really need a scrollbar :(
Here is the definition :
this.TxtBox_ApercuFichier.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.TxtBox_ApercuFichier.Location = new System.Drawing.Point(11, 30);
this.TxtBox_ApercuFichier.Multiline = true;
this.TxtBox_ApercuFichier.Name = "TxtBox_ApercuFichier";
this.TxtBox_ApercuFichier.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
this.TxtBox_ApercuFichier.Size = new System.Drawing.Size(702, 21);
this.TxtBox_ApercuFichier.TabIndex = 12;
Even with wordwrap at false, it's the same result.
( My textbox is in a groupbox).
Any idea please ?
Thanks a lot :)
Regards,
You need to do the following to get a horizontal scroll bar to display in a windows forms text box:
this.TxtBox_ApercuFichier.Multiline = true;
this.TxtBox_ApercuFichier.WordWrap = false;
this.TxtBox_ApercuFichier.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
You can then resize the text box to give the appearance of one line. You need to have Multiline enabled otherwise the height of the text box will be set to the text height (I can't seem to find an easy way to override this), hence you not being able to see the scroll bar.
The following code will set ScrollBar not visible and also the parent panel where its contained.
HScrollBar hScroller = textBox.HScrollBar;
hScroller.Visible = false;
hScroller.Parent.Visible=false;

Render CheckBox to Image for Tile

I want to display one or more CheckBoxes on a tile in my Windows Phone app. This works already for TextBlocks, but with a CheckBox it shows only the Text of the CheckBox and not the Checkmark itself.
This is a sample of my code:
public void CreateTile()
{
StackPanel panel = new StackPanel();
panel.VerticalAlignment = VerticalAlignment.Top;
panel.Margin = new Thickness(7.0, 7.0, 7.0, 0);
panel.Width = 336;
panel.Height = 336;
panel.Orientation = Orientation.Vertical;
// Create and add a CheckBox for each task
foreach (var task in _tasks)
{
TextBlock textBlock = new TextBlock();
textBlock.TextWrapping = TextWrapping.Wrap;
textBlock.Style = App.Current.Resources["PhoneTextLargeStyle"] as Style;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.Text = task.Text;
CheckBox checkBox = new CheckBox();
checkBox.IsChecked = task.IsDone;
checkBox.Content = textBlock;
panel.Children.Add(checkBox);
}
Grid layoutRoot = new Grid();
layoutRoot.Background = new SolidColorBrush(Colors.Blue);
layoutRoot.Width = 336;
layoutRoot.Height = 336;
layoutRoot.Children.Add(panel);
layoutRoot.Measure(new Size(336, 336));
layoutRoot.Arrange(new Rect(0, 0, 336, 336));
layoutRoot.UpdateLayout();
// Render grid into bitmap
WriteableBitmap bitmap = new WriteableBitmap(336, 336);
bitmap.Render(layoutRoot, null);
bitmap.Invalidate();
// Save background image for tile to isolated storage
Uri backgroundImage = TileHelper.SaveTileImage(bitmap);
}
If I create a tile with a background image created by the method above, the tile will look like this:
As you can see the text is displayed but there is no checkmark/square before the text.
I personally like to use Segoe UI Symbol as the Font Family in such situations. This gives me the flexibility to use Text and Symbols together while not messing around too much with code / images. SUS has great modern icons (or characters if you may call them) that are very much Metroish, I'd say.
Just open up Charmap (Win + R and type in charmap) and in the Font Select -> Segoe UI Symbol. Now you can select any character you like and paste into Visual Studio Editor itself. Yes, it works!
The symbol may not display properly in the Editor itself but it will at Runtime
Here are some suggestions:
Here are the corresponding characters:
☑

✅

Don't worry about them not looking right HERE. They should when you follow the above steps.
You can always "hack" it by using images of checkbox controls. Did you try to show created control in UI? i.e. adding it to page? Just to see if your code is executed correctly.
Or another solution would be to use check mark character - http://www.fileformat.info/info/unicode/char/2713/index.htm
I'll try to replicate this problem in my test app since it is strange that it does not work.

Get value from richtextbox and assign them to radio button in c#?

I'm working on program that will take the input from richtextboxes in a loop and then I want to assign the text that I get from user to radiobutton that will be in a groupbox. I successfully developed the form with richtextboxes in a loop but now I stuck how I will assign them to radio button in a loop? When I click button?? Please guide me in this regard?
a piece of code here
Label label1 = new Label();
label1.Size = new System.Drawing.Size(96, 24);
label1.Text = "Question" + _openCount++;
label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Size = new System.Drawing.Size(600, 91);
richTextBox1.Text = "";
Label label2 = new Label();
label2.Size = new System.Drawing.Size(96, 24);
label2.Text = "Option1";
......................
now i want to put my code in this button how i further start my code???
void button1(object sender, EventArgs e)
{
}
Create a list of your radio buttons, then create a list of your rich textbox strings.
Enumerable.Zip these two lists together then foreach through that list.
foreach(var item in zippedList)
{
item.RadioButton.Text = item.RichTextBoxText.Text;
}
The syntax for this would be way off, but I hope you get the idea. This is also assuming that you have an equal number of radio buttons and lines of text. You'll need to take that into consideration and will have to modify the two lists before zipping them if they are different lengths.

Programmatically adding Label to Windows Form (Length of label?)

In my code, i create a label with the following:
Label namelabel = new Label();
namelabel.Location = new Point(13, 13);
namelabel.Text = name;
this.Controls.Add(namelabel);
The string called name is defined before this, and has a length of around 50 characters. However, only the first 15 are displayed in the label on my form. I tried messing with the MaximumSize of the label but to no avail.
Try adding the AutoSize property:
namelabel.AutoSize = true;
When you place a label on a form with the design editor, this property defaults to true, but if you create the label in code like you did, the default is false.
Try the property AutoSize = true;
MSDN refs
Another way is using the MeasureString method of the Graphics class
Graphics e = nameLabel.CreateGraphics();
SizeF stringSize = new SizeF();
stringSize = e.MeasureString(name, namelabel.Font);
nameLabel.Width = (int)stringSize.Width;
You could use the property Label.AutoSize to automatically adjust the width of your label to properly fit all the contents stored in Label.Text.
It's worth mentioning that when creating the label using the design editor this property defaults to true, but when you programmatically creates a label on your own the property defaults to false.
namelabel.AutoSize = true;
Of course you could also manually set the width of your label using something as the below to calculate the required width.
Graphics namelabel_g = namelabel.CreateGraphics ();
namelabel.Width = namelabel_g.MeasureString (
namelabel.Text, namelabel.Font
);
Documentation regarding the use of Label.AutoSize use can be found on msdn:
msdn.microsoft.com - Label.AutoSize Property (System.Windows.Forms)
Documentation regarding Graphics.MeasureString can be found here:
msdn.microsoft.com - Graphics.MeasureString Method (String, Font) (System.Drawing)
panel_saved.Controls.Add(
new Label
{
Location = new Point(1, 2),
Size = new System.Drawing.Size(43, 18),
BorderStyle = BorderStyle.FixedSingle,
Text = "yourdata"
});

Categories

Resources