Q
QSIDeveloper
I am using a RichTextControl (C# VS2005 .NET 2.0) and allowing users to
change the font and color of what ever they select. The problem I am having
is that when I select a second set of character and change the font and or
color the Font and color from the previous change is lost.
This is the code that handles the change
private void ChangeFont()
{
if (InvokeRequired)
{
Invoke(m_ChangeFontD, null);
return;
}
bool wasProtected = false;
string s = richTextBox1.SelectedText;
int selectionStart = richTextBox1.SelectionStart;
int selectionLength = richTextBox1.SelectionLength;
FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.SelectionColor;
Font font = richTextBox1.SelectionFont;
fd.Font = font;
if (fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Focus();
//richTextBox1.SelectionStart = selectionStart;
//richTextBox1.SelectionLength = selectionLength;
richTextBox1.Select(selectionStart, selectionLength); //
regain selection
s = richTextBox1.SelectedText;
if (richTextBox1.SelectionProtected)
{
wasProtected = true;
richTextBox1.SelectionProtected = false; // so the font
canchange
}
richTextBox1.SelectionFont = fd.Font;
richTextBox1.SelectionColor = fd.Color;
richTextBox1.SelectionProtected = wasProtected;
s = richTextBox1.SelectedText;
System.Diagnostics.Debug.Print("SelectedText after font '" +
s + "'");
}
richTextBox1.Select(0,0);
m_TextToInsert = "";
}
change the font and color of what ever they select. The problem I am having
is that when I select a second set of character and change the font and or
color the Font and color from the previous change is lost.
This is the code that handles the change
private void ChangeFont()
{
if (InvokeRequired)
{
Invoke(m_ChangeFontD, null);
return;
}
bool wasProtected = false;
string s = richTextBox1.SelectedText;
int selectionStart = richTextBox1.SelectionStart;
int selectionLength = richTextBox1.SelectionLength;
FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = richTextBox1.SelectionColor;
Font font = richTextBox1.SelectionFont;
fd.Font = font;
if (fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Focus();
//richTextBox1.SelectionStart = selectionStart;
//richTextBox1.SelectionLength = selectionLength;
richTextBox1.Select(selectionStart, selectionLength); //
regain selection
s = richTextBox1.SelectedText;
if (richTextBox1.SelectionProtected)
{
wasProtected = true;
richTextBox1.SelectionProtected = false; // so the font
canchange
}
richTextBox1.SelectionFont = fd.Font;
richTextBox1.SelectionColor = fd.Color;
richTextBox1.SelectionProtected = wasProtected;
s = richTextBox1.SelectedText;
System.Diagnostics.Debug.Print("SelectedText after font '" +
s + "'");
}
richTextBox1.Select(0,0);
m_TextToInsert = "";
}