如何在richTextBox上创建字符串Bold的特定部分?
我有一个基于Form和2 richTextBoxes的聊天应用程序!
richTextBox1
用于显示所有对话
richTextBox_TextToSend
用于键入要发送的消息
当用户键入消息并按Enter键时,输入的文本将出现在richTextBox1中
private void button1_Click(object sender, EventArgs e) { // insert message to database if(richTextBox_TextToSend.TextLength>0) { string txt = richTextBox_TextToSend.Text; // send the typed message sendMessage(from,to,task_id,txt); // show the typed text in the richTextBox1 richTextBox1.Text += from+": "+richTextBox_TextToSend.Text+"\n"; richTextBox_TextToSend.Clear(); } }
类型字符串的变量包含发送消息的人的名称(使用应用程序的用户)
如何在正常字体样式中以粗体和其他文本显示名称,因此在键入消息后,我明白了
Chakib Yousfi :您好……
代替
Chakib Yousfi:你好……
任何帮助将受到高度赞赏。
首先,您应该选择要加粗的文本:
richTextBox1.SelectionStart = 0; richTextBox1.SelectionLength = 13;
然后,您可以为所选文本定义样式:
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
使用此代码:
private void button1_Click(object sender, EventArgs e) { // insert message to database if(richTextBox_TextToSend.TextLength>0) { string txt = richTextBox_TextToSend.Text; int start = richTextBox1.TextLength; string newMessage = from + ": " + richTextBox_TextToSend.Text + Environment.NewLine; // send the typed message sendMessage(from,to,task_id,txt); // show the typed text in the richTextBox1 richTextBox1.AppendText(newMessage); richTextBox_TextToSend.Clear(); richTextBox1.Select(start, from.Length); richTextBox1.SelectionFont = New Font(richTextBox1.Font, FontStyle.Bold); } }