Click here to Skip to main content
16,018,442 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can get content of a line using selectstart, when I want to know position of selected string. How to know string's position in richtextbox after I get its position in line?

Get content of line:
C#
string[] RichTextBoxLines = rtb.Lines;
foreach (string sline in RichTextBoxLines)
{
  label1.Text= sline;
  label2.Text=label1.Text.IndexOf("mk").ToString();
  int t = label1.Text.IndexOf("mk");
  //rtb.Select(string's postion in richtextbox, "mk".Length);
  //Substring or  do something

}

thanks
Posted
Updated 31-Jul-12 4:26am
v2
Comments
Sergey Alexandrovich Kryukov 30-Jul-12 2:49am    
You need to tell us exact type of RichTextBox, as the solutions for different types are very different.
--SA
Kenneth Haugland 31-Jul-12 11:13am    
I would guess this would involve the caret position anyway, so you'll have to do a search since you dont give us any information.

1 solution

You could try this:

C#
int charIndex = 0;

foreach (string sline in rtb.Lines)
{
  label1.Text= sline;
  label2.Text = label1.Text.IndexOf("mk").ToString();

  charIndex += sline.IndexOf("mk");

  rtb.Select(charIndex, "mk".Length);

  charIndex += sline.Length - sline.IndexOf("mk") + 1;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900