哪个是MFC C ++中用于单选的Combobox的事件处理程序

时间:2019-05-09 05:14:32

标签: c++ visual-studio mfc

在选择特定组合框时启用或禁用某些文本框时遇到麻烦。

我尝试使用OnCbnSelChangeLvLayers()OnCbnDropdownLvLayers()。 第一个事件处理程序的问题是我必须单击相同的值两次,然后在组合框frezees中进行选择。第二个事件处理程序解决了冻结问题,但是我必须单击组合框的下拉菜单才能使事件发生。我还为combobox尝试了其他一些事件处理程序,但未成功。 m_d_layers是组合框选择(1,2,3,4)的字符串变量

DDX:

void CThermalToolDlg::DoDataExchange(CDataExchange* pDX)
{
    DDX_CBString(pDX, IDC_LV_LAYERS, m_d_lvlayers);
}

消息映射:

BEGIN_MESSAGE_MAP(CThermalToolDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_DROPDOWN(IDC_LV_LAYERS, &CThermalToolDlg::OnCbnDropdownLvLayers)
ON_CBN_DROPDOWN(IDC_HV_LAYERS, &CThermalToolDlg::OnCbnDropdownHvLayers)
ON_CBN_DROPDOWN(IDC_LVDUCTTYPE1, &CThermalToolDlg::OnCbnDropdownLvducttype1)
ON_CBN_DROPDOWN(IDC_LVDUCTTYPE2, &CThermalToolDlg::OnCbnDropdownLvducttype2)
ON_CBN_DROPDOWN(IDC_LVDUCTTYPE3, &CThermalToolDlg::OnCbnDropdownLvducttype3)
ON_CBN_DROPDOWN(IDC_HVDUCTTYPE1, &CThermalToolDlg::OnCbnDropdownHvducttype1)
ON_CBN_DROPDOWN(IDC_HVDUCTTYPE2, &CThermalToolDlg::OnCbnDropdownHvducttype2)
ON_CBN_DROPDOWN(IDC_HVDUCTTYPE3, &CThermalToolDlg::OnCbnDropdownHvducttype3)
ON_BN_CLICKED(IDCANCEL, &CThermalToolDlg::OnBnClickedCancel)
ON_BN_CLICKED(ID_CALCULATE, &CThermalToolDlg::OnCalculate)
//ON_CBN_EDITUPDATE(IDC_LV_LAYERS,&CThermalToolDlg::OnCbnEditupdateLvLayers)
//ON_CBN_SELENDOK(IDC_LV_LAYERS, &CThermalToolDlg::OnCbnSelendokLvLayers)

END_MESSAGE_MAP()

事件处理程序:

void CThermalToolDlg::OnCbnDropdownLvLayers()
{
    // TODO: Add your control notification handler code here
    UpdateData();

    if (m_d_lvlayers == "1")
    {
        GetDlgItem(IDC_LV2_CU)->EnableWindow(0);
        GetDlgItem(IDC_LV2_ICI)->EnableWindow(0);
            //and so on
    }
    else if (m_d_lvlayers == "2")
    {
        GetDlgItem(IDC_LV2_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV2_ICI)->EnableWindow(1);
            //and so on

    }
    else if (m_d_lvlayers == "3")
    {
        GetDlgItem(IDC_LV2_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV2_ICI)->EnableWindow(1);
            //and so on
    }
    else
    {
        GetDlgItem(IDC_LV2_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV2_ICI)->EnableWindow(1);
            //and so on
    }
}

我希望在不冻结组合框的情况下单击即可获得所需的启用/禁用。还有其他方法或更快的方法吗?

2 个答案:

答案 0 :(得分:0)

这似乎是下拉列表组合框和简单组合框(而非下拉列表)的错误。 DDX_CBStringDDX_CBStringExact没有响应ON_CBN_DROPDOWN检索正确的值。

您可以使用自己的代码来检索正确的字符串:

void CThermalToolDlg::OnCbnDropdownLvLayers() 
{ 
    //UpdateData();
    CComboBox *cb = (CComboBox*)GetDlgItem(IDC_LV_LAYERS);
    if(cb)
    {
        int sel = cb->GetCurSel();
        if(sel >= 0)
            cb->GetLBText(sel, m_d_lvlayers);
    }
    TRACE(L"%s\n", m_d_lvlayers.GetString());
}

出于调试目的,请注释除ON_CBN_DROPDOWN(IDC_LV_LAYERS...)以外的其他消息。在继续下一步之前,请确保您具有正确的字符串。

请勿像上一个问题一样打UpdateData(FALSE)。如果未检索到正确的字符串,这尤其可能导致问题。

答案 1 :(得分:0)

由于@BarmakShemirani,我使用了他的代码来检索正确的字符串,而不是在UpdateData()事件处理程序中检索ON_CBN_SELENDOK,现在它就像一个魅力

DDX:

void CThermalToolDlg::DoDataExchange(CDataExchange* pDX)
{
  DDX_CBString(pDX, IDC_LV_LAYERS, m_d_lvlayers);
}

消息映射:

BEGIN_MESSAGE_MAP(CThermalToolDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELENDOK(IDC_LV_LAYERS, &CThermalToolDlg::OnCbnSelendokLvLayers)
ON_BN_CLICKED(IDCANCEL, &CThermalToolDlg::OnBnClickedCancel)
ON_BN_CLICKED(ID_CALCULATE, &CThermalToolDlg::OnCalculate)
END_MESSAGE_MAP()

事件处理程序:

void CThermalToolDlg::OnCbnSelendokLvLayers()
{
    // TODO: Add your control notification handler code here
    CComboBox *cb = (CComboBox*)GetDlgItem(IDC_LV_LAYERS);
    if (cb)
    {
        int sel = cb->GetCurSel();
        if (sel >= 0)
            cb->GetLBText(sel, m_d_lvlayers);
    }
    TRACE(_T("[%s]\n"), m_d_lvlayers.GetString());

    /*UpdateData();*/

    if (m_d_lvlayers == "1")
    {
        GetDlgItem(IDC_LV2_CU)->EnableWindow(0);
        GetDlgItem(IDC_LV2_ICI)->EnableWindow(0);
        GetDlgItem(IDC_LV2_PC)->EnableWindow(0);
        GetDlgItem(IDC_LV2_RESIN)->EnableWindow(0);
        GetDlgItem(IDC_LV2_RESO)->EnableWindow(0);
        GetDlgItem(IDC_LV2_INST)->EnableWindow(0);
        GetDlgItem(IDC_LV3_CU)->EnableWindow(0);
        GetDlgItem(IDC_LV3_ICI)->EnableWindow(0);
        GetDlgItem(IDC_LV3_PC)->EnableWindow(0);
        GetDlgItem(IDC_LV3_RESIN)->EnableWindow(0);
        GetDlgItem(IDC_LV3_RESO)->EnableWindow(0);
        GetDlgItem(IDC_LV3_INST)->EnableWindow(0);
        GetDlgItem(IDC_LV4_CU)->EnableWindow(0);
        GetDlgItem(IDC_LV4_ICI)->EnableWindow(0);
        GetDlgItem(IDC_LV4_PC)->EnableWindow(0);
        GetDlgItem(IDC_LV4_RESIN)->EnableWindow(0);
        GetDlgItem(IDC_LV4_RESO)->EnableWindow(0);
        GetDlgItem(IDC_LV4_INST)->EnableWindow(0);
        GetDlgItem(IDC_LVDUCTTYPE1)->EnableWindow(0);
        GetDlgItem(IDC_LVDUCTTYPE2)->EnableWindow(0);
        GetDlgItem(IDC_LVDUCTTYPE3)->EnableWindow(0);
        GetDlgItem(IDC_LV_D_NO_12)->EnableWindow(0);
        GetDlgItem(IDC_LV_D_NO_23)->EnableWindow(0);
        GetDlgItem(IDC_LV_D_NO_34)->EnableWindow(0);
    }
    else if (m_d_lvlayers == "2")
    {
        GetDlgItem(IDC_LV2_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV2_ICI)->EnableWindow(1);
        GetDlgItem(IDC_LV2_PC)->EnableWindow(1);
        GetDlgItem(IDC_LV2_RESIN)->EnableWindow(1);
        GetDlgItem(IDC_LV2_RESO)->EnableWindow(1);
        GetDlgItem(IDC_LV2_INST)->EnableWindow(1);
        GetDlgItem(IDC_LV3_CU)->EnableWindow(0);
        GetDlgItem(IDC_LV3_ICI)->EnableWindow(0);
        GetDlgItem(IDC_LV3_PC)->EnableWindow(0);
        GetDlgItem(IDC_LV3_RESIN)->EnableWindow(0);
        GetDlgItem(IDC_LV3_RESO)->EnableWindow(0);
        GetDlgItem(IDC_LV3_INST)->EnableWindow(0);
        GetDlgItem(IDC_LV4_CU)->EnableWindow(0);
        GetDlgItem(IDC_LV4_ICI)->EnableWindow(0);
        GetDlgItem(IDC_LV4_PC)->EnableWindow(0);
        GetDlgItem(IDC_LV4_RESIN)->EnableWindow(0);
        GetDlgItem(IDC_LV4_RESO)->EnableWindow(0);
        GetDlgItem(IDC_LV4_INST)->EnableWindow(0);
        GetDlgItem(IDC_LVDUCTTYPE1)->EnableWindow(1);
        GetDlgItem(IDC_LVDUCTTYPE2)->EnableWindow(0);
        GetDlgItem(IDC_LVDUCTTYPE3)->EnableWindow(0);
        GetDlgItem(IDC_LV_D_NO_12)->EnableWindow(1);
        GetDlgItem(IDC_LV_D_NO_23)->EnableWindow(0);
        GetDlgItem(IDC_LV_D_NO_34)->EnableWindow(0);
    }
    else if (m_d_lvlayers == "3")
    {
        GetDlgItem(IDC_LV2_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV2_ICI)->EnableWindow(1);
        GetDlgItem(IDC_LV2_PC)->EnableWindow(1);
        GetDlgItem(IDC_LV2_RESIN)->EnableWindow(1);
        GetDlgItem(IDC_LV2_RESO)->EnableWindow(1);
        GetDlgItem(IDC_LV2_INST)->EnableWindow(1);
        GetDlgItem(IDC_LV3_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV3_ICI)->EnableWindow(1);
        GetDlgItem(IDC_LV3_PC)->EnableWindow(1);
        GetDlgItem(IDC_LV3_RESIN)->EnableWindow(1);
        GetDlgItem(IDC_LV3_RESO)->EnableWindow(1);
        GetDlgItem(IDC_LV3_INST)->EnableWindow(1);
        GetDlgItem(IDC_LV4_CU)->EnableWindow(0);
        GetDlgItem(IDC_LV4_ICI)->EnableWindow(0);
        GetDlgItem(IDC_LV4_PC)->EnableWindow(0);
        GetDlgItem(IDC_LV4_RESIN)->EnableWindow(0);
        GetDlgItem(IDC_LV4_RESO)->EnableWindow(0);
        GetDlgItem(IDC_LV4_INST)->EnableWindow(0);
        GetDlgItem(IDC_LVDUCTTYPE1)->EnableWindow(1);
        GetDlgItem(IDC_LVDUCTTYPE2)->EnableWindow(1);
        GetDlgItem(IDC_LVDUCTTYPE3)->EnableWindow(0);
        GetDlgItem(IDC_LV_D_NO_12)->EnableWindow(1);
        GetDlgItem(IDC_LV_D_NO_23)->EnableWindow(1);
        GetDlgItem(IDC_LV_D_NO_34)->EnableWindow(0);
    }
    else
    {
        GetDlgItem(IDC_LV2_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV2_ICI)->EnableWindow(1);
        GetDlgItem(IDC_LV2_PC)->EnableWindow(1);
        GetDlgItem(IDC_LV2_RESIN)->EnableWindow(1);
        GetDlgItem(IDC_LV2_RESO)->EnableWindow(1);
        GetDlgItem(IDC_LV2_INST)->EnableWindow(1);
        GetDlgItem(IDC_LV3_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV3_ICI)->EnableWindow(1);
        GetDlgItem(IDC_LV3_PC)->EnableWindow(1);
        GetDlgItem(IDC_LV3_RESIN)->EnableWindow(1);
        GetDlgItem(IDC_LV3_RESO)->EnableWindow(1);
        GetDlgItem(IDC_LV3_INST)->EnableWindow(1);
        GetDlgItem(IDC_LV4_CU)->EnableWindow(1);
        GetDlgItem(IDC_LV4_ICI)->EnableWindow(1);
        GetDlgItem(IDC_LV4_PC)->EnableWindow(1);
        GetDlgItem(IDC_LV4_RESIN)->EnableWindow(1);
        GetDlgItem(IDC_LV4_RESO)->EnableWindow(1);
        GetDlgItem(IDC_LV4_INST)->EnableWindow(1);
        GetDlgItem(IDC_LVDUCTTYPE1)->EnableWindow(1);
        GetDlgItem(IDC_LVDUCTTYPE2)->EnableWindow(1);
        GetDlgItem(IDC_LVDUCTTYPE3)->EnableWindow(1);
        GetDlgItem(IDC_LV_D_NO_12)->EnableWindow(1);
        GetDlgItem(IDC_LV_D_NO_23)->EnableWindow(1);
        GetDlgItem(IDC_LV_D_NO_34)->EnableWindow(1);
    }
}

谢谢大家!