在 WxWidgets 文本控件中按 Enter 时防止换行

发布于 2024-11-27 00:19:37 字数 432 浏览 1 评论 0原文

我正在使用 WxWidgets 编写一个应用程序,并且遇到了多行文本控件 (wxTextControl) 的问题。它是聊天窗口中的输入字段,并且需要是多行,以防用户输入需要换行的较长消息。我希望发送事件(例如,按下“发送”按钮时执行的操作)在用户按控件中的 Enter 键时执行。我使用 wxEVT_COMMAND_TEXT_ENTER 事件进行此工作,并启用了 wxTE_PROCESS_ENTER 样式。然而,问题是,当发送命令确实被执行时,一个新的行字符 \n 也会被附加到文本中(这种情况发生在发送命令之后和我清除文本之后,导致除了一个新的字段之外的一个空字段)线)。我试图通过捕获 char 和 key down 事件来避免这种情况,但由于某种原因它们没有触发。

我只是想完全避免显示新行。有人有什么建议吗?

我正在 Windows 上进行开发,但该应用程序旨在在 WxWidgets 支持的所有平台上运行。

I am writing an application with WxWidgets, and have run into an issue with a multiline text control (wxTextControl). It is the input field in a chat window, and it needs to be multi line in case the user types a longer message that needs to wrap. I want the send event, e.g. the action that is taken when the Send button is pressed, to be executed when the user presses enter in the control. I have this working using the wxEVT_COMMAND_TEXT_ENTER event, with the wxTE_PROCESS_ENTER style enabled. However, the problem is that while the send command does get executed, a new line character \n is also appended to the text (this happens after the send command and after I have cleared the text, resulting in an empty field except for a new line). I tried to avoid this by trapping both char and key down events, but for some reason they are not firing.

I simply want to avoid the new line being shown at all. Does anyone have any tips?

I am developing on Windows, but the application is meant to run on all platforms supported by WxWidgets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2024-12-04 00:19:37

您可以跟踪 wxEVT_COMMAND_TEXT_UPDATED 并在输入换行符时清除编辑器(假设您在按下 Enter 时也清除编辑器)。

BEGIN_EVENT_TABLE(Test,wxFrame)
    EVT_TEXT_ENTER(EditorID, Test::OnEnter)
    EVT_TEXT(EditorId, Test::OnText)
END_EVENT_TABLE()

void Test::OnEnter(wxCommandEvent&)
{
    Send(editor->GetValue());
    editor->Clear();
}

void Test::OnText(wxCommandEvent& event)
{
    if (event.GetString() == wxT("\n")) { //seems to work, not much info in documentation?
        editor->Clear();
    }
}

You might track wxEVT_COMMAND_TEXT_UPDATED and clear the editor when a new-line is entered (it assumes you are also clearing the editor when Enter is pressed).

BEGIN_EVENT_TABLE(Test,wxFrame)
    EVT_TEXT_ENTER(EditorID, Test::OnEnter)
    EVT_TEXT(EditorId, Test::OnText)
END_EVENT_TABLE()

void Test::OnEnter(wxCommandEvent&)
{
    Send(editor->GetValue());
    editor->Clear();
}

void Test::OnText(wxCommandEvent& event)
{
    if (event.GetString() == wxT("\n")) { //seems to work, not much info in documentation?
        editor->Clear();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文