当用户从 CComboBox 中选择项目时捕获

2022-01-12 00:00:00 mfc ccombobox

这是最基本的.

我想捕捉用户从 CComboBox(实际上是 CComboBox 的子类)中选择项目的时间.

I want to catch when the user selects an item from a CComboBox (actually, a subclass of CComboBox).

尝试了很多 OnCblSelChangeOnCommand 的组合.猜猜我还没有击中正确的组合(没有双关语).

Tried lots of combinations of OnCblSelChange, OnCommand. Guess I haven't hit the right combo yet (no pun intended).

操作系统是 Vista,但我强制使用 XP 样式的对话框(应该没关系吧?)

OS is Vista but I'm forcing an XP-style dialog (That shouldn't matter, should it?)

我能够捕获派生自 CEditCFileDialog 的类的事件.

I'm able to catch events for classes derived from CEdit and CFileDialog.

到此为止,我已经束手无策了.任何帮助都将不胜感激.

I am at my wits end here. Any assistance would be ever-so appreciated.

当然,任何源代码都会比以往任何时候都更受欢迎.

Any source code would, of course, be more than ever-so appreciated.

推荐答案

不幸的是,似乎所有用于更改组合框的消息(甚至 SELEND_OK)都是在之前发送的文本实际上已经改变,所以 DoDataExchange 将在 CComboBox 中为您提供以前的文本.我使用了以下方法,按照 MSDN 的建议:

Unfortunately, it seems that all messages (even SELEND_OK) for combo box changing are sent before the text has actually changed, so DoDataExchange will give you the previous text in the CComboBox. I have used the following method, as suggested by MSDN:

void MyDialog::DoDataExchange(CDataExchange* pDX)
{
    DDX_Text(pDX, IDC_COMBO_LOCATION, m_sLocation);
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(MyDialog, CDialog)
    ON_CBN_SELENDOK(IDC_COMBO1, &MyDialog::OnComboChanged)
    ON_CBN_EDITUPDATE(IDC_COMBO1, &MyDialog::OnComboEdited) // This one updates immediately
END_MESSAGE_MAP()

...

void MyDialog::OnComboChanged()
{
    m_myCombo.GetLBText(m_myCombo.GetCurSel(), m_sSomeString);
}

void MyDialog::OnComboEdited()
{
    UpdateData();
}

它似乎工作得很好.

相关文章