以编程方式更改组合框

发布于 2024-07-04 08:53:57 字数 258 浏览 8 评论 0原文

我需要使用新值更新组合框,以便更改其中的反射文本。 最简洁的方法是在组合框初始化并显示一条消息之后。

因此,我尝试为包含组合框的 hwnd 创建一个 postmessage

因此,如果我想向它发送一条消息,将当前选定的项目更改为第 n 个项目,postmessage 会是什么样子?

我猜测它会涉及 ON_CBN_SELCHANGE,但我无法让它正常工作。

I need to update a combobox with a new value so it changes the reflected text in it. The cleanest way to do this is after the comboboxhas been initialised and with a message.

So I am trying to craft a postmessage to the hwnd that contains the combobox.

So if I want to send a message to it, changing the currently selected item to the nth item, what would the postmessage look like?

I am guessing that it would involve ON_CBN_SELCHANGE, but I can't get it to work right.

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

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

发布评论

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

评论(4

莫言歌 2024-07-11 08:53:57

如果您想更改标题 - 这是组合框关闭时显示的行,那么您可以执行以下操作:

m_ComboBox.DeleteString(0); // 首先删除之前的内容(如果有),0 = 可视字符串
m_ComboBox.AddString(_T("你好"));

将其放入 fx 中。 在 OnCloseupCombo 中 - 当事件关闭时,会触发下拉框

ON_CBN_CLOSEUP(IDC_COMBO1, OnCloseupCombo)

此更改是一个新字符串,而不是已分配的组合框元素的选择

if you fx want to change the title - which is the line shown when combobox is closed, then you can do following:

m_ComboBox.DeleteString(0); // first delete previous if any, 0 = visual string
m_ComboBox.AddString(_T("Hello there"));

put this in fx. in OnCloseupCombo - when event close a dropdownbox fires

ON_CBN_CLOSEUP(IDC_COMBO1, OnCloseupCombo)

This change is a new string not a selection of already assigned combobox elements

错爱 2024-07-11 08:53:57

您需要 ComboBox_SetCurSel

ComboBox_SetCurSel(hWndCombo, n);

或者如果它是 MFC您可能可以使用 CComboBox 控件:

m_combo.SetCurSel(2);

我想如果您手动执行此操作,您还需要 SendMessage 而不是 PostMessage。 CBN_SELCHANGE 是当选择更改时控件发送回给您的通知。

最后,您可能想向此问题添加 c++ 标签。

You want ComboBox_SetCurSel:

ComboBox_SetCurSel(hWndCombo, n);

or if it's an MFC CComboBox control you can probably do:

m_combo.SetCurSel(2);

I would imagine if you're doing it manually you would also want SendMessage rather than PostMessage. CBN_SELCHANGE is the notification that the control sends back to you when the selection is changed.

Finally, you might want to add the c++ tag to this question.

素手挽清风 2024-07-11 08:53:57

可能出现的问题是选择在选择更改消息处理程序内发生更改,这会导致另一个选择更改消息。

解决这个不需要的反馈循环的一种方法是向选择更改消息处理程序添加一个哨兵,如下所示:

void onSelectChangeHandler(HWND hwnd)
{
  static bool fInsideSelectChange = 0;

  //-- ignore the change message if this function generated it
  if (fInsideSelectChange == 0)
  {
    //-- turn on the sentinel
    fInsideSelectChange = 1;

    //-- make the selection changes as required
    .....

    //-- we are done so turn off the sentinel
    fInsideSelectChange = 0;
  }
}

What might be going wrong is the selection is being changed inside the selection change message handler, which result in another selection change message.

One way to get around this unwanted feedback loop is to add a sentinel to the select change message handler as shown below:

void onSelectChangeHandler(HWND hwnd)
{
  static bool fInsideSelectChange = 0;

  //-- ignore the change message if this function generated it
  if (fInsideSelectChange == 0)
  {
    //-- turn on the sentinel
    fInsideSelectChange = 1;

    //-- make the selection changes as required
    .....

    //-- we are done so turn off the sentinel
    fInsideSelectChange = 0;
  }
}
榆西 2024-07-11 08:53:57

一个简洁的版本:

const int index = 0;
m_comboBox.PostMessage(CBN_SELCHANGE, index);

A concise version:

const int index = 0;
m_comboBox.PostMessage(CBN_SELCHANGE, index);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文