Thread: Radio Buttons in Visual C++ 6

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    52

    Radio Buttons in Visual C++ 6

    Hi, I'm just a 16 year old without much access to much information/resources regarding programming. I have a book and I am working on a program that utilizes radio buttons. I seem, well not seem, I am having problems getting the radio buttons to do what I want them to do in my program. Here is a bit of a background on my application. It is a "visual" calculator for the Gas Laws in Chemistry. I have 5 radio buttons in a group box and basically, when the user selects a radio button i want the program to make certain edit boxes unusable and when the user enters numbers in certain edit boxes, pushes a button, it will do certain calculations based on the radio button that is selected. I know how to do the math, i know how to make edit boxes unusable, what i don't know how to do is the radio button's. Any help would be appreciated, either PM me, Instant message me on AOL instant messenger or just reply to this thread! thanks!

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You have the internet-- you have access to tons of programming information. Since you seem to be developing for Windows, check out MSDN: msdn.microsoft.com

    Okay, are you using MFC or straight Win32 API? If the answer is the latter, are you using a Window or a Dialog?

    If I understand your question correctly, you want to select one radio button and have the other radio buttons unselect? Use SendMessage (hwndButton, BM_SETCHECK, 1, 0); to select a radio button, and SendMessage (hwndButton, BM_SETCHECK, 0, 0); to unselect one.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    I am using Visual C++ 6 and i select MFC Appwizard(Exe) so i assume it is MFC. i don't quite understand where to apply your code. I thought radio buttons are the same as check boxes in the drag and drop interface. I dropped 5 radio buttons in a group box. I made sure the tab order was good, then i checked "Group" in the properties of the first radio button. It then allowed me to set a member variable for the first radiobutton in the group. The only variable type i could select was int. the one resource i had from MSDN was that each radio button after it in the group is assigned a number, the first is 0, the second is 1, and so on. When trying to take advantage of this property, it doesn't seem to work. I tried making an if statement, here is my code. i will explain it after.
    Code:
    void CChemCalcDlg::OnCalculate()
    {
    	UpdateData(TRUE);
    	if (m_RADIO1 == 0)
    	{
    		MessageBox ("Hi");
    	}
    	if (m_RADIO1 == 1)
    	{
    		MessageBox ("Hello");
    	}
    	if (m_RADIO1 == 2)
    	{
    		MessageBox ("Hola");
    	}
    	if (m_RADIO1 == 3)
    	{
    		MessageBox ("Hey");
    	}
    }
    The Messagebox's are used to just test if the radio button's are actually working, and m_RADIO1 is the variable for the first radio button or the group(I really am unsure). Obviously, none of this worked...so that is why i am here, i hope that helps anyone who tries to help me..thanks

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Heh. Just use PV=nRT all the time and be done with it.
    Away.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    well, i need to have these other elements, i could use check boxes but that would require so many if statements, here is some more code, this is me trying to get the radio buttons to make windows elements disabled when the radio button ishighlighted.
    Code:
    void CGasLawsCalcDlg::OnBoyle() 
    {
    	UpdateData(TRUE);
    	if (m_calculate == 0)
    	{
    		GetDlgItem(IDC_TEMP1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_TEMP2)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES2)->EnableWindow(FALSE);
    	}
    	if (m_calculate == 1)
    	{
    		GetDlgItem(IDC_PRESSURE1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_PRESSURE2)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES2)->EnableWindow(FALSE);
    	}
    	if (m_calculate == 2)
    	{
    		GetDlgItem(IDC_VOLUME1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_VOLUME2)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES2)->EnableWindow(FALSE);
    	}
    	if (m_calculate == 3)
    	{
    		GetDlgItem(IDC_MOLES1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES2)->EnableWindow(FALSE);
    	}
    }
    i'd think that would work as i have 5 radio buttons in a group

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    To set the m_calculate variable you will need to use

    SendMessage (hwndButton, BM_GETCHECK , (WPARAM)0, (LPARAM)0);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    how would i use that code to get it to do what i need it to do? btw, here is my current code
    Code:
    void CGasLawsCalcDlg::OnBoyle() 
    {
    	UpdateData(TRUE);
    	SendMessage (hwndButton, BM_GETCHECK , (WPARAM)0, (LPARAM)0);
    	switch (m_calculate)
    	{
    	case 0:
    		GetDlgItem(IDC_TEMP1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_TEMP2)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES2)->EnableWindow(FALSE);
    	case 1:
    		GetDlgItem(IDC_PRESSURE1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_PRESSURE2)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES2)->EnableWindow(FALSE);
    	case 2:
    		GetDlgItem(IDC_VOLUME1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_VOLUME2)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES2)->EnableWindow(FALSE);
    	case 3:
    		GetDlgItem(IDC_MOLES1)->EnableWindow(FALSE);
    		GetDlgItem(IDC_MOLES2)->EnableWindow(FALSE);
    	}
    it is giving me errors...i will look into why though, but please get back to me.
    Last edited by Ripper1; 04-22-2003 at 11:27 PM.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You seem to have four states the calculator can be in.

    The state is stored in the int m_calculate.

    How do you find the value of this variable?

    I would check each radio button using the BM_GETCHECK msg and so determine the m_calculate value (Using the correct HWND for each button)

    Use a series of if statements or a loop looking for which button returns BST_CHECKED to the BM_GETCHECK msg.

    then use the code you posted.

    BTW:
    In that function I would first enable ALL the controls then disable the ones not needed.

    Or process the BN_CLICKED msg for a button in the callback or the OnButtonClick msg.
    Last edited by novacain; 04-23-2003 at 12:52 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    ok, i am a TOTAL MFC newbie, i basically taught myself everything i know and the MSDN sort of sucks for my current position, i have to take a break with learning c to focus on SAT's and AP exams but i would like to fix this app, can you tell me what a HWND is? and give me an example of BM_GETCHECK. regarding your by the way, this is a function that basically i was hoping that it would do this:
    There are 5 radio buttons
    if the user clicks on one radio button to select a gas law, lets just say Boyles Law
    then the program makes the Moles1 edit box(i got rid of moles2) and the temperature edit box's unusable. The current function only worked for boyles law, and if i changed the radio button, the edit box's would remain unusable. thank you!

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Come on Mate! Whos writting this app you or me?


    >>can you tell me what a HWND is?

    A HWND is a Handle to a WiNDow, a window being a control, dialog or window.

    Look for CWND in MFC.

    To get you past you current problem.

    You need to know the checkbox that is currently checked.
    How do you know the user has selected a checkbox?
    Thats where this code has to go.
    Find this by asking each checkbox if it is checked or responding to the message that one has BN_CLICKED.

    Can you find which checkbox has been changed by the user and when?

    Send each a BM_GETCHECK message looking for the checkbox that returns a BST_CHECKED.
    Code:
    mCalculate=0;
    hWnd=GetDlgItem( hWholeDlg, ID_BUTTON1);
    if(BST_CHECKED == SendMessage(hWnd, BM_GETCHECK, (WPARAM)0, (LPARAM)0))
       m_Calculate=1;
    hWnd=GetDlgItem(hWholeDlg ,ID_BUTTON2);
    if(BST_CHECKED == SendMessage(hWnd, BM_GETCHECK, (WPARAM)0, (LPARAM)0))
       m_Calculate=2;
    //ect
    If one does you can use your code.
    If none do then????

    >>this is a function that basically i was hoping that it would do this:

    Yes but if some buttons are turned off for a calculation (say m_calculate=0 then DC_TEMP1, DC_TEMP2 ect will be turned off) , then a differnet type of calculation is selected, the buttons( DC_TEMP1, DC_TEMP2 ect) will still be off. You must turn them back on.
    With your code if I select each type in turn all of the controls will be off.

    PS why is MOLES1 and MOLES2 off in each type?
    Last edited by novacain; 04-23-2003 at 03:58 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    ok, so i decided to try and solve this once and for all, i check out the GetDlgItem function because its how i identify the hwnd..at least from my understanding with reading, it says i need to use the dialog window id as my first paramater, when i do this, i just get errors, i dont know what to place for the first paramater, hWholeDlg. The 2 moles, that was a mistake that i fixed. thanks for the help

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Every window, dialog or control has a different HWND.

    So if a radio button (HWND = hWndRB and ID = IDC_RB01 )

    is placed on a Dialog (HWND = hWndDlg)

    hWndRB=GetDlgItem(hWndDlg,IDC_RB01);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    ah, thanks for clarifying..how might i go about checking the hwnd, i looked in properties and i couldn't find it..sorry about these really newbieish questions, ive decided im going to stop programming MFC after i finish this app, mainly because i need to understand core c++ first. thanks for all help novacain.

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>how might i go about checking the hwnd

    I'm sorry I don't understand the question.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    you say that every window, dialog or control has a different hwnd...how do i find out the name of this hwnd? so i can place that as one of the paramaters in the get dlgfunction...thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM