Highlight selected text in richtextbox

I have a window shape that contains listboxsome richtextboxex. listboxcontains some values. When I select any value from listbox, it richtextboxexbinds to the data according to the selected value.

I need to select some text that is attached to richtextboxwhen I select a value from the list, for example:

Just a friendly reminder that you have <<<<<<<<Super ViewPlay → past due accounts (s), with past due balance <<OverdueInvTotal →. If you have questions about the amount you owe, please give us a call and discuss this with pleasure. If you have already sent a payment, please do not pay for this reminder.

All data comes from the database.

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

0
source share
2 answers

Something like this should work (it just checked .. it seems to work fine):

int openBrace = richTextBox.Text.IndexOf("<");
while (openBrace > -1) {
    int endBrace = richTextBox.Text.IndexOf(">", openBrace);
    if (endBrace > -1) {
        richTextBox.SelectionStart = openBrace;
        richTextBox.SelectionLength = endBrace - openBrace;
        richTextBox.SelectionColor = Color.Blue;
    }
    openBrace = richTextBox.Text.IndexOf("<", openBrace + 1);
}
0
source

One way to do this without adding a new object with the required functionality 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();
}

and add this to your constructor (after the call InitializeComponent();)

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

All Articles