L33*_*EAN 1 c# string richtextbox bold
我试图加粗"You >>"这个字符串的一部分,以显示在富文本框中.
以下是我单击消息发送按钮时的代码.displayBox是字符串的粗体,entryBox是用户输入消息的位置.
private void button1_Click(object sender, EventArgs e)
{
listData.Add(entryBox.Text);
// Remove the linebreak caused by pressing return
SendKeys.Send("\b");
// Empty the array string
ArrayData = "";
// Bold the You >>
displayBox.SelectionStart = 0;
displayBox.SelectionLength = 6;
displayBox.SelectionFont = new Font(displayBox.Font, FontStyle.Bold);
displayBox.SelectionLength = 0;
foreach (string textItem in listData)
{
ArrayData = ArrayData + "You >> " + textItem + "\r\n";
}
entryBox.Focus();
displayBox.Text = "";
displayBox.Refresh();
displayBox.Text = ArrayData;
entryBox.Text = "";
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒.
在评论中@ dash的链接帮助解决了这个问题.链接:http://msmvps.com/blogs/deborahk/archive/2009/10/31/richtextbox-styles.aspx
这是我的代码,它现在代表相同的按钮(虽然我已经重命名了).这可能不是解决这个问题的最干净的解决方案,但是我达到了预期的效果,所以我很满意.评论中对此进行了解释.
private void send_Click(object sender, EventArgs e)
{
if (entryBox.Text != "")
{
listData.Add(entryBox.Text);
// Remove the linebreak caused by pressing return
SendKeys.Send("\b");
// Empty the array string
ArrayData = "";
foreach (string textItem in listData)
{
ArrayData = ArrayData + "You >> " + textItem + "\r\n";
}
entryBox.Focus();
displayBox.Text = "";
displayBox.Refresh();
displayBox.Text = ArrayData;
// Format the "You >>"
displayBox.SelectionStart = 0;
displayBox.SelectionLength = 6;
displayBox.SelectionFont = new Font(displayBox.Font, FontStyle.Bold);
displayBox.SelectionColor = Color.Crimson;
displayBox.SelectionLength = 0;
string wordToFind = "You >>";
int startIndex = 0;
while (startIndex > -1)
{
startIndex = displayBox.Find(wordToFind, startIndex + 1,
displayBox.Text.Length,
RichTextBoxFinds.WholeWord);
if (startIndex > -1)
{
displayBox.Select(startIndex, wordToFind.Length);
displayBox.SelectionFont = new Font(displayBox.Font, FontStyle.Bold);
displayBox.SelectionColor = Color.Crimson;
}
}
// Reset the entry box to empty
entryBox.Text = "";
}
// Remove the linebreak caused by pressing return
SendKeys.Send("\b");
}
Run Code Online (Sandbox Code Playgroud)
我希望这能为任何有类似问题的人提供一些帮助!
:丹