简体   繁体   中英

RegExp C# and richtextbox

Im trying to make the following thing :

1) using regex to match all strings that have the following pattern "@username" << done i got the pattern @([A-z09_-]){4,20}

2) to parse the text from rich text box and to color those patterns "@somethign" in a color

3) make them clickable & when clicked to insert the clicked string in text box ( only if this is possible without tons of code & libraries )

well . thats basically it .. any help is appreciated :) ' Cheers :)

Use regexp to find all occurances of "@username", and store them in a collection. Then iterate through this collection and do this:

int startpos = 0;
if ( ( startpos = richTextBox1.Find(name) ) > 0 )
{
   richTextBox1.SelectionStart = startpos; 
   richTextBox1.SelectionLength = name.Length;
   richTextBox1.SetSelectionLink(true);
}

Note that this uses an extended richtextbox found here : Link . (The SetSelectionLink is not in the vanilla richtextbox class.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM