Sign in to follow this  

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

This topic is 3593 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

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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3593 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