Avoiding automatic grouping of Windows Forms radio button containers

Background

In .NET Windows Forms (2.0), radio buttons are automatically grouped using their container control, whether it be a form, panel, or group,

This means that when you select one switch, each other switch in the same container automatically changes to an unverified 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 
  • Each '[ ]' is a switch.
  • The user is prompted to select the desired answer in the first column and optional (desired, best answer) in column 2.

As you can see, the switches should be grouped by column: selecting one of the radio stations should clear the rest of the column, but should not have any effect in the other column.

Problem

Basically the problem is that it is a dynamically generated form.

  • The number of lines in the layout is dynamic and is loaded from a custom form definition.
  • The description text is unknown until the runtime and length vary greatly (some may have only a few words, and some may be dozens of lines).
  • Layout cannot be changed. This comes from the familiarity of (non-technical) users with a paper version of this form that has been used for years. This form will also be printed, and even if I changed the input system to my form, which would mean encoding a different print version with the "old" layout.

What I tried and did not work

At first, I tried to implement this as a dynamically generated layout using three panels, one for each column, so automatic grouping of switches will work fine. And that happened.

But the layout problems associated with horizontal alignment between the radio buttons and its text just kill me. The size of the text is not known until runtime, and text strings can wrap (sometimes several times for each element). Sometimes it is aligned correctly, sometimes it is not. I'm going to start debugging this, but I think that if you change this implementation with three panels, this is the best option.

What am i trying to do

I would like to reverse engineer the layout using a three-column TableLayoutPanel to simplify alignment-related problems, but that means that I cannot use the auto-container-by-group function.

EDIT:

As suggested by Gerry, another layout option is for each row to be implemented as a user control and use a stream layout panel.

This again means that I can use the controls for the layout, but not for grouping the buttons.

I will need to create groups of vertical radio buttons despite using horizontal user controls

/ EDIT

What am i trying to ask

I need pointers and suggestions on how to manually manage a switch group. I really don't mind subclassing the RadioButton class if necessary.

In particular, is there any “canonical” or known way to subclass “RadioButton” or go completely to the Win32 APIs, so that every time a control is checked (using the mouse, Enter keyboard, spacebar, and in any other way, which I might not know about their choice). Can I delay the event and manually update the status of every other control in my form and so that the auto-grouping function is well known as "deactivated"? I am afraid that the implementation is also related to the container, and I might also need a subclass of Panel ?.

How does the "magic" grouping work in Windows Forms?

Every solution I found for googling search that implements custom switch groups or “switch lists” is somehow based on using a container for the group. Can containers be avoided? If so, how ?.

+7
radio-button layout winforms
source share
2 answers

Workaround (read: promiscuous hacking). I found that I put each and every switch on my small panel. Therefore, there is no grouping between them. I listen to each checked event button and update the corresponding ones accordingly.

I still think it is too much to have many panels, and if anyone has a cleaner solution, I would like to hear it.

(OK, sorry for answering my own question. You know that when you take the time to explain something, you start to see the problem, it's a different light.)

+6
source share

I would choose a simple and simple solution. For example, a custom control that represents one line (so there are two radio buttons and a label).

EDIT: Well, I think I understand your requirement now. Can't you have only two sets of radio objects: those indicated in column A and columns B?

When your form is being drawn, add them to two <RadioButton> lists or something else and bind them to two separate events: columnARBChecked, columnBRBChecked. When one of the events is triggered, you can call some logic to check / uncheck the rest of the radio buttons, as it suits you.

+1
source share

All Articles