[.net] How to point directly to selected text in a textBox and display it?

Started by
3 comments, last by chrisliando 16 years, 2 months ago
I am developing a text comparer and editor. There are 2 textBoxes, Source and Target. So if I found that those 2 files are different, there is a listView which will be filled with list of different lines in Source and Target. If user click one of the listView item, the focus will be pointed to the corressponding line of both textBox and highlighting that line. I have successfully highlighting the lines but I only use small and short file. If I use large file, it just selecting the text without showing. Since I don't set the WordWrap property to true, users must scroll manually to look at it. Here is my code: private: System::Void listView1_DoubleClick(System::Object^ sender, System::EventArgs^ e) { for each (ListViewItem^ item in listView1->SelectedItems) { if ( item->SubItems[1]->Text != "<Whitespace>" ) { txtSource->Select(txtSource->Text->IndexOf(item->SubItems[1]->Text),item->SubItems[1]->Text->Length); txtSource->Focus(); } if ( item->SubItems[2]->Text != "<Whitespace>" ) { txtDest->Select(txtDest->Text->IndexOf(item->SubItems[2]->Text),item->SubItems[2]->Text->Length); txtDest->Focus(); } if ( item->SubItems[1]->Text != "<Whitespace>" && item->SubItems[2]->Text != "<Whitespace>" ) { txtSource->Select(txtSource->Text->IndexOf(item->SubItems[1]->Text),item->SubItems[1]->Text->Length); //txtSource->Focus(); txtDest->Select(txtDest->Text->IndexOf(item->SubItems[2]->Text),item->SubItems[2]->Text->Length); //txtDest->Focus(); } } //for each ListViewItem } // end of listView1_DoubleClick Thank you very much..
Advertisement
Try this:

txtSource->Focus();
txtSource->Select(....);
txtSource->ScrollToCaret();

When you select text, the carat position moves to the first selected character if the text box has focus. So, by setting focus first, we allow the carat to move when the text is selected. Then we instruct the text box to scroll to where the carat is.

I haven't tried this myself, but it should work.
Ok, thank you very much..
Is it possible to select full one line? so that it will be clearer for user to see the difference.

Thank you very much..
To select an entire line, you'd need to scan backward from your starting point to just before the previous newline, and scan forward from your starting point to just before the next newline to get the start and length values. There's no automatic way to do it.
Can you tell me how to implement it in a code sample, please?

Thank you very much..

This topic is closed to new replies.

Advertisement