Problems while using installed font in c# winform application - c#

In my application, i want user to select a font, from a list of fonts available in user's system and set that font to richtextbox.
I tried ::
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (FontFamily family in fonts.Families)
{
comboBox1.Items.Add(family.Name);
}
and
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
richTextBox1.Font = new Font(comboBox1.SelectedText, 14);
}
but it changes the default behaviour of the text. For example: text includes headings in bold and italics. Setting the font with above code transforms the text in the regular form.
My question is, to possibly keep the behaviour of the text as before and not to change the complete text in regular form by keeping the bold text in bold and italics in italics.

SelectedText is for the highlighted part of the ComboBox text property. Probably not what you want:
richTextBox1.Font = new Font(comboBox1.Text, 14);
Also, if you just want the highlighted part of the RichTextBox to have the new font:
richTextBox1.SelectionFont = new Font(comboBox1.Text, 14);
If any part of the highlighted text has a mix of bold and italics and different sizes, etc, the RichTextBox does not preserve those properties very well. See How do I maintain RichText formatting (bold/italic/etc) when changing any one element?

There's an option to initialize a font with a font prototype. Try defining a font prototype as a font with micrsoftsansserif, with desired size and style, and then pass the new font as the second parameter. msdn has all the info.

Related

WPF - Copying the content of a RichTextBox into a Paragraph

I'm currently trying to create a simple program in WPF to make cards for a custom card game.
Some of the descriptions feature rich text (like bold and italics) to highlight important things. The effect and the flavor text are copied into the same RichTextBox for aesthetic and convenience, which is why I need to be able to copy rich text from an input into a line/paragraph that is added to the display.
Here is the code:
private void EffectInput_TextChanged(object sender, TextChangedEventArgs e)
{
TextRange temp = new TextRange(EffectInput.Document.ContentStart, EffectInput.Document.ContentEnd);
Console.WriteLine(temp.Text);
effectText.Inlines.Clear();
effectText.Inlines.Add(new Run(temp.Text));
updateDescription();
}
private void updateDescription()
{
Description.Document.Blocks.Clear();
Description.Document.Blocks.Add(effectText);
Description.Document.Blocks.Add(flavorText);
}
I managed to make it work with the Text inside the box, but I can't find a way to also copy the format of the text. Can anyone help me?

How can I change the FontFamily Property in all Controls in Windows Forms Application?

I've been searching here for a way to change only the FontFamily of all Controls in my Windows Forms App. Note that I don't want to change their font sizes.
Here's the situation: To promote cross-Windows compatibility, and yet, create good UI, the FontFamily in pre-Vista computers will have one font (Tahoma) and newer versions will work with Open Sans, from Google (the Open Sans font will be distributed with the project).
I've managed to change the fonts in older versions of Windows using the Environment.OSVersion property and creating an Application Setting (UserFont) on my Form1. Like this:
private void Form1_Load(object sender, EventArgs e)
{
if (Environment.OSVersion.Version.Major == 5)
{
Properties.Settings.Default.UserFont = new Font("Tahoma", 12);
}
else if (Environment.OSVersion.Version.Major >= 6)
{
Properties.Settings.Default.UserFont = new Font("Open Sans", 12);
}
}
So, when the form loads, it changes all child controls to Tahoma or Open Sans, but it also changes the font size to 12. That leads to my question:
How can I change only the FontFamily of all Controls in the form keeping their size and styles?
P.S.: I thought of create several Application Settings for all the font sizes that I'll work with, but I really want a more simple solution.
Thank you :)
I have the same issue and what I did is just simple like this, with a couple of code lines:
In design, set the Open Sans as the initial font for your main form/window
(also, you should set all children controls to inherit their parents' font).
In code, first check the OS version:
if (!IsWinVistaOrHigher) {
this.Font = new Font("Tahoma", 12);
}
and all the children will be automatically inherited. If false, the Open Sans is loaded as if nothing's ever happened.
If there is just a few controls with fonts to be modified, the sample code below can keep their original font sizes:
label1.Font = new Font("Tahoma", label1.Font.Size);
label2.Font = new Font("Tahoma", label2.Font.Size);
If there are lots of font sizes to be changed, just loop through controls and set the font. The key here is the use of controlName.Font.Size, and you can adjust by controlName.Font.Size - 1
For further dig-in, the beneath code might help you to get more ideas:
Consider you're drop all controls in a Panel/ TableLayoutPanel/ FlowLayoutPanel, then the loop might be like this:
foreach (Control c in FlowLayoutPanel1.Controls) {
if (c.Controls != null) {
c.Font = new Font("Tahoma", c.Font.Size)
}
}
In general, a loop procedure could help:
public void SetAllControlsFont(ControlCollection ctrls) {
foreach(Control c in ctrls) {
if(c.Controls != null) {
SetAllControlsFont(c.Controls);
}
c.Font = new Font("Tahoma", c.Font.Size);
}
}
Then call it in Form1_Load() event by: SetAllControlsFont(this.Controls);
Note that the code might not work for lack of convert/cast type, but I hope you've got the general idea. Just comment here and I will come back.

How can I diplay whole text in RichTextBox using preseted font

in c# I'm trying to display text (from txt file) in RichTextBox and I want display whole text using the preseted font (Arial). When I type the text "test ěščřžýáíé" it displays ok but when I try to set the Text programmaticaly
RichTextBox.Text="test ěščřžýáíé";
the words/characters are displayed with different fonts (the word "test" is really displayed in Arial). What do I do wrong?
Thanks for any help
Pavel
How are you setting the font? I assume you are using the Win Forms RichTextBox. I have managed to produce a very simple application which does what you have described as what you need:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Font = new System.Drawing.Font(FontFamily.GenericSansSerif, 12.0f);
richTextBox1.Text = "test ěščřžýáíé";
}
With a Button button1 and a RichTextBox richTextBox1. When the button is clicked the text appears.

Windows Phone Checkbox Strikethrough/Change fontstyle

I have a list of checkboxes which, when checked, the content (text) should then be strikethrough or even just to change the fontstyle (colour) of the checked item. I'm trying to do this in a checkbox_Tap event handler which is in my codebehind page.
Any ideas as to how I could change the fontstyle or how I could make it strikethough?
The Design Guidelines mention that you should always use the system font unless you have a specific "brand", although there isn't anything forcing you to follow this.
The CheckBox itself, though, has properties you can set in the code-behind for FontFamily, FontSize, FontStretch, FontStyle, Foreground, etc.
In your CheckBox_Tap event handler, you could do something like:
void CheckBox_Tap(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
cb.FontFamily = //<your new font here>
cb.FontStyle = // <new style here>
}
As for Strikethrough - it doesn't look like there is a built in way to do that (there isn't a specific property for Strikethrough that you can just set to True, for example).
However, I did run across this post, and there is an answer that shows a hacky way to do this in XAML (which I haven't tried) using a Border. I'm wondering if you could try this, and wrap your CheckBox in a Border which is invisible by default, and then make it visible when you want the Strikethrough?
Hope this helps!

How do I change the color of a word inside a listbox

I made a Form with a TextBox that accepts a word and searches a bunch of sentences to see if any of them contains that word .After that I have to appear those sentences and highlight the word .My plan is to make a ListBox and add the sentences inside of it. My problem is how to highlight the word (by changing the color I suppose) so it can be distinguished.
Is there a preferable way?
I chose ListBox so I can select the sentence I'm looking for.
Edit
According to #Thorsten Dittmar directions a create an owner drawn list box.
public partial class Form1 : Form
{
private List<string> _items;
public Form1()
{
InitializeComponent();
_items = new List<string>();
_items.Add("One");
_items.Add("Two");
_items.Add("Three");
listBox1.DataSource = _items;
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(_items[e.Index],
new Font(FontFamily.GenericSansSerif,
8, FontStyle.Bold),
new SolidBrush(Color.Red), e.Bounds);
}
}
How I'm going to split the sentence in order to draw only one word?
Edit2
The way I finally did it was to make two seperate components, to compine my options.
One was a ListBox with all the sentences colored and the option to select one
of those and the other one a RichBox with separate colored words since is to difficult
to achieve that with the ListBox (for me a least).
The way I accomplished that was by using a boolean array pointing which word should
be colored in each sentence.
for (int i = 0; i < words.Length; i++)
{
if (segments[i]) //<-boolean array
{
rich.SelectionColor = Color.Red;
rich.AppendText(words[i] + " ");
rich.SelectionColor = Color.Black;
}
else
{
rich.AppendText(words[i] + " ");
}
}
There is no standard way of doing it in Windows Forms. You'd have to render the list items manually (create an owner drawn list box). In WPF this would be an easy task.
EDIT
Drawing only part of a string in a different font is not an easy task. What I'd try is the following:
Introduce tokens that tell you "bold start" and "bold end" - a bit like in HTML. Let's call them the same as in HTML. So your string could look like this:
Hello, I am <b>bold</b> text<b>!</b>
Now I'd tokenize my string into text that is non-bold and text that is bold. I'd get the following parts:
Hello, I am
bold
text
!
Now I'd draw each part using the following algorithm:
Draw string in current format at current position x
increase position x by width of the string drawn in step 1
change formatting according to upcoming string
goto 1
In step 2 the Graphics.MeasureString method would be called to get the width of the string.
Doing this for the 4 sample parts above would result in:
Hello, I am
Hello, I am bold
Hello, I am bold text
Hello, I am bold text !
A simple TextBox can have its Foreground property set, but it applies to the entire text within the TextBox.
If you want specific words to be "highlighted", you either need to split the sentence in several TextBoxes (dirty), or make use of a RichTextBox
Giannosfor, in response to your comment, you'll have to use the parameter e of the event handler to choose which item you want to hightlight (link here).
Look at the response from Shadow Wizard and particularly at the use of e.Index.
Graphics g = e.Graphics;
...
g.FillRectangle(new SolidBrush(color), e.Bounds);
Variable g represent the graphic part of your current item e. Method FillRectangle allows you to change the color of the item's background.
Edit 1:
I tried to do as you say in the comment below but it seems there is no way to hightlight only a part of a string using ListBox. To me it seems the only control that is able to support that is the RichTextBox. A solution might be to implement your own user control in the form of a list of RichTextBoxes.
Building on #Thorsten Dittmar answer, I developed pretty much exactly what you are looking for in a single ListBox. You can find it at https://advancedlistbox.codeplex.com/.

Categories