[RESOLVED] MFC Concept Help- Radio Buttons
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: [RESOLVED] MFC Concept Help- Radio Buttons

  1. #1
    Join Date
    Sep 2011
    Posts
    10

    [RESOLVED] MFC Concept Help- Radio Buttons

    I am having a little bit of a concept problem having to do with MFC (and I hope this is the right part of the forum). I am writing a program with MFC / C++ to control a robot.

    I did do some other research on this and it seems that the radio buttons cannot return a value. Initially I thought I could have return values for the joint sent from the radio button that would tell the SEND command which joint to move, but I am not really sure how I can do this now. Does anyone else have an idea of how this could be done?

    Thanks.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: MFC Concept Help- Radio Buttons

    Radio buttons sends a message that you can catch and then translate into something that fits your application. See http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: MFC Concept Help- Radio Buttons

    Quote Originally Posted by SpartanGuy07 View Post
    I am having a little bit of a concept problem having to do with MFC (and I hope this is the right part of the forum).
    If your program really is MFC, then no, this is not the right part. The correct section to post about it would then be http://www.codeguru.com/forum/forumdisplay.php?f=7. And in this case the link posted by S_M_A is probably a good place to start.

    If your code actually should be C++/CLI, you may want to have a look at http://msdn.microsoft.com/en-us/libr...diobutton.aspx, in particular the events.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Sep 2011
    Posts
    10

    Re: MFC Concept Help- Radio Buttons

    @Eri523

    Sorry about the wrong section. If either of you guys can move it then go ahead.

    @S_M_A

    I am using a worker thread to run this part of the application because otherwise the GUI freezes.

    Code:
    UINT WorkerThreadProc( LPVOID pParent )
    {
    	CString command;
    	CXdemDlg* p = ((CXdemDlg*)pParent);
    	do
    	{
    		if (p->RadioWaist.Checked)
    			(p->m_edresp.SetWindowText("True"));
    	} while ( ((CXdemDlg*)pParent)->isStepping() );
    	return TRUE;
    }
    RadioWaist is the member variable (an int) for the array of radio buttons. So obviously if I try to compile this I will get a type mismatch, however, it won't allow me to access Radio_Waist (the radio button). I am a bit new to multi-threading, so is there something that I am missing that I need to do in order to be able to check the status of the radio button and not just the variable?

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: MFC Concept Help- Radio Buttons

    I don't think I really understand why your design has to be like this but if you really need to check the radio button like that you can use GetDlgItem http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx.

    A better way in my opinion is to add code that handles the radio buttons (in the GUI code) and let that code refine the information into an enum or similar to be accessed in the worker thread. Don't forget to make it thread safe http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

    Doing it that way makes it easier if you later on decide to replace the radio buttons with for instance a combox.
    Last edited by S_M_A; October 1st, 2011 at 10:23 AM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  6. #6
    Join Date
    Sep 2011
    Posts
    10

    Re: MFC Concept Help- Radio Buttons

    Quote Originally Posted by S_M_A View Post
    I don't think I really understand why your design has to be like this but if you really need to check the radio button like that you can use GetDlgItem http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx.
    I think we might have a bit of a misunderstanding here.

    I did write a switch function that returned a value for when one of the radio buttons was clicked (just to test to make sure I did it correctly).

    Code:
    int CXdemDlg::Joint()
    {
    	UpdateData(true);
    
    	switch(RadioWaist)
    	{
    	case 0:
    			m_edresp.SetWindowText("Waist Selected");
    			return(1);
    			break;
    	case 1:
    			m_edresp.SetWindowText("Shoulder Selected");
    			return(2);
    			break;
    	case 2:
    			m_edresp.SetWindowText("Elbow Selected");
    			break;
    	case 3:
    			m_edresp.SetWindowText("Hand Selected");
    			break;
    	case 4:
    			m_edresp.SetWindowText("Wrist Selected");
    			break;
    	}
    
    }
    But I guess this can't be directly applied to the thread? I get an assert saying that if this fired I am mostly trying to pass objects from one thread to another. I guess that is what you mean by making it safe?

    This is where I am a bit confused....

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: MFC Concept Help- Radio Buttons

    I don't know how you called this but since the assert fired you probably called it from the thread. See this quite recent thread regarding that http://www.codeguru.com/forum/showthread.php?t=516854

    What you can do instead is to let the dialog code catch the message that the radio button sends when being selected and set a member variable to specific value. Probably an enum like Waist, Shoulder ... and so on. That member variable can safely be accessed by the thread if protected by for instance a CriticalSection see http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  8. #8
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,286

    Re: MFC Concept Help- Radio Buttons

    Quote Originally Posted by SpartanGuy07 View Post
    I think we might have a bit of a misunderstanding here.

    I did write a switch function that returned a value for when one of the radio buttons was clicked (just to test to make sure I did it correctly).

    Code:
    int CXdemDlg::Joint()
    {
    	UpdateData(true);
    
    	switch(RadioWaist)
    	{
    	case 0:
    			m_edresp.SetWindowText("Waist Selected");
    			return(1);
    			break;
    	case 1:
    			m_edresp.SetWindowText("Shoulder Selected");
    			return(2);
    			break;
    	case 2:
    			m_edresp.SetWindowText("Elbow Selected");
    			break;
    	case 3:
    			m_edresp.SetWindowText("Hand Selected");
    			break;
    	case 4:
    			m_edresp.SetWindowText("Wrist Selected");
    			break;
    	}
    
    }
    But I guess this can't be directly applied to the thread? I get an assert saying that if this fired I am mostly trying to pass objects from one thread to another. I guess that is what you mean by making it safe?

    This is where I am a bit confused....
    The simple way to deal with this is to let MFC help you.

    If you set the group style to the first radio button, and then create an INT DDX variable using the "Add variable" option, MFC will track the selected radio item in that group.

    Then you can pass the index of the selected radio button to the thread when you start the thread.

  9. #9
    Join Date
    Sep 2011
    Posts
    10

    Re: MFC Concept Help- Radio Buttons

    Quote Originally Posted by Arjay View Post
    The simple way to deal with this is to let MFC help you.

    If you set the group style to the first radio button, and then create an INT DDX variable using the "Add variable" option, MFC will track the selected radio item in that group.

    Then you can pass the index of the selected radio button to the thread when you start the thread.
    I have actually done this. Like I said I wanted to test to make sure that I set up the group right and everything so I created the function Joint to reply in an edit box which joint radio button was clicked. The issue I am apparently having is passing the index for the radio button to the thread.

    Code:
    void CXdemDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CXdemDlg)
    	DDX_Control(pDX, IDC_ED_RESP, m_edresp);
    	DDX_Control(pDX, IDC_ROBXCTRL1, m_robx);
    	DDX_Control(pDX, StepSizeInput, SSI);
    	DDX_Radio(pDX, Radio_Waist, WhichRadio);
    	//}}AFX_DATA_MAP
    }
    Here are my DDX controls. I set the group of radio buttons as Radio because thats what I read somewhere else. Is that what should be changed?

    My radio buttons are:
    Radio_Waist
    Radio_Shoulder
    Radio_Elbow
    Radio_Wrist
    Radio_Hand

    And like I said I tried to make sure I set that up right first by having a confirmation print out to an edit box saying "Waist Selected" or "Shoulder Selected"...etc. So I know they are assigned the way they should be with indexes correct.

    My issue seems to be with the thread accessing them correctly?

    P.S. The returns in the Joint function were just as a test for something. But is that how I should pass it to the thread?
    Last edited by SpartanGuy07; October 3rd, 2011 at 03:20 PM.

  10. #10
    Join Date
    Sep 2011
    Posts
    10

    Re: MFC Concept Help- Radio Buttons

    RESOLVED! Finally got it to work the way I wanted. Thank you for all the help and guidance S_M_A and Arjay!

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center