Reputation: 401
when the selection area is combined with 2 areas that has different fonts, it returns null. The code is below:
private Font myGetSelectionFont()
{
Font t = this.workspace.SelectionFont;
if (t == null)
return defaultFont;
else return t;
}
private void toolStripComboBox_size_SelectedIndexChanged(object sender, EventArgs e)
{
string sizeString = ((ToolStripComboBox)sender).SelectedItem.ToString();
float curSize = float.Parse(sizeString);
Font oldFont = myGetSelectionFont();
Font newFont = this.getFont(oldFont.FontFamily.Name, curSize, oldFont.Style);
this.setFontIcons(newFont);
this.workspace.SelectionFont = newFont;
this.workspace.Focus();
}
There is no problem to set a different font for the selected area, but i don't know the font size and other properties for the selected area. what should i do?
I think I can choose to set the font as the font before the selected area, or behind the selected area, but i don't know how to do it.
Upvotes: 3
Views: 1443
Reputation: 2984
A possible solution would be to loop
private void toolStripComboBox_size_SelectedIndexChanged(object sender, EventArgs e)
{
float size = float.Parse(((ToolStripComboBox)sender).SelectedItem.ToString());
SetFontSize(richTextBox1, size);
}
private void SetFontSize(RichTextBox rtb, float size)
{
int selectionStart = rtb.SelectionStart;
int selectionLength = rtb.SelectionLength;
int selectionEnd = selectionStart + selectionLength;
for (int x = selectionStart; x < selectionEnd; ++x)
{
// Set temporary selection
rtb.Select(x, 1);
// Toggle font style of the selection
rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily, size, rtb.SelectionFont.Style);
}
// Restore the original selection
rtb.Select(selectionStart, selectionLength);
}
Upvotes: 2