Change default font dialog in c# - c#

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.

Related

Font size being reported as fractionally off set value

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?

How can I make default fontstyle be one that the font likes?

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.

Datagridview title font in windows forms in c#

How can I change the font of title of a Data grid view in Windows Form in C#? Or how can I change column names font in a dataGridView in Windows Form in c#?
Any help would be appreciated.
Let's assume You have some columns and You want to change the header of first column
MyDataGridView.Columns[0].HeaderText = "My title";
To change the font in header check this:
// ("Arial", 20") means it will use Arial font with 20em size.
dgv.ColumnHeadersDefaultCellStyle.Font = new Font("Arial", 20);
How to: Set Font and Color Styles in the Windows Forms DataGridView Control
there is HeaderText property in Column object,
public Form1()
{
InitializeComponent();
grid.DefaultCellStyle.Font = new Font("Tahoma", 15);
grid.Columns[0].HeaderText = "First Column";
//..............
}
DataGridViewColumn.Name Property

How to set Textbox.Font.Size in C#?

I load font.size in a file that this format is string and i want to set textbox.font.size by
this value but say "this value is readonly not set"
how i can set font.size in coding?
By Using this it is possible to programmatically choose the best font. This also allows you to set different sizes on the various alternative fonts.
Font font = new Font("Times New Roman", 16.0f,
FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
textBox1.Font = font;
For more details, check here
You can Set the Font Property of TextBox Control.
Font Property of TextBox Control Expects Font Class Object.
you can create the Font class oject with different styles by passing different parameters to its constructors.
Font Class Constructor Description :
FontFamily - FontFamily (EnumType) : used to specify Font name
ex:Arial,Times New Roman etc.,
FontSize - float(DataType) : it's a float value of font size.
FontStyle - FontStyle (EnumType) : it is a FontStyle of different
types ex: FontStyle.Regular,FontStyle.Bold,FontStyle.Italic etc.,
Now See sample Example:
Font fnt=new Font(textBox1.Font.FontFamily,12.0F);//Edit your size asper your requirement. it's float value
textBox1.Font = fnt;
Create new font from current font (use it as prototype) and provide font size (parse your string to float):
textBox1.Font = new Font(textBox1.Font, Single.Parse(sizeString));
You have to set it at start of the Initialization of textbox
like
var textbox = new TextBox()
{
FontFamily = "Segoe WP",
FontSize = 18
};
Font.Size is read only. You must set the Font object itself.

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