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
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Showing selection in wxTextCtrl

Post by Widgets » Mon Aug 17, 2020 4:11 pm

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 64-bit & Mint 10.2
MSVC Express 2010, 2015, 2017, 2019
wxWidgets 3.1.2
Fight SPAM - report it with wxSR

User avatar
doublemax
Moderator
Moderator
Posts: 15855
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Showing selection in wxTextCtrl

Post by doublemax » Mon Aug 17, 2020 5:29 pm

Check if it's just a redraw issue by calling a_pTextCtrl->Refresh() afterwards.
Use the source, Luke!

Widgets
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Showing selection in wxTextCtrl

Post by Widgets » Mon Aug 17, 2020 6:00 pm

I was already calling Refresh() and tried a few more calls in the calling function, but it does not help.
Environment: Win 10 64-bit & Mint 10.2
MSVC Express 2010, 2015, 2017, 2019
wxWidgets 3.1.2
Fight SPAM - report it with wxSR

User avatar
doublemax
Moderator
Moderator
Posts: 15855
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Showing selection in wxTextCtrl

Post by doublemax » Mon Aug 17, 2020 7:43 pm

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
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Showing selection in wxTextCtrl

Post by Widgets » Mon Aug 17, 2020 8:49 pm

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 64-bit & Mint 10.2
MSVC Express 2010, 2015, 2017, 2019
wxWidgets 3.1.2
Fight SPAM - report it with wxSR

catalin
Moderator
Moderator
Posts: 1598
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Showing selection in wxTextCtrl

Post by catalin » Tue Aug 18, 2020 11:53 am

Create the wxTextCtrl with wxTE_NOHIDESEL flag.

Widgets
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Showing selection in wxTextCtrl

Post by Widgets » Tue Aug 18, 2020 3:44 pm

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 64-bit & Mint 10.2
MSVC Express 2010, 2015, 2017, 2019
wxWidgets 3.1.2
Fight SPAM - report it with wxSR

Post Reply