Click to See Complete Forum and Search --> : How do I put 30 buttons in a Dialogbox with all button presses handled by the same function


Jason Watson
April 29th, 1999, 07:39 PM
I want to put 30 buttons on a Dialog box. I need to know how to create an array of CButtons so that I can access all the controls with the same member variable. I also need to be able to process button presses using the same function

Similar to the way VB does it.

Centurion Software Australia

April 30th, 1999, 12:50 AM
Hi!
1. Put in the 30 buttons on to your dialog box ensuring in the process that the IDs are contiguous.
2. Add the following message map entry in your dialog
ON_CONTROL_RANGE(BN_CLICKED,IDC_BUTTON1,IDC_BUTTON30,HandlerForBtnPress)
3. Add the prototype and the implementation of the BN_CLICKED Handler
//Prototype in the header file
afx_msg void HandlerForBtnPress(int nID);
//Sample implementation in the cpp file
void CTrialDialog::HandlerForBtnPress(int nID)
{
int nButton = nID - IDC_BUTTON1;
CString strMessage;
strMessage.Format("Button %d Clicked !!!",nButton);
AfxMessageBox(strMessage);
}
This ensures that the same function handles all button presses.

Incase you need to create an array of CButtons, here is the code for it :
//Header File
const int nradio_buttons = 30; //Number of buttons
//Class Definition
CButton* m_ButtonArray[nradio_buttons];
//OnInitDialog
for(int i=0;i<30;i++)
{
m_ButtonArray[i] = (CButton*)GetDlgItem(IDC_BUTTON1+i);
}
//Make use of m_ButtonArray[i] for further processing as U need.

Regards,
Jose Wilson
Software Engineer
Aditi Technologies
Blore - 80
Email :josew@aditi.com

May 7th, 1999, 04:19 PM
Try the following
ON_CONTROL_RANGE( BN_CLICKED,ID_BUTTON_01,ID_BUTTON30,ButtonCatcher )

MyClass::ButtonCatcher()
{
// TODO: add logic (maybe a switch statement)
}