Jump to content
  • Advertisement
Sign in to follow this  
chrisliando

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

This topic is 3962 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
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.

Share this post


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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!