How to change the font style of text - c#

How can I change the Font Style (regular, bold, etc) ?
So far what I have is this:
if (listBox1.SelectedIndex == 3)
{
if (textBox1.Text == "regular")
{
//FontStyle regular = new FontStyle !!!wrong one!!!
}
}
So what i need is that when I'd type "regular" in the textbox, the font style will change to regular. How do I do that?

You need to create a new Font and apply it to your text box:
textBox1.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Normal);
This will create one with a new family etc. You can read the size and family from the existing Font object to preserve those.
textBox1.Font = new Font(textBox1.Font, FontStyle.Normal);
For more information on the properties of the Font class see the MSDN page

You need to set the Control.FontWeight property
if (listBox1.SelectedIndex == 3)
{
if (textBox1.Text == "regular")
{
TextBox1.FontWeight = FontWeights.Regular;
}
}

Are you looking for something like this:
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold);

Related

Emulate System style on custom .NET Combobox

I've derived the ComboBox Class in order to have a custom layout of the list items. This is a DropDown list style Combobox, which has a nice appearance, while the custom one looks like an old-style one.
Is there any way to emulate or force the control to draw the "windows default-style" layout while still having a custom design? This is, having a style similar to the right one on the picture above, with the gradient background (or whatever it takes depending on the version of windows).
This what I've done so far:
public ComboBoxRGB()
{
this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (Items.Count > 0)
{
if (e.Index > -1)
{
if (Items[e.Index] == null) return;
int BarWidth = 16;
int BarSpacing = 1;
int Spacing_Top = 2;
int Spacing_Left = 3;
int Spacing_Right = 4;
ComboBoxRGBItem item;
try
{
item = (ComboBoxRGBItem)Items[e.Index];
}
catch { return; }
e.DrawBackground();
e.DrawFocusRectangle();
//Draw color indicator
System.Drawing.SolidBrush bColor = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(item.Red, item.Green, item.Blue));
e.Graphics.FillRectangle(bColor, Spacing_Left + e.Bounds.Left, e.Bounds.Top + Spacing_Top, 12, 12);
//Draw text
System.Drawing.SolidBrush sbText = (e.State == System.Windows.Forms.DrawItemState.Selected) ? new System.Drawing.SolidBrush(System.Drawing.Color.White) : new System.Drawing.SolidBrush(System.Drawing.Color.Black);
e.Graphics.DrawString(item.Text, e.Font, sbText, Spacing_Left + e.Bounds.Left + BarWidth + Spacing_Right, e.Bounds.Top);
} //IF_Index
} //IF_Items_Count
}
I would be very pleased if anyone has any idea on how to do this. I would like to try to emulate the appearance manually, since this changes from windows version to version.
I was not able to find information about this on internet, so I suppose is not possible to auto-draw the backgound using system-default-style.
For the moment, the solution used is to draw it completely manually, through the override of Paint method, but this requires to take into account what is the version of Windows being executed and use different designs for drawing the control.
In case anyone has a different solution or any suggestion, please, let me know it.

Detect and Highlight Font of text in TextBox in Font Dialog - C# Winform [duplicate]

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.

Bold Text in GroupBox

In the following code-snippet I'm creating a GroupBox, where I would like the title to be bold.
Control containerControl;
containerControl = new GroupBox { Name = "" + viewGroupID, Text = viewName, Dock = DockStyle.Top, Height = 20, Padding = new Padding( 2 ) };
The title is being set by the Text Property, but at the moment I have not been able to make it bold.
In the above picture, it's the "View Group A" that needs to be bold.
I have read, that this can be done by using a label, but is there any possible way to to achieve this just by creating a GroupBox?
You can do that by making a new font with FontStyle parameter added:
GroupBox gb = new GroupBox()
{
Font = new Font( DefaultFont.FontFamily, DefaultFont.Size, FontStyle.Bold ),
Text = "Text"
//You can also use 'this.Font' instead of 'DefaultFont' to use the font of your form
};

Adding multiple external fonts to my winform application

I would like to add or deploy the app with more than 10 external fonts from my resource folder. I have gone through various SO questions but none of them suits my requirement except this SO answer.
Now I have implemeted the same thing in my winform app and I would like to add these fonts to combo box with their styles such as shown below.
Any suggestions are most welcome.
You have to draw the items in the ComboBox yourself if you want to show them in the drop down in their appropriate font. This isn't hard, the following code does the bare minimum:
var comboBox = new ComboBox();
comboBox.DisplayMember = "Name";
comboBox.Items.AddRange(FontFamily.Families);
comboBox.DrawMode = DrawMode.OwnerDrawFixed;
comboBox.DrawItem += (s, e) => {
var fontFamily = (FontFamily) comboBox.Items[e.Index];
var itemText = comboBox.GetItemText(fontFamily);
// Some fonts don't work with a regular style, if they don't have a
// regular style, we'll default to the provided font.
if (!fontFamily.IsStyleAvailable(FontStyle.Regular))
{
TextRenderer.DrawText(
e.Graphics, itemText, e.Font, e.Bounds, e.ForeColor, e.BackColor
TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
}
else
{
using (var font = new Font(fontFamily, comboBox.Font.Size, FontStyle.Regular))
{
TextRenderer.DrawText(
e.Graphics, itemText, font, e.Bounds, e.ForeColor, e.BackColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
}
}
};
This implementation handles a FontFamily not having a regular font style by falling back to the ComboBox's default font. You can handle this more gracefully by attempting to find a valid style for the FontFamily before creating the font.

How to check if a font supports a specific style

I'm getting the following exception when changing my application font, because I use a strike out in a part of my application, and some fonts don't support it:
I change my application font using a font dialog. I need to check if the selected font supports the strikeout style after assigning it to my application.
What is the recommended way to do this? I know I could create a font with the style and catch the exception, but is there a more elegant way to do it?
Thanks in advance.
EDIT: The user selects a font, not necesary strikeout. In that moment I need to check if the font supports the style strikeout, because I create a strikeout font in a part of my application. If the font don't support the strikeout style would not allow the user to choose that font.
Updated : (to reflect update in the initial post):
InstalledFontCollection ifc = new InstalledFontCollection();
for (int i = 0; i < ifc.Families.Length; i++)
{
if (ifc.Families[i].IsStyleAvailable(FontStyle.StrikeOut))
{
//add particular font with this family to your "font selector"
}
}
If you are using the standard Font class, then you can use the Font.Strikeout property:
//Gets a value that indicates whether this Font specifies a horizontal line through the font.
public bool Strikeout { get; }
Finally I used the following:
private bool SupportStrikeout(Font font)
{
try
{
using (Font strikeout = new Font(font, FontStyle.Strikeout))
{
return true;
}
}
catch (ArgumentException)
{
return false;
}
}

Categories