Radiobutton creation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Radiobutton creation

  1. #1
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Radiobutton creation

    Please give an example code of creating and handling Radiobuttons.. Im using Win32 not MFC.
    Last edited by hypheni; October 20th, 2009 at 10:05 AM.

  2. #2
    Join Date
    Jun 2008
    Posts
    592

    Re: Radiobutton creation

    Code:
    hwnd =  CreateWindowEx(0, "BUTTON", "", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_RADIOBUTTON, 0, 0, 20, 20, Window_hwnd, NULL, GetModuleHandle(NULL), NULL);
    look at http://msdn.microsoft.com/en-us/libr...43(VS.85).aspx for more info
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  3. #3
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Radiobutton creation

    How to handle RADIOBUTTON's BN_CLICKED message which are in the same group. And tell me, just by putting WS_GROUP flag in CreateWindowEx() will the buttons be in same group ?

    See.. what I'm trying to do is.. I'm creating a few RADIOBUTTONS dynamically within a DialogBox and there is one PUSHBUTTON. So while the PUSHBUTTON is pressed it'll retrieve the selected RADIOBUTTON's text.

  4. #4
    Join Date
    Jun 2008
    Posts
    592

    Re: Radiobutton creation

    Only use BS_GROUPBOX when starting a new group. They start as being grouped.

    It also sounds like you want to use BS_AUTORADIOBUTTON instead of BS_RADIOBUTTON to perform automatic selection between radio buttons.

    Use IsDlgButtonChecked or Button_GetCheck to find which radio button is checked
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  5. #5
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Radiobutton creation

    how can i catch the radiobutton when i press the pushbutton...

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    Re: Radiobutton creation

    hypheni, please post non-mfc related questions in the "C++ and WinAPI " forum. This forum is for Visual Studio questions using MFC, and ATL.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Radiobutton creation

    The simplest way would be using CheckRadioButton API:
    void CheckRadioButton(HWND hwndDlg,
    int nIDFirstButton,
    int nIDLastButton,
    int nIDCheckButton)

    This function clears the selection from all buttons with IDs in the range given by nIDFirstButton and nIDLastButton except the one whose ID is given by nIDCheckButton
    Last edited by VictorN; October 21st, 2009 at 02:28 AM.
    Victor Nijegorodov

  8. #8
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Radiobutton creation

    See.. I'm creating RADIOBUTTONs dynamically by CreateWindowEx(). So how should I obtain the Identifier (Resource ID) of those RADIOBUTTONs. see my code..

    Code:
    while(some condition here)
    {
    hRadio[i] = CreateWindowEx(0, L"BUTTON", 0, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 26, hight, 140, 20, hParent, (HMENU)IDB_RADIO, hIns, 0);
    SetWindowText(hRadio[i], string);
    i++;
    hight+=20;
    }

    here ID is IDB_RADIO.. for this I need to put it in the Resource.h file of the project. so how it can be done in case of dynamic creation..

  9. #9
    Join Date
    Jun 2008
    Posts
    592

    Re: Radiobutton creation

    Well to process a radio button in win32 style would be
    Code:
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message) 
        {
            case WM_COMMAND:
            {
                if( HIWORD(wParam) == BN_CLICKED )
                {
                    HANDLE ButtonId = LOWORD(wParam);
                    HWND ButtonHwnd = lParam;
                }
            }
            break;
     
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
    Quote Originally Posted by hypheni
    here ID is IDB_RADIO.. for this I need to put it in the Resource.h file of the project. so how it can be done in case of dynamic creation..
    I would simply just add them by hand and leave at that.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Radiobutton creation

    Quote Originally Posted by hypheni View Post
    See.. I'm creating RADIOBUTTONs dynamically by CreateWindowEx(). So how should I obtain the Identifier (Resource ID) of those RADIOBUTTONs. see my code..

    Code:
    ... 
    hRadio[i] = CreateWindowEx(0, L"BUTTON", 0, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 26, hight, 140, 20, 
                                hParent, (HMENU)IDB_RADIO, hIns, 0);
    here ID is IDB_RADIO.. for this I need to put it in the Resource.h file of the project. so how it can be done in case of dynamic creation..
    But you are passing this id into CreateWindowEx as HMENU hMenu (that is according to MSDN:
    hMenu
    [in] Handle to a menu, or specifies a child-window identifier, depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.
    !
    Victor Nijegorodov

  11. #11
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Radiobutton creation

    @VictorN

    Thats fine..
    Code:
    #define IDB_RADIO [ID here]
    This should be placed into resource.h. Am I right ??.
    But the problem is Im creating multiple RADIOBUTTONs Dynamically using loop. So How should I put those Dynamic IDs in resource.h file ?

    Code:
    hRadio[i] = CreateWindowEx(0, L"BUTTON", 0, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 26, hight, 
                                                                140, 20, hParent, (HMENU)IDB_RADIO[i], hIns, 0);
    IDB_RADIO[i].. What should I put into resource.h file and how do I handle these in CALLBACK function.

    Hope you got my point.
    Last edited by hypheni; October 21st, 2009 at 03:00 AM.

  12. #12
    Join Date
    Jun 2008
    Posts
    592

    Re: Radiobutton creation

    Code:
    enum
    {
             IDB_RADIO1 = 200, 
             IDB_RADIO2, //201
             IDB_RADIO3, //202 
             IDB_RADIO4,
             IDB_RADIO5,
             IDB_RADIO6,
             IDB_RADIO7,
             IDB_RADIO8,
    };
     
    hRadio[i] = CreateWindowEx(0, L"BUTTON", 0, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 
    26, hight, 140, 20, hParent, (HMENU)IDB_RADIO1 + i, hIns, 0);
    I told you how to handle them in the callback
    http://www.codeguru.com/forum/showpo...27&postcount=9
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Radiobutton creation

    Quote Originally Posted by hypheni View Post
    @VictorN

    Thats fine..
    Code:
    #define IDB_RADIO [ID here]
    This should be placed into resource.h. Am I right ??...
    Mmm...
    You may and you may not...
    Generally you don't need to put in the resource.h. It doesn't matter in what file it will be defined.
    Just define (reserve) a range (beginning from IDB_RADIO) of "unique" IDs for your radio buttons so no other control of the same dialog uses any ID from this range.
    Increment the ID passed in the CreateWindowEx in your while loop and save the count of all created radio buttons (say, nCount)
    Now all your radio buttons will have the IDs from IDB_RADIO to (IDB_RADIO + nCount -1), so you'll be able to use CheckRadioButton and IsDlgButtonChecked APIs

    [QUOTE=hypheni;1888738... how do I handle these in CALLBACK function.

    Hope you got my point.[/QUOTE]And what would be the problem if you knew all your button IDs?
    Victor Nijegorodov

  14. #14
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Radiobutton creation

    Okaay.. See my code.. This is for creating those RadioButtons.. This is working perfectly well..

    Code:
    #define ID_RADIO1		        1034
    #define ID_RADIO2		        1035
    #define ID_RADIO3		        1036
    #define ID_RADIO4		        1037
    #define ID_RADIO5		        1038
    #define ID_RADIO6		        1039
    
    
    int i=0;
    HWND hRadio [10];              //Maximum 10 RadioButtons
    while (some condition)
    {
        hRadio[i] = CreateWindowEx(0, L"BUTTON", 0, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,                   
                                                       26, hi, 140, 20, hReIP, (HMENU)1034+i, hIns, 0);
        SetWindowText(hRadio[i], lpHold); 
        SendMessage(hRadio[i], WM_SETFONT, (WPARAM)hFont, 0);
        hi+=18;
        i++;
    }
    Now, the message handling code.. where Im having the actual problem.. See I need to choose only one radio button at a time and process that RadioButton while pressing a PUSHBUTTON to get the RadioButton's name.

    Code:
    case WM_COMMAND:
    {
         switch(LOWORD(wParam))
         {
             case ID_RADIO1:
                 if ( HIWORD(wParam) == BN_CLICKED)
                 {
                     WCHAR text[100];
                     SendMessage((HWND)hRadio, WM_GETTEXT, 100, (LPARAM)text);
                     MessageBox(0,text,0,0);
                 }
            break;
         }
    }
    But this is handling just 1st radio button which in fact working but if i process same way IDB_RADIO2 and so on it's not working for those buttons. and I need to process radiobutton with a pushbutton.
    Last edited by hypheni; October 23rd, 2009 at 01:32 AM.

  15. #15
    Join Date
    Jun 2008
    Posts
    592

    Re: Radiobutton creation

    I don't know you mean by process radiobutton with a pushbutton. What are you trying with the push button?
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center