CComboBox下拉时未选择CurSel [英] CComboBox not selecting CurSel when dropped down

查看:252
本文介绍了CComboBox下拉时未选择CurSel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对话框中有一个按字母顺序排序的组合框。此组合包含多个字符串,但有些字符串在不同情况下重复。即我们有一个开和开,一个关和一个关。这似乎是多余的,但是有一个原因,尽管现在这并不重要。

I have an alphabetically sorted combobox in a dialog. This combo contains multiple strings, but some are duplicated with different cases. i.e. we have an 'On' and an 'ON', an 'Off' and an 'OFF'. This may seem redundant but there is a reason, although this is not important right now.

重复项显然在列表中一个接一个地出现,首字母大写。即:

The duplicates obviously appear one after the other in the list, with the capitalized strings first. i.e.:


OFF

OFF

Off

ON

On

当用户选择 On(小写)时,正确的索引设置为 CurSel ,并显示正确的字符串。但是,当我单击组合框的箭头以下拉列表时,它没有突出显示 CurSel ,而是突出显示了大写的字符串。参见下面的图像。

When the user selects the 'On' (lower case), the correct index is set as CurSel and the correct string is displayed. However, when I click on the arrow of the combobox to drop down the list, it does not highlight the CurSel, but the one previous to it, the capitalized string. See images below.

在下拉列表中选择了此选项:

This is was is selected in the dropdown:

这是在扩展下拉列表时在组合框中选择的内容。

This is what is selected in the combobox when expanding the dropdown.

我捕获了 ON_CBN_DROPDOWN 消息,并检查了cursel值,它与我期望的一样。

I have captured the ON_CBN_DROPDOWN message, and checked the cursel value and it is as I expected.

我也已经将此组合框子类化了,以便我可以区分大小写的方式在此列表中搜索字符串,因为我知道它没有正常实现,所以可能是造成我问题的原因。

I have also already subclassed this combobox so that I can search for strings in this list in a case-sensitive way, as I know its not implemented normally, so it may be what is causing my issue.

但是我不明白为什么在此阶段字符串会覆盖cursel值? CurSel 值不应该用来选择相关项目吗?

But I don't understand why the string would be overriding the cursel value at this stage? Should the CurSel value not be the one used to select the relevant item?

任何有关如何解决此问题的想法都将是极大的

Any ideas on how I can fix this would be greatly appreciated.

编辑:
我试图通过覆盖以下内容捕获 CBN_DROPDOWN 消息 OnWndMsg 。出现此消息时,我将获得当前选定的项目(正确的项目),然后再下拉菜单。然后,我放下菜单,并调用 SetCurSel 到我之前检索的内容。

I have tried to capture the CBN_DROPDOWN message by overwriting the OnWndMsg. When this message occurs, I get the currently selected item (which is the correct item) before dropping down the menu. I then drop the menu, and call SetCurSel to what I retrieved before.

BOOL CMyComboBox::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam,    LRESULT *pResult)
{
    if(message == CBN_DROPDOWN)
    {
        int nCurSel = GetCurSel();
        if(nCurSel != CB_ERR)
        {
            ShowDropDown();
            SetCurSel(nCurSel);
            return TRUE;
        }

    }
    return CComboBox::OnWndMsg(message, wParam, lParam, pResult);
}

这种种类有效,但是当我取消焦点或单击再次单击下拉箭头以隐藏该下拉菜单,文本框中将显示错误的项目。这是一种有效的方法,还是我完全不在这里?
下拉列表折叠时发送什么消息?

This kind of works but when I kill focus, or click on the dropdown arrow again to hide the dropdown, the wrong item is displayed in the text box. Is this a valid method, or am I completely off base here? What message is sent when the drop down is collapsed?

编辑2:
我已经实现了区分大小写的组合框,一个href = http://www.codeproject.com/Articles/1363/Case-sensitive-ComboBox rel = nofollow noreferrer title = code project>代码项目,效果很好。

推荐答案

进一步我的评论。我想您会发现,当内部索引为下拉样式时,内部机制会使用 SelectString 来设置索引。

Further to my comment. I think you will find that the internal mechanics is using SelectString to set the index when it is a dropdown style.

副作用是它可能不会从列表中为您选择正确的条目。因此,鉴于组合内容的性质,请尝试以下操作:

The side effect is that it may not pick the right entry for you from the list. Therefore, given the nature of the content in your combo, please try this:

int iIndex = m_cbData.FindStringExact(-1, "On");
m_cbData.SetCurSel(iIndex);

int iIndex = m_cbData.FindStringExact(-1, "OFF");
m_cbData.SetCurSel(iIndex);

不过,请注意 FindStringExact 说,搜索不区分大小写。但是 SelectString (默认行为)更糟。

However, be warned, the document for FindStringExact says the search is not case sensitive. But SelectString (default behaviour) is even worse.

可以解决所有这些问题的替代方法是使用 SetWindowText 并以此方式进行。这样,列表框组件中的内容并不重要。例如:

An alternative, which may resolve all of this, is to use SetWindowText and do it that way. This way, it does not matter what is in the listbox component. Eg:

m_cbData.SetWindowText("On");
m_cbData.SetWindowText("ON");

并通过映射到字符串或直接使用 GetWindowText获得变量的值

And get the value for the variable by either mapping to a string, or directly using GetWindowText.

更新:已经有人完成了工作!这是一个区分大小写的ComboBox类:

UPDATE: Someone has done the work already! Here is a Case Sensitive ComboBox class:

http://www.codeproject.com/Articles/1363/Case-sensitive-ComboBox

这篇关于CComboBox下拉时未选择CurSel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆