Click here to Skip to main content
15,886,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i created a dialog box while clicking on the button.but when i again clicked again dialog box is creating. i want to create it on the first click. when the button is clicked further clicks should be disabled.can u help me to do this?

What I have tried:

i created a dialog box while clicking on the button.but it is again and again creating dialog box on further click

[EDIT: Inserted code posted as comment]
C++
void CMyViewerDlg::OnBnClickedShow()
{
    m_DCM.Create(IDD_Tags, this);
    m_DCM.ShowWindow(SW_SHOW);
    BOOL isOn = true;
    if (isOn == false)
    {
        GetDlgItem(IDC_SHOW)->EnableWindow(TRUE);
    }
    else
    {
        GetDlgItem(IDC_SHOW)->EnableWindow(FALSE);
    }
}

this is the code. Iam a beginer in programming. tell me how to change the code.
Posted
Updated 12-Mar-17 17:14pm
v4
Comments
Jochen Arndt 10-Mar-17 3:11am    
I assume that it is a modeless dialog (otherwise the parent window would be blocked so that clicking the button again would not be possible).

The best answer to your question depends on how the dialog is created and how you detect that it is closed (if already detected).

So how do you create that dialog (just show the code of your button click handler).

You are creating a modeless dialog. In this case you must track the state of that dialog. Your existing code can be used as starting point:
void CMy3D_Med_ViewerDlg::OnBnClickedShowDicomTag()
{
    m_DCMTagDlg.Create(IDD_DICOM_Tags, this);
    m_DCMTagDlg.ShowWindow(SW_SHOW);
    GetDlgItem(IDC_SHOW_DICOM_TAG)->EnableWindow(FALSE);
}

By disabling the button it is grayed out and the handler is not called anymore when clicked again.

But when the dialog is closed you must now enable the button again. Do this by handling the WM_CLOSE message:
C++
void DCMTagDlg::OnClose()
{
    CMy3D_Med_ViewerDlg *p = (CMy3D_Med_ViewerDlg *)GetParent();
    p->GetDlgItem(IDC_SHOW_DICOM_TAG)->EnableWindow(TRUE);
    CDialog::OnClose();
}
Note that the above can be only used when the dialog is called only from CMy3D_Med_ViewerDlg.


There is also a different approach for this problem: The child dialog is never closed but only hidden:
C++
void DCMTagDlg::OnClose()
{
    ShowWindow(SW_HIDE);
}

This requires using a pointer to the dialog instead of an instance (just replace DCMTagDlg m_DCMTagDlg by DCMTagDlg *m_pDCMTagDlg and initialise the pointer with NULL in the constructor):
void CMy3D_Med_ViewerDlg::OnBnClickedShowDicomTag()
{
    if (m_pDCMTagDlg == NULL)
    {
        m_pDCMTagDlg = new DCMTagDlg();
        m_pDCMTagDlg->Create(IDD_DICOM_Tags, this);
    }
    m_pDCMTagDlg->ShowWindow(SW_SHOW);
}

Note that you now have to destroy the dialog when the parent window is closed:
C++
void CMy3D_Med_ViewerDlg::OnClose()
{
    if (m_pDCMTagDlg)
    {
        m_pDCMTagDlg->Destroy();
        delete m_pDCMTagDlg;
        m_pDCMTagDlg = NULL;
    }
    CDialog::OnClose();
}

When using this you may still disable the button. When not doing so, clicking on the button will just show the existing dialog.
 
Share this answer
 
v2
Just disable the button after first click, see CWnd::EnableWindow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900