Change Font.Size in a RichTextBox .SelectionFont with multiple FontFamily - c#

How can I change the Font.Size in a RichTextBox .SelectionFont (which has two or three different FontFamily), without affecting the FontFamilyand the FontStyle
This one works fine, if I have only one Font.Family
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, 12);
But, if I have more, it throws nullexception.

Perhaps this answer from the question: "Reset RTF in RichTextBox?" may help you. I quote:
More you ask?
I use most of this via a small utility class that wraps this for all the styles and font changes. This way you can change font-size and not change font name, etc.

I've had the same problem. Check the answer by LarsTech here:
Changing font for richtextbox without losing formatting
You'll find properties such as SelectionFontName, SelectionFontSize, ...

Related

Set the font in winforms designer to a Font object

I'm working on a project in Windows Forms and I want to use custom fonts for buttons etc.
I have a static class that contains the styles of fonts that I want. For example:
ParagraphFont = new Font(Properties.Resources.Font, 14.25F);
TitleFont = new Font(Properties.Resources.Font, 48, FontStyle.Bold);
I want to be able to apply the font to the form/user control through the designer, but I can't figure out how to do this.
I've tried setting the fonts of all the controls to these variables after the InitialiseComponent() method, but this seems long-winded and tedious to do to the multiple forms and controls that my project has
Is there a quicker way of doing this?
Edit: My question is different to the suggested duplicate. I don't need to set the default font of the project, I want to set the font of each control individually to a style variable. For example:
label.Font = ParagraphFont;
In the designer. This way, I can change the font used in ParagraphFont at any time

richtextbox is shrinking at run time however textbox is not

i'm at a loss as to why my parameter richtextbox is resizing its height at runtime. I'm assuming that it has something to do with my high def displays.
all other fields are simply textbox.
any suggestions as to fields I can check? Form.AutoScale is set to font.
http://imgur.com/a/S0dq0
It seems that this issue is directly related to the use of the font "Microsoft YaHei". Changing the font to San Serif immediately resolved the issue.

Change RichTextBox Font keeping text color

I have a lot of formatted code snippets saved in an XML file.
On request I load them and set as Rtf in a RichTextBox:
string cscode = #"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fprq1\fcharset0 Courier New;}{\f1\fnil\fcharset0..." etc.
rtb_cs.Rtf = cscode;
The code snippets are copy-pasted from the Visual Studio, so the text is colored differently.
Different Visual Studios are using different Fonts for the text.
Is there a way to change the Font but keep the colors?
I've tried to set the Font property of the RichTextBox, but this also resets the colors.
//This changes colors as well
rtb_cs.Font = new Font(FontFamily.GenericSansSerif, 10);
Take a look at LarsTech's solution here:
Changing font for richtextbox without losing formatting
It works for other settings too.

Want to bold few characters of a word get bold in treenode in winforms using c#

i have "search TextBox" to search in treeview, i give result very well. But i want to get those parts get Bold which i typed in "search TextBox" of my winform.
Ex: i Typed Ram then it gives *Ram*esh .
The TreeNode class doesn't support that, its Text is always drawn with one font, the TreeView.Font. Making parts of the text bold is technically possible but very hard to get right. You need to enable custom drawing with the TreeView.DrawMode property and DrawItem event, there's a good example of it in the MSDN Library article.
That's the easy part, the hard problem is that the node is too small to fit the text after you draw parts of it in a bold font. TreeView is missing a "MeasureNodeText" event that would allow you to ask for enough space. The only workaround for that is to lie about the node text and make it artificially wider by prefixing characters. Which you then don't draw in the DrawItem event. Very hard to get consistently right, you'll want to consider a fixed pitch font instead.
I cannot recommend you pursue this unless the feature is really important to you. This otherwise explains why you never see this feature in other programs. Consider changing the color instead of the font weight too. Still hard to glue the pieces together btw.

How can I change the font in a DataGrid?

I am using:
dg.ForeColor = System.Drawing.Color.Black;
dg.BackColor = System.Drawing.Color.Beige;
to set the background and foreground color of a DataGrid. How can I change the font to say Calibri or any others using the built-in methods.
If this is a web application, the DataGrid class already provides a Font property that you can set to
a font of your choosing. You can set it either from the designer or in your source code, just as you've set the BackColor and ForeColor properties.
However, as I mentioned in a comment, you should be very careful about setting controls to use a font that the user might not have installed on their computer. I recommend checking a list of web-safe fonts.
You can change the font with this code
dg.Font=new Font(string familyName,int size);

Categories

Resources