-
August 30th, 2009, 03:12 PM
#1
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 :-)
-
August 30th, 2009, 05:39 PM
#2
Re: multiple buttons using the same OnBnClicked
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.
-
August 30th, 2009, 06:46 PM
#3
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.
-
August 30th, 2009, 07:05 PM
#4
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 );
// ...
}
-
August 31st, 2009, 06:55 AM
#5
Re: multiple buttons using the same OnBnClicked
works great - thanks very much
-
August 31st, 2009, 09:15 AM
#6
Re: multiple buttons using the same OnBnClicked
Originally Posted by MJBritton25
works great - thanks very much
Your welcome. Sure makes the code cleaner, doesn't it?
-
August 31st, 2009, 01:19 PM
#7
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.
prepare that the light at the end of your tunnel is just a freight train coming your way - metallica
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|