multiple buttons using the same OnBnClicked
I've got many (many) buttons on screen and I'd like to write just one OnClick event for all of them.
The question is for my Onclick function, is it possible to tell which button has called the onclick ?
I'd like to achieve the following:
void CTestDlg::OnBnClicked()
{
int i = **Determine which button was clicked, numbered one to 32**
ProcessButton(i);
}
Before anyone comments on "why have I got 32 buttons on screen" its because its realtime, and yes it does look awful and the client has asked for it to look so bad specifically :-)
Re: multiple buttons using the same OnBnClicked
Quote:
Originally Posted by
MJBritton25
Before anyone comments on "why have I got 32 buttons on screen" its because its realtime, and yes it does look awful and the client has asked for it to look so bad specifically :-)
LOL. Check out the ON_COMMAND_RANGE macro DDX entry.
Re: multiple buttons using the same OnBnClicked
I don't know of a way to determine which button was clicked the way you're asking. You could
Set up a separate event handler for each button that would call a common function and pass in the button ID
You could override PreTranslateMessage and intercept the button clicks there.
I would assume the last button clicked would have the focus. You could check for the button with the focus to determine which one was clicked.
Re: multiple buttons using the same OnBnClicked
Sorry, I said ON_COMMAND_RANGE, it's ON_CONTROL_RANGE.
Code:
ON_CONTROL_RANGE(BN_CLICKED, IDC_BUTTON1, IDC_BUTTON10, OnButtonClicked)
Code:
void CMyDialog::OnButtonClicked( UINT nID )
{
int nButton = nID - IDC_BUTTON1;
ASSERT( nButton >= 0 && nButton < 10 );
// ...
}
Re: multiple buttons using the same OnBnClicked
works great - thanks very much
Re: multiple buttons using the same OnBnClicked
Quote:
Originally Posted by
MJBritton25
works great - thanks very much
Your welcome. Sure makes the code cleaner, doesn't it? :)
Re: multiple buttons using the same OnBnClicked
and make sure that the ID's are consecutive - sometimes they tend not too, esp. when generated by the resource wizard.