突出显示RichTextBox中的特定文本 [英] Highlight specific text in richtextbox

查看:255
本文介绍了突出显示RichTextBox中的特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含listbox和一些richtextboxex的窗口形式. listbox包含一些值.当我从listbox中选择任何值时,richtextboxex会根据选择的值与数据绑定.

I have a window form that contain a listbox and some richtextboxex. listbox contains some values. When I select any value from listbox, richtextboxex bind with data according to selected value.

当我从列表框中选择一个值时,我必须突出显示一些绑定到richtextbox的文本,例如:

I have to highlight some text which is bind to richtextbox when I select a value from listbox, e.g.:

谨在此提醒您,您有<<< OverdueInvCount >>>逾期发票,且有<<< OverdueInvTotal >>.如果您对所欠款项有任何疑问,请给我们打电话,我们将很乐意与您讨论.如果您已经发送了付款,请忽略此提醒.

Just a friendly reminder that you have <<OverdueInvCount>> overdue invoice(s), with an overdue balance of <<OverdueInvTotal>>. If you have any questions about the amount you owe, please give us a call and we’ll be happy to discuss it. If you’ve already sent your payment, please disregard this reminder.

所有数据都来自数据库.

All data is coming from database.

我要突出显示<<OverdueInvCount>><<OverdueInvTotal>>这两个字.

I want to highlight <<OverdueInvCount>> and <<OverdueInvTotal>> these words.

推荐答案

在不转换为具有所需功能的新对象的情况下实现此目的的一种方法是覆盖ListBox DrawItem

One way to do that without casting to new object with functionality you want is to override ListBox DrawItem

void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    var item = listBox1.Items[e.Index] as <Your_Item>;

    e.DrawBackground();
    if (item.fIsTemplate)
    {
        e.Graphics.DrawString(item.Text + "(Default)", new Font("Microsoft Sans Serif", 8, FontStyle.Regular), Brushes.Black, e.Bounds);
    }
    else
    {
        e.Graphics.DrawString(item.Text, new Font("Microsoft Sans Serif", 8, FontStyle.Regular), Brushes.Black, e.Bounds);
    }
    e.DrawFocusRectangle();
}

并将其添加到构造函数中(在InitializeComponent();调用之后)

and add this in your constructor (after InitializeComponent(); call)

listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);

这篇关于突出显示RichTextBox中的特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆