The background
In .NET Windows Forms (2.0) radio buttons are automatically grouped by their container control, whether it is a Form, Panel or GroupBox.
That means that when you select one radio button, every other radio button in the same container is automatically changed to unchecked state.
Now, consider the following form layout:
MUST HAVE NICE TO HAVE DESCRIPTION
--------- ------------ -----------------------------------------------
[ ] [ ] The gizmo works
[ ] [ ] The gizmo works beautifully
[ ] [ ] The gizmo works amazingly and makes you breakfast
[ ] [ ] The gizmo comes with a pony
'[ ]'
is a radio button.As you can see, the radio buttons should be grouped by column: selecting one of the radios should clear the rest of the column, but should have no effect in the other column.
The problem
Basically the problem is that this is a dynamically generated form.
What I have tried and didn't work
I first tried to implement this as a dynamically generated layout using three panels, one for each column, so the automatic radio button grouping would work just fine. And it did.
But the layout problems related to horizontal alignment between the radio buttons and its text are just killing me. The size of the text is not known until runtime and the text lines might wrap (sometimes several times for each item). Sometimes it aligns properly, sometimes it doesn't. I'm about to start debugging this, but I'm thinking if maybe changing this three-panel implementation is a better option.
What I'm trying to do
I would like to redesign the layout by using a TableLayoutPanel
with three columns in order to simplify the alignment related issues, but that means that I cannot use the automatic radio button grouping-by-container "feature".
EDIT:
As Gerrie suggested, other layout option is to have each line implemented as a user control and use a flow layout panel.
That means, again, that I might want to use controls for layout, but not for radio button grouping.
I would need to get vertical radio button groups despite using horizontal user controls
/EDIT
What I'm trying to ask
I need pointers and suggestions about how to manually control the grouping of the radio buttons. I really don't mind subclassing the RadioButton
class if needed.
Specifically, is there any 'canonical' or known way of subclassing 'RadioButton' or go all the way down to Win32 APIs, so that every time the control is checked (by using the mouse, keyboard Enter, Space bar, and any other way that I could not be aware of selecting them) I can trap the event and manually update the state of every other control on my form and in a way that the automatic grouping feature is well known to be 'de-activated'? I fear that the implementation is also container-related, and I might be also forced to subclass Panel?.
How does the grouping 'magic' work in Windows Forms?
Every solution I have found googling that implements custom radio button groups or 'radio button lists' is based in some way in using a container for the group. Can containers be avoided? If so, how?.
You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.
You can Add GroupBox Control or Panel control into the Form. In this Control, you can Nest RadioButton Instances. Drag and Drop GroupBox and inside it Drag and Drop Radio Button Control and Button Control. By Using Nesting Strategy, you can have Multiple groups of RadioButtons on a Single Form.
In android, Radio Group is used to group one or more radio buttons into separate groups based on our requirements. If we group Radio Buttons using RadioGroup, at a time only one item can be selected from the group of radio buttons.
The RadioButton control is used to provide a set of mutually exclusive options. The user can select one radio button in a group. If you need to place more than one group of radio buttons in the same form, you should place them in different container controls like a GroupBox control.
A temporal solution (read: messy hack) I found is that I'm putting each and every radio button in its own small panel. So no grouping at all happens between any of them. I'm listening to every button checked
events and updating the appropriate ones accordingly.
I still think it is overkill to have that many panels, and if anyone has a cleaner solution I'd love to hear it.
(OK, sorry for answering my own question. You know that when you take the time to explain something you start seeing the problem it a different light.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With