hypheni
October 20th, 2009, 08:58 AM
Please give an example code of creating and handling Radiobuttons.. Im using Win32 not MFC.
|
Click to See Complete Forum and Search --> : Radiobutton creation hypheni October 20th, 2009, 08:58 AM Please give an example code of creating and handling Radiobuttons.. Im using Win32 not MFC. Joeman October 20th, 2009, 09:02 AM 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/library/bb775943(VS.85).aspx for more info hypheni October 20th, 2009, 11:04 AM 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. Joeman October 20th, 2009, 11:54 AM 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 hypheni October 20th, 2009, 12:34 PM how can i catch the radiobutton when i press the pushbutton... Arjay October 20th, 2009, 12:56 PM hypheni, please post non-mfc related questions in the "C++ and WinAPI (http://www.codeguru.com/forum/forumdisplay.php?f=47) " forum. This forum is for Visual Studio questions using MFC, and ATL. VictorN October 20th, 2009, 02:11 PM 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 hypheni October 21st, 2009, 01:17 AM See.. I'm creating RADIOBUTTONs dynamically by CreateWindowEx(). So how should I obtain the Identifier (Resource ID) of those RADIOBUTTONs. see my 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.. Joeman October 21st, 2009, 02:49 AM Well to process a radio button in win32 style would be 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; } 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. VictorN October 21st, 2009, 03:36 AM See.. I'm creating RADIOBUTTONs dynamically by CreateWindowEx(). So how should I obtain the Identifier (Resource ID) of those RADIOBUTTONs. see my 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. ! :wave: hypheni October 21st, 2009, 03:57 AM @VictorN Thats fine.. #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 ? 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. Joeman October 21st, 2009, 04:29 AM 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/showpost.php?p=1888727&postcount=9 VictorN October 21st, 2009, 04:35 AM @VictorN Thats fine.. #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? :confused: hypheni October 23rd, 2009, 02:27 AM Okaay.. See my code.. This is for creating those RadioButtons.. This is working perfectly well.. #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. 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. Joeman October 23rd, 2009, 02:48 AM I don't know you mean by process radiobutton with a pushbutton. What are you trying with the push button? hypheni October 23rd, 2009, 03:12 AM my aim is to select a radiobutton and then push a pushbutton. that pushbutton will retrieve the selected radiobutton's text.. Joeman October 23rd, 2009, 03:33 AM Are you having trouble with knowing when you pressed the push button? hypheni October 23rd, 2009, 03:35 AM see.. i want to select a radiobutton and retrieve that radio's name by a messagebox by pressing a pushbutton.. VictorN October 23rd, 2009, 03:38 AM ... 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; } }What does this "undefined" hRadio mean? Joeman October 23rd, 2009, 03:40 AM So I suppose that means yes? It is the same for any type of button case WM_COMMAND: { switch(LOWORD(wParam)) { case ID_PUSHBUTTON: if ( HIWORD(wParam) == BN_CLICKED) { WCHAR text[100]; SendMessage((HWND)hRadio, WM_GETTEXT, 100, (LPARAM)text); MessageBox(0,text,0,0); } break; } } is that what you wanted? VictorN October 23rd, 2009, 03:43 AM see.. i want to select a radiobutton and retrieve that radio's name by a messagebox by pressing a pushbutton.. Which "radiobutton" from these ten do you want to "select?" How do you want to "select": using CheckRadioButton API or...? :confused: Please, don't post code with undefined identificators hypheni October 23rd, 2009, 03:49 AM @..Victor N. HWND hRadio[10] is declared globally... and SendMessage() can not cast to HWND to HWND_ thats wht (HWND)hRadio. @..Joeman Ya this I need.. But in.. SendMessage((HWND)hRadio, WM_GETTEXT, 100, (LPARAM)text); hRadio index should be mentioned.. here is the problem. selected radiobutton's index should be mentioned here.. Joeman October 23rd, 2009, 04:02 AM I suppose you can loop through each radio button and check to see which is checked with IsDlgButtonChecked or SendMessage.. for ex //loop code LRESULT lResult = SendMessage( hRadio[i], BM_GETCHECK, 0, 0 ); bool Checked = lResult == BST_CHECKED; if( Checked ) RadioId = i; //end of loop code now you have the Id in RadioId hypheni October 23rd, 2009, 04:04 AM @..VictorN i have checked radiobutton 1.. by putting CheckRadioButton(hReIP, ID_RADIO1, ID_RADIO10, ID_RADIO1); in WM_INITDIALOG. hypheni October 23rd, 2009, 04:14 AM @..Joeman I think this code is going to be useful for me. Let me implement it a bit more.. Thanks.. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |