I am loading a txt File into a textBox with the code below.
For each Line I am checking : if( LoadTopic2.substr(0, 2) == "Ex" )

If that is True I want to select that Row and mark that Line Blue.
I have started some code out below but
dont really know how to select a Row like this.

richTextBox1->Select ????

String^ text;
std::string Load2;

			
for (int id = 0; id < (counting + 1); id++)
{	
String^ Load1 = gcnew String(vec1[id].c_str());
this->richTextBox1->Text = text;
this->richTextBox1->Multiline = true;
this->richTextBox1->WordWrap = false;
text += Load1 + System::Environment::NewLine;	

   MarshalString(Load1 , Load2);

   if( Load2.substr(0, 2) == "Ex" )
   {
	richTextBox1->Select ????
	this->richTextBox1->SelectionColor = Color::Blue;
	this->richTextBox1->DeselectAll();					


				
   }
}
Member Avatar for jencas

IIRC SelStart and SelLength

You could try this, worked for me in a StripToolMenu.

this->richTextBox1->Checked = true;
this->richTextBox1->CheckState = System::Windows::Forms::CheckState::Checked //You can leave System::Windows::Forms:: off if you're using the namespace

Sorry, thought you were asking about getting a row to appear as checked. Either way, I'm supposing there should be something similar to the code above.

I am not really sure how you meen. Is is Start and Length of selected text.

I have tried out a new approach like this to bluemark the lines that begins with "Examples".
I have managed to BlueMark all words "Example" in the textBox like this.
The problem now is that I want to bluemark the whole line that begins with "Examples".
So I need to find the End index of the Row for this line of code.

this->richTextBox1->Select( startPos, 8); //Instead of 8. Endindex of Line should be found ?

String Edit1 = richTextBox1->Text->ToString();

for(int startPos = Edit1->IndexOf("Examples"); -1 != startPos; startPos = Edit1->IndexOf("Examples", startPos + 8))
{
			
this->richTextBox1->Select( startPos, 8);
this->richTextBox1->SelectionColor = Color::Blue;
this->richTextBox1->DeselectAll();
			
}

IIRC SelStart and SelLength

Since you're loading the text from a file, can't you just take the length from the string and use that as your endpoint for the select() function?

Yes that is a good idéa. I did try this before like this.
The problem is that the line doesn´t get Marked Blue anywhere.
I can´t understand why that is not happening.

String^ text;
std::string LoadTopic2;
int Start = 0;

				
for (int id = 0; id < (counting + 1); id++)
{	
String^ LoadTopic = gcnew String(HelpTopic[id].c_str());
this->richTextBox1->Text = text;
this->richTextBox1->Multiline = true;
this->richTextBox1->WordWrap = false;
text += LoadTopic + System::Environment::NewLine;	

MarshalString(LoadTopic, LoadTopic2);
					

   if( LoadTopic2.substr(0, 8) == "Examples" )
   {
      this->richTextBox1->Select( Start, LoadTopic2.length() );      
      this->richTextBox1->SelectionColor = Color::Blue;
      this->richTextBox1->DeselectAll();
   }
       Start = Start + LoadTopic2.length();
					

				
}

Since you're loading the text from a file, can't you just take the length from the string and use that as your endpoint for the select() function?

Are you sure the LoadTopic2.length() returns a value higher than 0?
It's the only thing I can think of preventing it from getting selected..

You could try something like:

int test = LoadTopic2.length()
if(test <= 0)
 System::Windows::Forms::MessageBox::Show("Test not higher than 0!", "Error", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Exclamation);

I tried out this code to show if the value is greater than zero and ShowValue
do show the number 16 wich is the correct length of that row in the textFile.
So I get a bit confused like you :)
I am not sure what to do.

if( LoadTopic2.substr(0, 8) == "Examples" )
{
   stringstream v1;
   std::string v2;
   v1 << LoadTopic2.length();
   v2 = v1.str();
   String^ ShowValue = gcnew String(v2.c_str());
   MessageBox::Show(ShowValue);

      this->richTextBox1->Select( Start, LoadTopic2.length() );
      this->richTextBox1->SelectionColor = Color::Blue;
      this->richTextBox1->DeselectAll();
}
Start = Start + LoadTopic2.length();

Are you sure the LoadTopic2.length() returns a value higher than 0?
It's the only thing I can think of preventing it from getting selected..

You could try something like:

int test = LoadTopic2.length()
if(test <= 0)
 System::Windows::Forms::MessageBox::Show("Test not higher than 0!", "Error", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Exclamation);

Are you sure it isn't working?
This works for me.. But only when the richTextBox has focus, else it isn't visible.

int Start = 0;			
this->richTextBox1->Select(Start, this->richTextBox1->Text->Length);

What exactly is the point in selecting it first, and deselecting it under the same conditions? You wouldn't even notice it being selected, and that's probably the problem too. There's nothing wrong with selecting it, you're just deselecting it right after.

I tried to not use this line also but that didn´t work either.

this->richTextBox1->DeselectAll();

I have tested it 5 times. I did test your code also with no difference.
If I put this code after all code, then "Examples" will get marked blue so it is possible to mark a word blue anyway..

String^ Edit1 = richTextBox1->Text->ToString();
for(int startPos = Edit1->IndexOf("Examples"); -1 != startPos; startPos = Edit1->IndexOf("Examples", startPos + 8))
{
	this->richTextBox1->Select( startPos, 8);
	this->richTextBox1->SelectionColor = Color::Blue;
	this->richTextBox1->DeselectAll();
}

Are you sure it isn't working?
This works for me.. But only when the richTextBox has focus, else it isn't visible.

int Start = 0;			
this->richTextBox1->Select(Start, this->richTextBox1->Text->Length);

What exactly is the point in selecting it first, and deselecting it under the same conditions? You wouldn't even notice it being selected, and that's probably the problem too. There's nothing wrong with selecting it, you're just deselecting it right after.

I found out a "go around" solution. I can select areas after the loop and Mark them blue. So I put flags in variables to remember Start and EndIndex and it will work in this case.

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.