C# 使用C在文本框中更改一个单词的颜色#

C# 使用C在文本框中更改一个单词的颜色#,c#,colors,textbox,C#,Colors,Textbox,我有一个名为txtMessages的文本框,我想更改该文本框中的文本颜色,但不是整个文本,例如: 奎托斯:嗨,我有个问题 我想要KrAToS部分(this.client.NetworkName)涂成红色,但文本的其余部分保持黑色 这是我的代码:希望任何人都能帮助我,提前感谢 private void SendMessage() { if ( this.client.Connected && this.txtNewMessage.Text.Trim(

我有一个名为
txtMessages
的文本框,我想更改该文本框中的文本颜色,但不是整个文本,例如:

奎托斯:嗨,我有个问题

我想要KrAToS部分(
this.client.NetworkName
)涂成红色,但文本的其余部分保持黑色

这是我的代码:希望任何人都能帮助我,提前感谢

    private void SendMessage()
    {
        if ( this.client.Connected && this.txtNewMessage.Text.Trim() != "" )
        {
            this.client.SendCommand(new Proshot.CommandClient.Command(Proshot.CommandClient.CommandType.Message , IPAddress.Broadcast , this.txtNewMessage.Text));
            this.txtMessages.Text += this.client.NetworkName;
            this.txtMessages.Text += " : " + this.txtNewMessage.Text.Trim() + Environment.NewLine;
            this.txtNewMessage.Text = "";
            this.txtNewMessage.Focus();
        }
    }

您可以尝试匹配关键字并根据需要为其上色:

Dictionary<string,Color> dict = new Dictionary<string,Color>(StringComparer.CurrentCultureIgnoreCase);
dict.Add(client.NetworkName,Color.Red);
//you can add more pairs
//build the pattern whenever you finish adding more entries to your dict    
string pattern = string.Format("(?i)({0})",string.Join("|",dict.Select(el=>el.Key).ToArray()));
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
//TextChanged event handler for your richTextBox1
private void richTextBox1_TextChanged(object sender, EventArgs e) {
        int currentIndex = richTextBox1.SelectionStart;
        int currentSelectionLength = richTextBox1.SelectionLength;
        //BeginUpdate
        SendMessage(richTextBox1.Handle, 0xb, IntPtr.Zero, IntPtr.Zero);
        var matches = Regex.Matches(richTextBox1.Text, pattern);
        richTextBox1.SelectAll();
        richTextBox1.SelectionColor = Color.Black;
        foreach (Match m in matches)
        {
            richTextBox1.SelectionStart = m.Index;
            richTextBox1.SelectionLength = m.Length;
            richTextBox1.SelectionColor = dict[m.Value];
        }
        richTextBox1.SelectionStart = currentIndex;
        richTextBox1.SelectionLength = currentSelectionLength;
        //EndUpdate
        SendMessage(richTextBox1.Handle, 0xb, new IntPtr(1), IntPtr.Zero);            
        richTextBox1.Invalidate();
}
Dictionary dict=新字典(StringComparer.CurrentCultureInoRecase);
dict.Add(client.NetworkName,Color.Red);
//您可以添加更多对
//每当您完成向dict添加更多条目时,构建模式
string pattern=string.Format(“(?i)({0})”,string.Join(“|”),dict.Select(el=>el.Key.ToArray());
[DllImport(“user32”)]
私有静态外部int SendMessage(IntPtr hwnd、int msg、IntPtr wParam、IntPtr lParam);
//richTextBox1的TextChanged事件处理程序
私有void richTextBox1_TextChanged(对象发送方,事件参数e){
int currentIndex=richTextBox1.SelectionStart;
int currentSelectionLength=richTextBox1.SelectionLength;
//开始更新
SendMessage(richTextBox1.Handle,0xb,IntPtr.Zero,IntPtr.Zero);
var matches=Regex.matches(richTextBox1.Text,pattern);
richTextBox1.SelectAll();
richTextBox1.SelectionColor=Color.Black;
foreach(匹配中的匹配m)
{
richTextBox1.SelectionStart=m.索引;
richTextBox1.SelectionLength=m.长度;
richTextBox1.SelectionColor=dict[m.Value];
}
richTextBox1.SelectionStart=currentIndex;
richTextBox1.SelectionLength=currentSelectionLength;
//结束更新
SendMessage(richTextBox1.Handle,0xb,新的IntPtr(1),IntPtr.Zero);
richTextBox1.Invalidate();
}

改用
RichTextBox
。您使用的是
winforms
还是
wpf
?@kratose.Best它是怎么工作的?您知道如何在C#中注册事件处理程序吗?在这里发布之前,我已经测试过这段代码(我不太专业,不先测试就在这里编写)。