2012-04-30 80 views
1

我想粗体显示在富文本框中的该字符串的"You >>"部分。C#在RichTextBox中粗体显示字符串的一部分

以下是我单击消息发送按钮时的代码。 displayBox是其中id为粗体的字符串,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 = ""; 

    } 

任何帮助将是伟大的。

+1

见http://msmvps.com/blogs/deborahk/archive/2009/10/31/richtextbox-styles.aspx对工作的例子。看起来您在添加文本之前设置了格式 - 它应该是相反的方式(当有东西需要选择!) – dash

+0

感谢您的链接。你是对的,我是在实际字符串被绘制到richText框之前设置格式。我现在唯一的问题是,只有第一个“你”是粗体。任何关于如何为每个“You >>”实现的代码指针? – L337BEAN

+0

我已通过您发布的链接帮助解决了该问题。再次感谢@dash – L337BEAN

回答

5

此问题已在评论中的@ 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"); 
    } 

我希望这可以为任何人提供类似的问题一些帮助!

:丹

+1

+1良好的工作。当我在评论中留下链接时,我通常会回弹并添加答案,因为在网站关闭或移动内容时,目标链接在网络上消失是非常常见的。你已经在SO上保存了这里的后代。有很多事情比“,答案只是在这个链接”更令人沮丧,点击,找不到页面! – dash

+0

谢谢@dash。我确切地知道你的意思! – L337BEAN

相关问题