简体   繁体   English

Windows 7上的SetCurSel之后的MFC CComboBox OnKillFocus / ShowDropDown

[英]MFC CComboBox OnKillFocus/ShowDropDown after SetCurSel on Windows 7

As you may know a CComboBox (DropDownList style) only selects properly by typing if you don't let it autosort it's content. 如您所知,CComboBox(DropDownList样式)只有在不让它自动对内容进行自动排序时才通过键入来正确选择。 Here an example of what i mean: You type A and then B. If it is not on autosort it will first select the next entry beginning with A and then the next entry beginning with B. If it is sorted it will select the next entry with A and then the next beginning with AB. 这是我的意思的示例:键入A,然后键入B。如果它不在自动排序中,它将首先选择以A开头的下一个条目,然后选择以B开头的下一个条目。如果已排序,则将选择下一个条目。 A,然后下一个AB。

I have a class that inherits from CComboBox and it's content is not sorted by the built in sort mechanism. 我有一个从CComboBox继承的类,它的内容不是由内置的排序机制排序的。 I wanted a proper select-by-typing behaviour, so i implemented that myself. 我想要一个正确的按键入选择行为,所以我自己实现了这一点。 I select the entries by using SetCurSel and then sending a CBN_SELCHANGE message to the parent window. 我使用SetCurSel选择条目,然后将CBN_SELCHANGE消息发送到父窗口。

On Windows XP this worked perfectly fine. 在Windows XP上,此功能运行良好。 We are using Windows 7 now and following problem is occuring now: If i use the mouse to open the dropdown list, select an entry by typing it and then use Tab to jump to the next control, the value that was selected at the beginning is automatically reselected. 我们现在正在使用Windows 7,并且现在出现以下问题:如果我使用鼠标打开下拉列表,请通过键入选择一个条目,然后使用Tab跳到下一个控件,在开头选择的值是自动重新选择。

If i overwrite OnKillFocus and do the following: 如果我覆盖OnKillFocus并执行以下操作:

int index = GetCurSel();
CComboBox::OnKillFocus();
SetCurSel(index);

it works generally, but for a moment the old selected value will still flash up which is ugly. 它通常可以正常工作,但是片刻之后旧选择的值仍然会闪烁,这很丑陋。

If i don't call CComboBox::OnKillFocus() the correct entry remains selected, but the combo box remains dropped down. 如果我不调用CComboBox :: OnKillFocus(),则正确的条目将保持选中状态,但组合框仍保持下拉状态。

So my next try was to call SetDropDown(FALSE) manually (and nothing else in OnKillFocus()), but then the problem reappears. 因此,我的下一个尝试是手动调用SetDropDown(FALSE)(OnKillFocus()中没有其他功能),但是问题再次出现。

So my guess is that if you open the dropdown list the current selection is internally stored. 所以我的猜测是,如果您打开下拉列表,则当前选择将在内部存储。 Calling SetCurSel does not update this internal value. 调用SetCurSel不会更新此内部值。 And if you tab out that internal value is reselected again. 并且,如果您将其标签出去,则会再次选择内部值。

Any ideas how i can fix this problem? 有什么想法我可以解决这个问题吗?

Only unverified ideas: 仅未经验证的想法:

  • Override CB_SHOWDROPDOWN processing. 覆盖CB_SHOWDROPDOWN处理。 If dropdown list is closed, probably call SetCurSel for a stored item. 如果关闭了下拉列表,则可能为存储的项目调用SetCurSel。

  • Override CB_SETCURSEL processing, Add flag indicating whether to process the CB_SETCURSEL message. 覆盖CB_SETCURSEL处理,添加指示是否要处理CB_SETCURSEL消息的标志。 Turn off the flag on the time of call CComboBox::OnKillFocus() in your code sample. 关闭代码示例中调用CComboBox :: OnKillFocus()时的标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM