I'm making a form in Visual Studio 2019 with C# and set the font of a label with a font dialog:
FontDialog fontDlg = new FontDialog();
if (fontDlg.ShowDialog() != DialogResult.Cancel)
{
Label.Font = fontDlg.Font;
Label.Text = fontDlg.Font.Name;
}
I set Sans Serif 10pt. I then read back the size with
string size = Label.Font.Size
and I get "9.75" instead of "10".
Why would this be?
Related
So I'm trying to use to a menu strip to only change the font family or the font size. But as you can see, I have to put 12 in the font size to make it work. How can I change the font family or the font size by itself while keeping the current font size or font-family
private void arialToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
{
ActiveMdiChild.Controls["richTextBox1"].Font = new Font("Arial", 12);
}
}
As you can see in the documentation you cannot directly set the Font.Size property so your best bet is probably this approach:
var currentFont = ActiveMdiChild.Controls["richTextBox1"].Font;
currentFont = new Font(currentFont.FontFamily, 12);
I have a small app that get a list of all the windows fonts. I can then choose one and display all its characters. All worked till I then found I had a problem, not all fonts have FontStyle:Regular. No problem I could do a check but this is where I run into a problem, when I start the system seems to have FontStyle:Regular as its default and I cannot change it so if I run the code below with text "Aharoni" selected in my combobox it falls over telling me that regular is not supported. How can I make it ignore the style or force the style to be one it uses like bold?
var cvt = new FontConverter();
Font fname = cvt.ConvertFromString(cmbobx_fontname.Text) as Font;
If I cannot do this then is it possible to get the styles that the choosen font will support?
The FontFamily class has a method to check if a style is available for the font - so rather than try to create a Font directly, first create a FontFamily instance & interate through the styles till you find one that is supported e.g.
FontFamily fontf = new FontFamily("Aharoni");
System.Drawing.FontStyle fs = System.Drawing.FontStyle.Regular;
System.Drawing.Font font;
if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Regular))
fs = System.Drawing.FontStyle.Regular;
else if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Bold))
fs = System.Drawing.FontStyle.Bold;
else if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Italic))
fs = System.Drawing.FontStyle.Italic;
else if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Strikeout))
fs = System.Drawing.FontStyle.Strikeout;
else if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Underline))
fs = System.Drawing.FontStyle.Underline;
else
throw new Exception("No Font Styles Available");
font = new System.Drawing.Font(fontf, 10, fs);
You could iterate through the FontStyle enum - rather than use if/else as I have done.
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.
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"
});
Can anyone tell me how can set default Font Name , Font Size , Font Color.. of FontDialog;
FontDialog dlg = new FontDialog();
dlg.ShowColor = true;
if (dlg.ShowDialog() != DialogResult.OK) return;
The dlg.ShowDialog() ; method should show Font name that I choose insted of "microsoft san serif"
You just need to set the Font property before calling ShowDialog.
For example:
dlg.Font = new Font("Consolas", 10);
//or
dlg.Font = myCurrentlySelectedFont;
It's also worth pointing out that when getting the font name from the font dialog, you want the value: fontDlg.Font.Name, or fontDlg.Font.FontFamily.Name.
This value will correctly allow you to set the font name as above before showing the dialogue.