GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Oct-2004, 13:43
drewdaman drewdaman is offline
New Member
 
Join Date: Sep 2004
Posts: 14
drewdaman is on a distinguished road

radio buttons


hello all,

thank you for replying to my previous message luciwicz. i moved away from the mfc part of my project for a while, but now its time to work on it again!

i have created some five radio buttons. i read somewhere that there is a way to group them together. is this true? i can only select one out of the five buttons, so maybe this is already done. i was wondering how i can figure out which one is selected? i know that there is a GetCheck() method. But is there a better way to figure out which button has been marked instead of checking each one individually?

the following is the code where i declare the buttons, just so you can make sure i'm doing things teh way i'm supposed to! i have, of course, declared them somewhere else (in a header file).

CPP / C++ / C Code:
	//two step creation process (for each control)
	  one=new CButton();
	  one->Create("Channel 1", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
		  CRect(cLeft,cTop,cLeft+cWidth,cBottom), 
		  this, 111); 
		
		//increment the variables appropriately.
	  cTop=cBottom+incCC;
	  cBottom=cTop + cHeight;

	  two=new CButton();
	  two->Create("Channel 2", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
		  CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, 222); 

	  cTop=cBottom+incCC;
	  cBottom=cTop + cHeight;

	  three=new CButton();
	  three->Create("Channel 3", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
		  CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, 333); 

	  cTop=cBottom+incCC;
	  cBottom=cTop + cHeight;

	  four=new CButton();
	  four->Create("Channel 4", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
		  CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, 444);
Last edited by LuciWiz : 13-Oct-2004 at 01:16. Reason: Please insert your c++ code between [c++] [/c++] tags
  #2  
Old 14-Oct-2004, 08:55
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
When you create the buttons via the Resource Editor, if you access the properties of one of the created buttons, you will be able to set it's "Group" property.
This means that every button with an Id greater than this button's will be part of the group. This applies to all buttons folowing, unless another button with the "Group" property set follows.
Every button after that will be in the second group.
Since you created the radio buttons dinamically, I think they all default to one group (I'm not sure how/whether you can change this behaviour).

As to the other question, I sometimes use a different mechanism to see which button is checked - maybe you can use it as well, although it's kind of a hack...
I just use a flag variable - which could be a member of your dialog - and implement each buttons OnClick event. In each implementation I set the flag's value to the number of the button (0, 1, 2, ...).
When I want to know which button is checked, I just test this flag!
Of course, in this case the implementation can be much more elegant using ON_COMMAND_RANGE

I changed your chosen ID's to more reasonable ones and defined the first one to a default value of 111.
Let's see what we have; first, define this value:

CPP / C++ / C Code:
#define IDC_FIRST_BUTTON 111

Then, in your OnInitDialog:

CPP / C++ / C Code:
	//two step creation process (for each control)
	int cTop = 50, cBottom = 100, incCC = 5, cHeight = 50, cWidth = 100, cLeft = 50;
    
	one = new CButton();
    one->Create("Channel 1", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
      CRect(cLeft, cTop, cLeft + cWidth, cBottom), 
      this, IDC_FIRST_BUTTON); 
    
    //increment the variables appropriately.
    cTop = cBottom + incCC;
    cBottom = cTop + cHeight;

    two = new CButton();
    two->Create("Channel 2", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
      CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON + 1); 

    cTop = cBottom + incCC;
    cBottom = cTop + cHeight;

    three = new CButton();
    three->Create("Channel 3", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
      CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON + 2); 

    cTop = cBottom + incCC;
    cBottom = cTop + cHeight;

    four = new CButton();
    four->Create("Channel 4", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
      CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON + 3);


Let's now map a method - lets say OnRadioClicked on the created buttons (just add the line I did):

CPP / C++ / C Code:
BEGIN_MESSAGE_MAP(CRadioDlg, CDialog)
	//{{AFX_MSG_MAP(CRadioDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(IDC_FIRST_BUTTON, IDC_FIRST_BUTTON + 3, OnRadioClicked) // THIS IS THE LINE I ADDED	
END_MESSAGE_MAP()

Now lets tell him who the new method is:

radioDlg.h
CPP / C++ / C Code:
	// Generated message map functions
	//{{AFX_MSG(CRadioDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnRadioClicked( UINT nID ); // THIS IS THE LINE I ADDED
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

Now lets implement the new method:

CPP / C++ / C Code:
void CRadioDlg::OnRadioClicked( UINT nID )
{
	int nButton = nID - IDC_FIRST_BUTTON; 
	switch (nButton)
	{
	case 0:
		AfxMessageBox("First button selected");
		break;
	case 1:
		AfxMessageBox("Second button selected");
		break;
	case 2:
		AfxMessageBox("Third button selected");
		break;
	case 3:
		AfxMessageBox("Fourth button selected");
		break;
	default:
		// Not handled
		break;
	}    	
}


Of course, this is a stupid example; just something to get you going
Hope this is helpfull enough....

Kind regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 15-Oct-2004, 09:24
drewdaman drewdaman is offline
New Member
 
Join Date: Sep 2004
Posts: 14
drewdaman is on a distinguished road
Quote:
Originally Posted by LuciWiz
When you create the buttons via the Resource Editor, if you access the properties of one of the created buttons, you will be able to set it's "Group" property.
This means that every button with an Id greater than this button's will be part of the group. This applies to all buttons folowing, unless another button with the "Group" property set follows.
Every button after that will be in the second group.
Since you created the radio buttons dinamically, I think they all default to one group (I'm not sure how/whether you can change this behaviour).

As to the other question, I sometimes use a different mechanism to see which button is checked - maybe you can use it as well, although it's kind of a hack...
I just use a flag variable - which could be a member of your dialog - and implement each buttons OnClick event. In each implementation I set the flag's value to the number of the button (0, 1, 2, ...).
When I want to know which button is checked, I just test this flag!
Of course, in this case the implementation can be much more elegant using ON_COMMAND_RANGE

I changed your chosen ID's to more reasonable ones and defined the first one to a default value of 111.
Let's see what we have; first, define this value:

CPP / C++ / C Code:
#define IDC_FIRST_BUTTON 111

Then, in your OnInitDialog:

CPP / C++ / C Code:
	//two step creation process (for each control)
	int cTop = 50, cBottom = 100, incCC = 5, cHeight = 50, cWidth = 100, cLeft = 50;
    
	one = new CButton();
    one->Create("Channel 1", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
      CRect(cLeft, cTop, cLeft + cWidth, cBottom), 
      this, IDC_FIRST_BUTTON); 
    
    //increment the variables appropriately.
    cTop = cBottom + incCC;
    cBottom = cTop + cHeight;

    two = new CButton();
    two->Create("Channel 2", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
      CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON + 1); 

    cTop = cBottom + incCC;
    cBottom = cTop + cHeight;

    three = new CButton();
    three->Create("Channel 3", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
      CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON + 2); 

    cTop = cBottom + incCC;
    cBottom = cTop + cHeight;

    four = new CButton();
    four->Create("Channel 4", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE, 
      CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON + 3);


Let's now map a method - lets say OnRadioClicked on the created buttons (just add the line I did):

CPP / C++ / C Code:
BEGIN_MESSAGE_MAP(CRadioDlg, CDialog)
	//{{AFX_MSG_MAP(CRadioDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(IDC_FIRST_BUTTON, IDC_FIRST_BUTTON + 3, OnRadioClicked) // THIS IS THE LINE I ADDED	
END_MESSAGE_MAP()

Now lets tell him who the new method is:

radioDlg.h
CPP / C++ / C Code:
	// Generated message map functions
	//{{AFX_MSG(CRadioDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnRadioClicked( UINT nID ); // THIS IS THE LINE I ADDED
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

Now lets implement the new method:

CPP / C++ / C Code:
void CRadioDlg::OnRadioClicked( UINT nID )
{
	int nButton = nID - IDC_FIRST_BUTTON; 
	switch (nButton)
	{
	case 0:
		AfxMessageBox("First button selected");
		break;
	case 1:
		AfxMessageBox("Second button selected");
		break;
	case 2:
		AfxMessageBox("Third button selected");
		break;
	case 3:
		AfxMessageBox("Fourth button selected");
		break;
	default:
		// Not handled
		break;
	}    	
}


Of course, this is a stupid example; just something to get you going
Hope this is helpfull enough....

Kind regards,
Luci


thanks! looks like i really need to look over message maps!! do you know any good tutorials for them? thanks.
  #4  
Old 15-Oct-2004, 10:23
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Well, when it comes to VC, I usually just point to the msdn

However, I also recommend this great tutorial - all you have to do is do a search on MESSAGE_MAP once you opened the page in your browser. I am confident you will find all you need to know here!

For further questions, you are welcome to post here!

P.S: I will have to ask you to use the code tags in your posts as I suggested. For further info on this please direct your browser to this page Thank you.

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 04-Dec-2005, 10:49
rajesh20k rajesh20k is offline
New Member
 
Join Date: Nov 2004
Posts: 2
rajesh20k is on a distinguished road

Re: radio buttons


This is a good example for adding single event - handler for radio button
Regard
Rajesh
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
collecting user input from radio buttons. Adelle29 Web Design Forum 1 31-May-2004 17:28
windows media player internet radio Hood Computer Software Forum - Windows 0 02-May-2004 03:58
GIDForums MemberRank buttons. JdS GIDForums™ 2 24-Jan-2004 22:35
radio button problem zuzupus MySQL / PHP Forum 1 08-Aug-2003 04:25
GIDTopsites buttons jrobbio GIDTopsites™ 0 26-Mar-2003 09:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:31.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.