Sign in to follow this  

[.net] How to go to the desired line in a textBox?

This topic is 3591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

1. I am developing a text Comparer app. After I found differences of 2 textBoxes, I put those differences on a listView like this: Line # | Source File | Line # | Target File | ------------------------------------------------------ Line 2 | eat | Line 2 | sleep | Line 5 | Nevada | Line 5 | Los Angeles | As can be seen above, I also put line number on the listView.. I want to use the number as a point to be selected when the user double-click on selected listView item. The problem is there might be same String occured on other line. So I must check whether the String user selected is located on the same line as the listView items shown. So I need to go that line and select (full line if possible) the same String as the listView item. I tried this one on the listView_DoubleClick event: for each (ListViewItem^ item in listView1->SelectedItems) { int pos = textBox1->Text->IndexOf(item->SubItems[1]->Text); int pos1 = textBox2->Text->IndexOf(item->SubItems[2]->Text); array<Char>^ chars = {' '}; array<String^>^ tmpln = item->Text->Split(chars); int tmp = Convert::ToInt32(tmpln[1]) - 1; int vline = textBox1->GetLineFromCharIndex(pos); int vline2 = textBox2->GetLineFromCharIndex(pos1); if ( tmp == vline && tmp == vline2 ) { String ^teks1 = item->SubItems[1]->Text; String ^teks2 = item->SubItems[2]->Text; if ( teks1 != " " ) { textBox1->HideSelection = false; textBox1->Focus(); textBox1->Select(pos,teks1->Length); textBox1->ScrollToCaret(); } if ( teks2 != " " ) { textBox2->HideSelection = false; textBox2->Focus(); textBox2->Select(pos1,teks2->Length); textBox2->ScrollToCaret(); } if ( teks1 != " " && teks2 != " " ) { textBox1->HideSelection = false; textBox1->Select(textBox1->Lines[tmp]->IndexOf(teks1),teks1->Length); textBox1->ScrollToCaret(); textBox2->Focus(); textBox2->HideSelection = false; textBox2->Select(textBox2->Lines[tmp]->IndexOf(teks2),teks2->Length); textBox2->ScrollToCaret(); } lnLbl->Text = tmpln[1]; colLbl->Text = pos.ToString(); } } //for each ListViewItem I think the problem is on these 2 lines: textBox1->Select(textBox1->Lines[tmp]->IndexOf(teks1),teks1->Length); textBox2->Select(textBox2->Lines[tmp]->IndexOf(teks2),teks2->Length); On those 2 lines above, I tried to go to the line that match the listView item and select the string that is match the listView item, too.. But it did not work.. I don't know how to select the line that I want.. If it is known then it must have worked. How to go to the line I want and SELECT it? Thank you very much.. How to do this? 2. Is it possible to select empty String? Because there are some points on this comparison that resulted like this: Line # | Source File | Line # | Target File | ------------------------------------------------------- Line 27 | " " | Line 27 | sleep | Line 5 | Nevada | Line 5 | " " | The String " " is my own creation as a value to mark the empty String. Thank you very much..

Share this post


Link to post
Share on other sites

private void button1_Click(object sender, EventArgs e)
{
int max = 5;

if (richTextBox1.Lines.Length < 5)
{
max = richTextBox1.Lines.Length;
}

int char_count = 0;

for (int count = 0; count < max -1; count++)
{
char_count += richTextBox1.Lines[count].Length +1;
}

richTextBox1.SelectionStart = char_count;
richTextBox1.SelectionLength = richTextBox1.Lines[max -1].Length;
richTextBox1.Select();
}
}




The above code will choose line five for you of a richText box (what you should be using instead of a textbox). You should be able to modify this to do what you need.

theTroll

Share this post


Link to post
Share on other sites
Why are you repeating the line number twice?

For example, you could do this:

Line # | Source | Target
------------------------
2 | Eat | Sleep
------------------------
5 | Nevada | Los Angeles
========================

I would do away with the spaces when storing the string, but would store it like this line_#|source|target so that the line 2 comparison would look like "2|Eat|Sleep". This also helps when you've got empty space as the string would look like "2||Sleep" which would produce an empty string when tokenized. Plus, you're not repeating the line twice which looks cleaner.

Share this post


Link to post
Share on other sites

This topic is 3591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this