How to check if a font supports a specific style - c#

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;
}
}

Related

How to create unstable font sizes?

I have created a TextBox on the code
RichTextBox tb3 = new RichTextBox();
Then I wrote an other code (in the same void) to change it's font:
tb3.Font.Size(FontSize.Text);
But the following error appeares : "non-invocable member 'Font Size' cannot be used like a method.
Note: "FontSize" is an ID for a textbox.
How can I do it without creating a new Font?
Please help!
tb3.Font.Size isn't a method, it is
public float Size { get; }
and you can't change it so.
if you want to change font see it -> link1
and it link2
you should create new font, for example:
tb3.Font = new Font(tb3.Font.FontFamily, 23, FontStyle.Regular);

How to set DefaultFont to custom font in OxyPlot?

I am using OxyPlot in a WPF application to plot a simple LineSeries. I also defined Roboto as the standard font of my application. However my PlotView does not use this font but uses its default font Segoe UI.
The PlotModel offers a DefaultFont property to change this font. This property does not accept a FontFamily but only a string. So if I try to modify the font by entering a FontFamily name which is installed on my system it works:
var model = new PlotModel
{
DefaultFont = "Verdana"
};
If I try to do the same with Roboto it does not:
var model = new PlotModel
{
DefaultFont = "./Resources/Roboto/#Roboto"
};
or
var model = new PlotModel
{
DefaultFont = "/MyNamespace;component/Resources/Roboto/#Roboto"
};
What am I missing here? Is there maybe another way to accomplish this?
Note: This is my first question, please tell me what I can improve in the future.

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 can I compare or verify Button.Background to a hexadecimal color in Silverlight using C# code programmatically?

Updated:
How can I compare or verify Button.Background to a hexadecimal color such as {#FF008F06} etc.. in Silverlight application using C# code programmatically?
The Background property is of type Brush, so it can be a SolidColorBrush or a GradientBrush and so on.
I think you need to distinguish between the various types and perform the correct comparison.
For example:
if(Button.Background is SolidColorBrush)
{
bool isRed = ((SolidColorBrush)Button.Background).Color == Colors.Red;
}
else if (Button.Background is GradientBrush)
{
...
}
To compare a to a specific hexadecimal color:
bool flag = ((SolidColorBrush)Button.Background).Color ==
(Color)ColorConverter.ConvertFromString("#FF008F06");
EDIT Test Assert
var Greenish = new SolidColorBrush(Colors.Green);
Assert.AreEqual(Greenish.Color, ((SolidColorBrush)Button.backGround)).Color;

How-To set Height of a Textbox?

For my single line Textbox, I set is Border = None. On doing this, the height turns very small. I can't programamtically set the height of the textbox. If I set any border, then again its fine, but I don't want any border. Even the text is not visible completely - so the font size is already bigger the the textbox height.
I tried creating a custom textbox, and set the Height of it, but it has no effect. How to handle this situation? Any help is highly appreciated.
There is a simple way not to create a new class.
In Designer.cs file:
this.textBox1.AutoSize = false;
this.textBox1.Size = new System.Drawing.Size(228, 25);
And that's all.
TextBox derives from Control, which has an AutoSize property, but the designers have hidden the property from the PropertyGrid and Intellisense, but you can still access it:
public class TextBoxWithHeight : TextBox {
public TextBoxWithHeight() {
base.AutoSize = false;
}
}
Rebuild and use.
TextBox controls automatically resize to fit the height of their Font, regardless of the BorderStyle you choose. That's part of the defaults used by Visual Studio.
By changing the Multiline, you can override the Height.
this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif",
26.25F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.textBox1.Location = new System.Drawing.Point(373, 502);
// this is what makes the height 'stick'
this.textBox1.Multiline = true;
// the desired height
this.textBox1.Size = new System.Drawing.Size(100, 60);
Hope this helps.
I just created this case in an empty project and don't see the result you are describing.
When the BorderStyle is none, the display area of the Textbox auto-sizes to the font selected. If I then set Multiline = true, I can change the height portion of the Size property and the change sticks.
Perhaps another portion of your code is modifying the height? A resize event handler perhaps?
My suggestions:
Post the relevant portions of your code
Try to reproduce the issue in an empty WinForms project (as I just did)
I find the best solution is to subclass the Textbox and expose the hidden AutoSize there:
public class TextBoxWithHeight : TextBox
{
public bool Auto_Size
{
get { return this.AutoSize; }
set { this.AutoSize = value; }
}
}
Now you can set the Autosize on or off using the object inspector in the visual designer or in code, whatever you prefer.
Just select your textbox and go to properties then increase your font size.. DONE !!!

Categories