Mike Mike - 1 month ago 10
C++ Question

Pushbutton has to be clicked twice to trigger c++

I am defining a PrintBox Dialog that contains two buttons PRINTER1 and PRINTER2 in my resource file .rc as follows:

IDD_PRINTBOX DIALOGEX 0,0,211,55
STYLE_DS_SETFONT | DS_MODALFRAME | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Choose Printer"
FONT 8, "MS Sans Serif",0,0,0x0
BEGIN
PUSHBUTTON "PRINTER1",IDC_PRINTER1,7,7,50,14
PUSHBUTTON "PRINTER2",IDC_PRINTER2,152,7,49,14
END


and in the classes of the buttons, I have .h file and .cpp file
.h file

#ifndef PRINTCHOOSEDLG_H
#define PRINTCHOOSEDLG_H
#include <string>
#pragma once



class CPrintChooseDlg : public CDialog
{


public:

CPrintChooseDlg(CWnd* pParent = NULL);
enum { IDD = IDD_PRINTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
afx_msg void OnPrinter1();
afx_msg void OnPrinter2();

DECLARE_MESSAGE_MAP()
};


#endif


.cpp file

#include "stdafx.h"
#include "Tungsten.h"
#include "PrintChooseDlg.h"


CPrintChooseDlg::CPrintChooseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPrintChooseDlg::IDD, pParent)
{

}


void CPrintChooseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);

}


BEGIN_MESSAGE_MAP(CPrintChooseDlg, CDialog)
ON_BN_CLICKED(IDC_PRINTER1,OnPrinter1)
ON_BN_CLICKED(IDC_PRINTER2,OnPrinter2)
END_MESSAGE_MAP()



// PrintChoose message handlers

void CPrintChooseDlg::OnPrinter1()
{

CDialog::OnOk();
}
void CPrintChooseDlg::OnPrinter2()
{

CDialog::OnOk();
}


*The problem is that the button has to be clicked twice in order to fire the event.

*What I tried is to change the definition of PUSHBUTTON and define the header differently in the.h file. I changed also DIALOGEX to DIALOG, but the same problem exist. How can it be clicked only once to fire the event?
Thanks in Advance

Answer

use DoModal() only once.

instead of using if you can do it in switch case

switch(alg.DoModal())
{
    case IDC_PRINTER1: break;
    case IDC_PRINTER2: break;
    ...
}
Comments