2013-10-26 69 views
0

我已经知道如何更改字体:如何更改文本的字体?

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; 
    } 

但是我怎么允许两种字体的同时,使字体恢复正常? 也可以让你不必先键入文字然后选择它;只需按下按钮。

回答

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; 
相关问题