Showing selection in wxTextCtrl Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Showing selection in wxTextCtrl

Post by Widgets »

In my current project I want to implement a 'find' feature within one of several a wxTextCtrl with the following code.
FWIW, the code makes sure the appropriate window is selected.

Code: Select all

void MyFrame::DoFind( const wxString arc_ws2Find, wxTextCtrl* a_pTextCtrl )
{
  long lStart;
  long lFoundPos;
  long Last;
  bool bDown = true;
  bool bMatchCase = false;
  a_pTextCtrl->ShowPosition( 0 );
  wxString wsContent = a_pTextCtrl->GetValue();
  if ( !bMatchCase )
    wsContent = wsContent.MakeLower();
  if (bMatchCase )
	lFoundPos = (bDown) ? wsContent.find(arc_ws2Find) : wsContent.rfind(arc_ws2Find);
  else
	lFoundPos = (bDown) ? wsContent.Lower().find(arc_ws2Find) : wsContent.Lower().rfind(arc_ws2Find);
  if ( lFoundPos == wxNOT_FOUND )
    return;

  long lStartPos = (bDown) ? a_pTextCtrl->GetInsertionPoint() + lFoundPos : lFoundPos;
  long lEndPos = lStartPos + arc_ws2Find.length();

  a_pTextCtrl->SetSelection(lFoundPos, lFoundPos + arc_ws2Find.length());
  a_pTextCtrl->ShowPosition( lFoundPos + arc_ws2Find.length() );  // <<<<<everything works up to here
  // none of these show the selection
//    a_pTextCtrl->Update();//SetFocus();//Layout();//Refresh();
//    a_pTextCtrl->SendSizeEventToParent();
//    a_pTextCtrl->LineDown();
    a_pTextCtrl->Refresh();
    ::wxYield();
//    a_pTextCtrl->FitInside();
//    a_pTextCtrl->ShowPosition( lFoundPos + arc_ws2Find.length() );
}
The code works well enough, except it does not show the selection as highlighted until I switch to a different text ctrl ( within the same wxNotebook) and then back to the one of interest.
The big question: how can I make the selection appear without the extra switching. In fact, the only way I noticed that the selection had been done, was when I by chance ended up switching between the 2 windows
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Showing selection in wxTextCtrl

Post by doublemax »

Check if it's just a redraw issue by calling a_pTextCtrl->Refresh() afterwards.
Use the source, Luke!
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Showing selection in wxTextCtrl

Post by Widgets »

I was already calling Refresh() and tried a few more calls in the calling function, but it does not help.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Showing selection in wxTextCtrl

Post by doublemax »

This works out of the box for me by adding a few lines to the "minimal" sample:

Code: Select all

wxPanel *panel = new wxPanel(this, wxID_ANY);
_tc = new wxTextCtrl(panel, wxID_ANY, "jds ljskljdsflsjdflkjsdlfj sldf jdlsk fjsldkj flsdkj flskjd lfj dsl fjl", wxPoint(10,10), wxSize(400,300), wxTE_MULTILINE);

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
  _tc->SetSelection(10,20);
}
Try this and if it works for you, strip down your code piece by piece.
Use the source, Luke!
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Showing selection in wxTextCtrl

Post by Widgets »

It gets curioser all the time.
When I strip all code following the SetSelection(), I can execute the search by clicking on the 'search 'button' - the magnifying glass - the control's text is positioned to make the line with the search string visible at the top of the window.
If I then right-click, I get a pop up context menu, and the selection shows.

If I add a call to a_pTextCtrl->SetFocus(); then, if I execute the search with pressing 'Enter' after the search string, it shows the line with the search string, and the selection is highlighted.
But if I click on the 'search' button, the search is executed, the expected line becomes the top line in the window, but the selection is NOT highlighted - unless I right-click in the window to pop up the context menu.
FWIW, it does not seem to matter whether I have handlers for wxEVT_COMMAND_TEXT_ENTER (I am using wxCrafter to set things up) and/or wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN, as long as I have one. Neither does it seem to matter whether I call event.Skip() after the call to search.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Showing selection in wxTextCtrl

Post by catalin »

Create the wxTextCtrl with wxTE_NOHIDESEL flag.
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Showing selection in wxTextCtrl

Post by Widgets »

That fixed it, thank you
I had experimented with this flag, but, after checking again now, I had set it for the wrong text control.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Post Reply