Austin
Austin

Reputation: 37

how to add search feature to textctrl in wxwidget c++

how to add search feature to wxtextctrl in wxwidget c++?

what I want is to add a search bar that can search words in wxtextctrl. if a word has been found using a search bar, the word searched will be highlighted.

Upvotes: 0

Views: 574

Answers (1)

avariant
avariant

Reputation: 2300

I have implemented a search and highlight mechanism with wxWidgets, but it uses wxStyledTextCtrl, not wxTextCtrl (so I know it's not an exact answer for what you are looking for).

If you were in a position to change your wxTextCtrl to a wxStyledTextCtrl, you can do a Next and Previous function like this:

Next:

//Sets the current caret position as the start of the search
editor->SearchAnchor();
//flags can be things like wxSTC_FIND_MATCHCASE for case sensitive searching
int findpos = editor->SearchNext(flags, find_string);
if (findpos > 0)
{
    //search does not implicitly ensure your found location is visible
    editor->EnsureCaretVisible();
    //TODO: any other UI response to a valid find
}
else
{
    //TODO: any other UI response to no valid find
}

Previous is exactly the same except your replace SearchNext with SearchPrev

int findpos = editor->SearchPrev(flags, find_string);

Obviously, the alternative if you need to use wxTextCtrl is to manually search the string and set the selection directly using wxTextCtrl::SetSelection. This post on the wxForum might help with that: https://forums.wxwidgets.org/viewtopic.php?t=15917

Upvotes: 1

Related Questions