我已经知道如何更改字体:
private void toolStripButton2_Click(object sender, EventArgs e)//italic
{
//maintext is the richTextBox
maintext.SelectionFont = new Font(maintext.Font, FontStyle.Italic);
maintext.SelectionStart = maintext.SelectionStart + maintext.SelectionLength;
maintext.SelectionLength = 0;
maintext.SelectionFont = maintext.Font;
}
但是如何同时允许两种字体,并使字体恢复正常? 此外,您不必先输入文本然后选择它;只需按下按钮。
答案 0 :(得分:1)
您可以定义尽可能多的“选择块”,因为您希望关联者为每个人选择不同的字体。示例代码使文本的前半部分字体样式为斜体,后半部分为粗体。
maintext.SelectionStart = 0;
maintext.SelectionLength = maintext.Text.Length / 2;
maintext.SelectionFont = new Font(maintext.Font, FontStyle.Italic);
maintext.SelectionStart = maintext.Text.Length / 2;
maintext.SelectionLength = maintext.Text.Length - maintext.Text.Length / 2;
maintext.SelectionFont = new Font(maintext.Font, FontStyle.Bold);
maintext.SelectionStart = maintext.Text.Length;
maintext.SelectionFont = new Font(maintext.Font, FontStyle.Regular);
maintext.SelectionLength = 0;