Any Control with huge buttons.
-
Hi,, In my application I have to use large number of buttons(AROUND 112), all buttons will call the same function " DoMyDuty(int a) ", variable 'a' is different for all buttons. here is sample code for better understand void MyClass::OnB1() { int a; a=1; -----Only this value changed DoMyDuty(a); } void MyClass::OnB2() { int a; a=15; -------Only this value changed DoMyDuty(a); } ..... . . . . ..... void MyClass::OnB112() { int a; a=30; -------Only this value changed DoMyDuty(a); } The CODE SEEMS TO BIG but simple to do... iS THERE ANY ALTERNATIVE LOGIC TO DO THIS......?????? Any other control behaves like this???? thank u. verma
-
Hi,, In my application I have to use large number of buttons(AROUND 112), all buttons will call the same function " DoMyDuty(int a) ", variable 'a' is different for all buttons. here is sample code for better understand void MyClass::OnB1() { int a; a=1; -----Only this value changed DoMyDuty(a); } void MyClass::OnB2() { int a; a=15; -------Only this value changed DoMyDuty(a); } ..... . . . . ..... void MyClass::OnB112() { int a; a=30; -------Only this value changed DoMyDuty(a); } The CODE SEEMS TO BIG but simple to do... iS THERE ANY ALTERNATIVE LOGIC TO DO THIS......?????? Any other control behaves like this???? thank u. verma
If you are using MFC, you can have a single handler respond to a group of controls using
ON_CONTROL_RANGE()
[^]. This would let you have a single handler, like this:// in your message map:
ON_CONTROL_RANGE(BN_CLICKED,IDC_B1,IDC_B112,OnB)
// handler:
void MyClass::OnB(UINT ID)
{
static struct {
UINT ID;
int a;
} table[] = {
{ IDC_B1, 1 },
{ IDC_B2, 15 },
//...
{ IDC_B112, 30 }
};
for (int i = 0; i < (sizeof(table) / sizeof(table[0])); i++) {
if (table[i].ID == ID) {
DoMyDuty(table[i].a);
break;
}
}
}The handler is called with the resource ID of the button that was clicked. It then uses a simple table to look up the value of 'a' to use for the call to the
DoMyDuty()
function.Software Zen:
delete this;
Fold With Us![^] -
If you are using MFC, you can have a single handler respond to a group of controls using
ON_CONTROL_RANGE()
[^]. This would let you have a single handler, like this:// in your message map:
ON_CONTROL_RANGE(BN_CLICKED,IDC_B1,IDC_B112,OnB)
// handler:
void MyClass::OnB(UINT ID)
{
static struct {
UINT ID;
int a;
} table[] = {
{ IDC_B1, 1 },
{ IDC_B2, 15 },
//...
{ IDC_B112, 30 }
};
for (int i = 0; i < (sizeof(table) / sizeof(table[0])); i++) {
if (table[i].ID == ID) {
DoMyDuty(table[i].a);
break;
}
}
}The handler is called with the resource ID of the button that was clicked. It then uses a simple table to look up the value of 'a' to use for the call to the
DoMyDuty()
function.Software Zen:
delete this;
Fold With Us![^]Another similar approach:
// in your message map:
ON_CONTROL_RANGE(BN_CLICKED,IDC_B1,IDC_B112,OnB)// handler:
void MyClass::OnB(UINT ID)
{
DoMyDuty(ID - IDC_B1 + 1);
}Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
Another similar approach:
// in your message map:
ON_CONTROL_RANGE(BN_CLICKED,IDC_B1,IDC_B112,OnB)// handler:
void MyClass::OnB(UINT ID)
{
DoMyDuty(ID - IDC_B1 + 1);
}Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
I don't think that works. The value of 'a' in his original post didn't vary predictably according to the button pressed, so you needed a lookup table approach.
Software Zen:
delete this;
Fold With Us![^] -
I don't think that works. The value of 'a' in his original post didn't vary predictably according to the button pressed, so you needed a lookup table approach.
Software Zen:
delete this;
Fold With Us![^]Thanks gary!!!!! It works fine.....