本文介绍了突出显示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.:

所有数据都来自数据库.

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中的特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:52
查看更多