Reputation: 1
all. I'm writing a programm at C# .NET. I have a richtextBox in my form(Windows Form). I open a text file and put text in richtextBox. I need to format selected text with FontDialog. But it's changed all text, not selected. What method can be applied, to FontDialog changed only selected text?
My code:
if (fontDialog1.ShowDialog() != DialogResult.Cancel)
{
if (richTextBox1.SelectedText.Length > 0)
{
richTextBox1.Font = fontDialog1.Font;
richTextBox1.ForeColor = fontDialog1.Color;
}
thanks.
Upvotes: 0
Views: 94
Reputation: 304
User RichTextBox.SelectionFont property
if (fontDialog1.ShowDialog() != DialogResult.Cancel)
{
if (richTextBox1.SelectedText.Length > 0)
{
richTextBox1.SelectionFont = fontDialog1.Font;
richTextBox1.SelectionColor = fontDialog1.Color;
}
Check out http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members%28v=vs.85%29.aspx for the list of all members of RichTextBox
Upvotes: 0
Reputation: 136164
The documentation for SelectedText
gives some good help
richTextBox1.SelectionFont = fontDialog1.Font
richTextBox1.SelectionColor = fontDialog1.Color;
Upvotes: 0
Reputation: 3317
You need to use the SelectionFont & SelectionColor properties of the richtextBox: because the properties you are using apply to the full contents of the control
if (fontDialog1.ShowDialog() != DialogResult.Cancel)
{
if (richTextBox1.SelectedText.Length > 0)
{
richTextBox1.SelectionFont = fontDialog1.Font;
richTextBox1.SelectionColor = fontDialog1.Color;
}
Upvotes: 3