If I want to find the start and end position of the word "how" on these two rows in a textBox.
Is there any other approach then substring each line or is it the substring method I am going to use. ?

//text in textBox

Hi, how are you
good how are you

Is it possible to use ->Contains to get the start and end index of the string "how" or more important, is it possible to Select "how" in the textBox if found ?

String^ str = "how";

if (richtextBox1->Text->Contains(str))
{
    MessageBox:: Show("found how");
}

I think you could use directly the IndexOf() method, i.e
richtextBox1->Text->IndexOf("how")
and once you get the index (or -1, if not found), you probably already know how to select the text in a TextBox.

Thank you, I have tried out to do this code that look like this:

if (richTextBox1->Text->Contains("how"))
{
this->richTextBox1->Select( richTextBox1->Text->IndexOf("how"), 3);
this->richTextBox1->SelectionColor = Color::Blue;
this->richTextBox1->DeselectAll();
}

If I have these lines in the textBox, then only the First how on the first line will be marked blue. So this works fine but what I am trying is to mark all words that is "how"
Could this be possible in any way ?

hello how are you how
hello how


I think you could use directly the IndexOf() method, i.e
richtextBox1->Text->IndexOf("how")
and once you get the index (or -1, if not found), you probably already know how to select the text in a TextBox.

You have to use the IndexOf() in a loop, like

for(int startPos = richTextBox1->Text->IndexOf("how");
     -1 != startPos;
     startPos = richTextBox1->Text->IndexOf("how", startPos + 3))
{
    // something here
}

I have tried this out to set a loop for the richtextBox. I use the code as seen inside a buttoncontrol but still only the first "how" is selected and turns blue. It seems that every found "how" should be painted blue but peheps something is missing. I experimenting but cant find what it could be.

private: System::Void button2_Click_3(System::Object^  sender, System::EventArgs^  e) 
{
		
for(int startPos = richTextBox1->Text->IndexOf("how"); -1 != startPos; startPos = richTextBox1->Text->IndexOf("how", startPos + 3))
   {
			
   this->richTextBox1->Select( richTextBox1->Text->IndexOf("how"), 3);
   this->richTextBox1->SelectionColor = Color::Blue;
   this->richTextBox1->DeselectAll();
			
    }	

}

//text in textbox

hello how are you how
hello how

Instead of

this->richTextBox1->Select( richTextBox1->Text->IndexOf("how"), 3);

use

this->richTextBox1->Select( [B]startPos[/B], 3);

i.e. you were simply always getting the first index of the string 'how'.

Yes, that is right. Now I understand how that works.
Thank you for your help !

Instead of

this->richTextBox1->Select( richTextBox1->Text->IndexOf("how"), 3);

use

this->richTextBox1->Select( [B]startPos[/B], 3);

i.e. you were simply always getting the first index of the string 'how'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.