Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me reduce redundant functions in MFC 2

Status
Not open for further replies.

MinnisotaFreezing

Programmer
Jun 21, 2001
120
KR
Hey all, I'm sure most peoples minds are still half focused on the east cost, so hopefully it will only take half your attention to solve my problem.

In a dialog box I have 10 buttons IDC_SHIP_DETAIL1 to IDC_SHIP_DETAIL10. I then have 10 Message Maps
ON_BN_CLICKED(IDC_SHIP_DETAIL1, OnShipDetail1)
..
ON_BN_CLICKED(IDC_SHIP_DETAIL10, OnShipDetail10)
so far so good. Now I have 10 functions:
void OnShipDetail1()
{
LoadInfo(1);
}
void OnShipDetail2()
{
LoadInfo(2);
}
..
void OnShipDetail10()
{
LoadInfo(10);
}

I feel those 10 functions are redundant. I would like 1 function
void OnShipDetail(int i)
{
LoadInfo(i);
}
Where the i is the index of the button that called it. I could then even call LoadInfo directly.

I tried changing my message map to
ON_BN_CLICKED(IDC_SHIP_DETAIL1, OnShipDetail(1))
and the appropriate OnShipDetail prototypes and functions. but I got an "illegal call of non-static member function" error on the ON_BN_CLICKED line.

Does anyone know how I can pass info to a message map function, or know of any other way I can identifiy which button was pushed other than having 10 seperate functions?

Any help would, of course, be greatly appriciated.

CJB
 
It's not the prettiest solution, but would save you some code length - use the OnShipDetailX() functions to call the real function ShipDetail(int i).
 
I'm sorry StukA, I don't quite follow you. Your solution would still require 10 message maps and 10 corresponding functions? Like so:
Code:
	ON_BN_CLICKED(IDC_SHIP_DETAIL1, OnShipDetail1)
	ON_BN_CLICKED(IDC_SHIP_DETAIL2, OnShipDetail2)
	ON_BN_CLICKED(IDC_SHIP_DETAIL3, OnShipDetail3)
	ON_BN_CLICKED(IDC_SHIP_DETAIL4, OnShipDetail4)
	ON_BN_CLICKED(IDC_SHIP_DETAIL5, OnShipDetail5)
	ON_BN_CLICKED(IDC_SHIP_DETAIL6, OnShipDetail6)
	ON_BN_CLICKED(IDC_SHIP_DETAIL7, OnShipDetail7)
	ON_BN_CLICKED(IDC_SHIP_DETAIL8, OnShipDetail8)
	ON_BN_CLICKED(IDC_SHIP_DETAIL9, OnShipDetail9)
	ON_BN_CLICKED(IDC_SHIP_DETAIL10, OnShipDetail10)

then

void PurchaseOrder::OnShipDetail1()
{
	ShipDetail(1);
}
void PurchaseOrder::OnShipDetail2()
{
	ShipDetail(2);
}
void PurchaseOrder::OnShipDetail3()
{
	ShipDetail(3);
}
void PurchaseOrder::OnShipDetail4()
{
	ShipDetail(4);
}
void PurchaseOrder::OnShipDetail5()
{
	ShipDetail(5);
}
void PurchaseOrder::OnShipDetail6()
{
	ShipDetail(6);
}
void PurchaseOrder::OnShipDetail7()
{
	ShipDetail(7);
}
void PurchaseOrder::OnShipDetail8()
{
	ShipDetail(8);
}
void PurchaseOrder::OnShipDetail9()
{
	ShipDetail(9);
}
void PurchaseOrder::OnShipDetail10()
{
	ShipDetail(10);
}

and finally

void PurchaseOrder::ShipDetail(int i)
{
     lParam = i;
      pDlg->->SendMessage(WM_LOAD, wParam, lParam)
}

I did that, and it works, but it gets kind of messy, especially since I also have OnKillFocusFirst1-10 and OnKillFocusSecond1-10. That right there leaves me with 30 member functions in my class. I guess if you need that many functions, you need that many functions. Thanks StuckA.

CJB
 
--MinnisotaFreezing
You are right, there can be something done for reducing the code redundancy.

You will have to use ON_CONTROL_RANGE for the controls handler (for BN_CLICKED for example).
Also ON_COMMAND_RANGE and ON_UPDATE_COMMAND_UI_RANGE for menu items.

Read also the article "Handlers for Message-Map Ranges" from MSDN. There are also some somples at the topics asociated with the Message handlers from above.

One more thing, take care, the COMMMANDS IDs MUST be one after another, otherwise you will get soem nasty runtime errors.

HTH,


s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Thank you so much Burtan. I didn't see how this would work untill I got to the very bottom of the article, and it explains how you can tell which button the message came from.

Thanks to StukA as well. Im sure this is what you were getting at, its just that sometimes I need my hand held.

Thanks again, and I have a problem passing class pointers I need to write up, maybe you two can take a look at it when I am done.

CJB
 
If you are having a problem with class pointers look at this post

thread116-133591 posted a class in there that i refer to from time to time to remind me how exactly to pass method pointers.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top