【问题标题】:how to add search feature to textctrl in wxwidget c++如何在 wxwidget c++ 中向 textctrl 添加搜索功能
【发布时间】:2019-02-21 02:29:34
【问题描述】:

如何在 wxwidget c++ 中为 wxtextctrl 添加搜索功能?

我想要的是添加一个可以在wxtextctrl中搜索单词的搜索栏。如果使用搜索栏找到了单词,则搜索到的单词将突出显示。

【问题讨论】:

  • 也许你想要wxSearchCtrl
  • 是的,但我不知道如何将它与 wxtextctrl 集成。
  • 我明白你的意思。您必须在文本中手动定位搜索词,然后使用SetStyle。该组件不直接提供您想要的突出显示行为。不是想成为 LMGTFY 洞,但我找到了this on the wxWidgets forum

标签: c++ wxwidgets


【解决方案1】:

我已经使用 wxWidgets 实现了搜索和突出显示机制,但它使用 wxStyledTextCtrl,而不是 wxTextCtrl(所以我知道这不是您要查找的确切答案)。

如果您能够将您的 wxTextCtrl 更改为 wxStyledTextCtrl,您可以像这样执行 Next 和 Previous 函数:

下一步:

//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
}

除了将SearchNext 替换为SearchPrev 之外,之前的版本完全相同

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

显然,如果您需要使用 wxTextCtrl,另一种方法是手动搜索字符串并直接使用 wxTextCtrl::SetSelection 设置选择。 wxForum 上的这篇文章可能会对此有所帮助:https://forums.wxwidgets.org/viewtopic.php?t=15917

【讨论】:

  • 请注意,使用 wxStyledTextCtrl 确实需要链接 wxScintilla 库,因为它是基于它构建的。
猜你喜欢
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多