Please give an example code of creating and handling Radiobuttons.. Im using Win32 not MFC.
Printable View
Please give an example code of creating and handling Radiobuttons.. Im using Win32 not MFC.
look at http://msdn.microsoft.com/en-us/libr...43(VS.85).aspx for more infoCode:hwnd = CreateWindowEx(0, "BUTTON", "", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_RADIOBUTTON, 0, 0, 20, 20, Window_hwnd, NULL, GetModuleHandle(NULL), NULL);
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.
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
how can i catch the radiobutton when i press the pushbutton...
hypheni, please post non-mfc related questions in the "C++ and WinAPI " forum. This forum is for Visual Studio questions using MFC, and ATL.
The simplest way would be using CheckRadioButton API:Quote:
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
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..
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;
}
I would simply just add them by hand and leave at that.Quote:
Originally Posted by hypheni
But you are passing this id into CreateWindowEx as HMENU hMenu (that is according to MSDN:! :wave:Quote:
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.
@VictorN
Thats fine..
This should be placed into resource.h. Am I right ??.Code:#define IDB_RADIO [ID here]
But the problem is Im creating multiple RADIOBUTTONs Dynamically using loop. So How should I put those Dynamic IDs in resource.h file ?
IDB_RADIO[i].. What should I put into resource.h file and how do I handle these in CALLBACK function.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);
Hope you got my point.
I told you how to handle them in the callbackCode: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);
http://www.codeguru.com/forum/showpo...27&postcount=9
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:
Okaay.. See my code.. This is for creating those RadioButtons.. This is working perfectly well..
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:#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++;
}
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.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;
}
}
I don't know you mean by process radiobutton with a pushbutton. What are you trying with the push button?
my aim is to select a radiobutton and then push a pushbutton. that pushbutton will retrieve the selected radiobutton's text..
Are you having trouble with knowing when you pressed the push button?
see.. i want to select a radiobutton and retrieve that radio's name by a messagebox by pressing a pushbutton..
So I suppose that means yes? It is the same for any type of button
is that what you wanted?Code: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;
}
}
@..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..
I suppose you can loop through each radio button and check to see which is checked with IsDlgButtonChecked or SendMessage.. for ex
now you have the Id in RadioIdCode:
//loop code
LRESULT lResult = SendMessage( hRadio[i], BM_GETCHECK, 0, 0 );
bool Checked = lResult == BST_CHECKED;
if( Checked ) RadioId = i;
//end of loop code
@..VictorN
i have checked radiobutton 1.. by putting
CheckRadioButton(hReIP, ID_RADIO1, ID_RADIO10, ID_RADIO1);
in WM_INITDIALOG.
@..Joeman
I think this code is going to be useful for me. Let me implement it a bit more.. Thanks..