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.
Related
C# -> Windows Forms
In the past I've set as a descriptor:
Color gcolorBackDialogBox;
Color gcolorForeDialogBox;
Under dlg_Load() I've defined them as:
gcolorBackDialogBox = this.BackColor;
gcolorForeDialogBox = this.ForeColor;
Under a function I've used them as:
gLBlStatus.BackColor = gcolorBackDialogBox;
gLBlStatus.ForeColor = gcolorForeDialogBox;
where gLBlStatus is the name of a label on a dialog box. This sets the label to the same color as the dialog box.
I'm converting to C# -> WPF. I can change the labels:
gLBlStatus.Background = Brushes.LightGray;
gLBlStatus.Foreground = Brushes.Black;
but can't find a way to replicate the global aspect.
I've found that the default dialog box color is SystemColors.ControlBrushKey ... am unable to find a way to make this nomenclature work.
Can't you create a SolidColorBrush global instance,
SolidColorBrush gcolorBackDialogBox = new SolidColorBrush(color);
SolidColorBrush gcolorForeDialogBox = new SolidColorBrush(color);
There are other classes that inherit from Brush for different styles of color.
I have a TrueType font, which is "Semibold". I try to use that in the following method:
private FontFamily GetFontFamily(string name)
{
PrivateFontCollection pfc = new PrivateFontCollection();
var path = Server.MapPath("~/Static/webfont/" + name + ".ttf");
pfc.AddFontFile(path);
return pfc.Families[0];
}
private Font GetFont(string name, int size,FontStyle style)
{
return new Font(GetFontFamily(name), size, style);
}
Where I provide a name of my font, and it finds the Sentinel-SemiboldItalic.ttf font. As a FontStyle, I have tried to provide any of the options in the .NET framework (regulary, bold, italic, underline and strikeout).
No matter what, I get the following error:
Font 'Sentinel Semibold' does not support style 'Regular'.
What should I do? How to use a semibold font as a font in C#? Also, can I somehow convert my TrueType font to a regular one (if that would fix the issue) ?
As you can read in the answer to this question, each font has its own properties (for example: enabling a Regular Style or not), as provided by the font creator.
By looking at your font (even just by looking at its name), it seems clear that it is a sub-type whose defining characteristics are precisely being (semi-)bold and italic; consequently, it does sound logical to not have the option to remove these features. If you want a non-bold, non-italic version, you should rely on the parent family (Sentinel).
I too faced the same issue like you. And found that even though we are adding the font[.ttf] file, the incorrect font is getting added in privatefontcollection. Hence i found the workaround for this case. This issue will be thrown randomly. So add the font till correct font is added in private font collection.
private FontFamily GetFontFamily(string name)
{
PrivateFontCollection pfc = new PrivateFontCollection();
var path = Server.MapPath("~/Static/webfont/" + name + ".ttf");
pfc.AddFontFile(path);
**while (pfc.FontFamilies != null && pfc.Families.Length > 0 && (pfc.FontFamilies[0] as FontFamily).Name != "YourFontName")
{
pfc.Dispose();
pfc = new PrivateFontCollection();
GetFontFamily();
}
return pfc.Families[0];
}
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"
});
I feel like a real noob posting this, but I can't seem to find anything for this...
I have a control that I'm basically trying to toggle the fontstyle between bold and not bold. This should be simple...
However, you can't acccess the Control.Font.Bold property as it is read only, therefore, you need to change the Font property.
To make it bold, I just do this:
this.btn_buttonBolding.Font = new Font(this.btn_buttonBolding.Font, FontStyle.Bold);
Not ideal, but it works. However, how do I go about removing this bold style (once it is bold already)?
I looked hard for duplicates; closest I could find was this, but it doesn't quite answer my situation:
Substract Flag From FontStyle (Toggling FontStyles) [C#]
And this which gives how to set it, but not remove it: Change a font programmatically
Am I missing a simple constructor for the font that could do this? Or am I just missing something easier?
I know this is a bit old, but I was faced with the exact same problem and came up with this:
Font opFont = this.btn_buttonBolding.Font;
if(value)
{
this.btn_buttonBolding.Font = new Font(opFont, opFont.Style | FontStyle.Bold);
}
else
{
this.btn_buttonBolding.Font = new Font(opFont, opFont.Style & ~FontStyle.Bold);
}
The magic is in the "~" which is the bitwise NOT. (See the MSDN KB article "~Operator")
VB.NET version:
Dim opFont As Font = me.btn_buttonBolding.Font
If (value)
me.btn_buttonBolding.Font = new Font(opFont, opFont.Style Or FontStyle.Bold)
Else
me.btn_buttonBolding.Font = new Font(opFont, opFont.Style And Not FontStyle.Bold)
End if
The FontStyle enum contains 5 distinct values.
The one that reset your previous set is FontStyle.Regular
Regular Normal text.
Bold Bold text.
Italic Italic text.
Underline Underlined text.
Strikeout Text with a line through the middle.
It's a bitwise enum where Regular is 0. So setting this value alone reset all other flags
Try this:
private void btn_buttonBolding_Click(object sender, EventArgs e)
{
var style = btn_buttonBolding.Font.Bold ? FontStyle.Regular : FontStyle.Bold;
btn_buttonBolding.Font = new Font(this.Font, style);
}
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.