需要先选中修改的字符: public void SelectMyString() { // Create a string to search for the word "fox". String searchString = "fox"; // Determine the starting location of the word "fox". int index = textBox1.Text.IndexOf(searchString, 16, 3); // Determine if the word has been found and select it if it was. if (index != -1) { // Select the string using the index and the length of the string. textBox1.Select(index, searchString.Length); } }
richTextBox1.SelectionColor = System.Drawing.Color.Red;这些属性只影响选定的文本,如果未选定文本,则只影响在当前插入点位置键入的文本
http://www.codeproject.com/cs/miscctrl/csexrichtextbox.asp
我也是这么写的,但在键入新内容时,依然是arial字体。
yourRichBox.Focus();
yourRichBox.SelectionFont = new Font( "宋体", 14 );
yourRichBox.SelectionColor = Color.Purple;
public void SelectMyString()
{
// Create a string to search for the word "fox".
String searchString = "fox";
// Determine the starting location of the word "fox".
int index = textBox1.Text.IndexOf(searchString, 16, 3);
// Determine if the word has been found and select it if it was.
if (index != -1)
{
// Select the string using the index and the length of the string.
textBox1.Select(index, searchString.Length);
}
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Focus();
richTextBox1.SelectionFont = new Font("新宋体", 9);
richTextBox1.SelectionColor = Color.Black;
}在设计状态下,Font属性和ForeColor属性也是这么设置的。但运行时,只要输入新的内容,显示的字体就是arial,而不是我设置的,我在keydown事件中显示了richTextBox1.SelectionFont属性,验证了这一点。
我的目的很简单,只是想让RichTextBox以我想要的字体显示我输入的内容。结果我新输入的内容都是以arial字体显示的。而在设计状态时输入的内容倒是和Font属性一致,这个算是bug吗?
richTextBox1.Focus();
之前加上
this.Show();