Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent auto-selection of radio buttons

When adding an instance of the Gtk# RadioButton class to a GUI, it is automatically checked ("activated") as the first member of its radio button group.

For Gtk, this has been discussed to some extent in another question, with the main point of the selected answer being that users expect one radio button to be selected at all times.

I do not dispute that.

However, I am automatically generating my user interface in a way so each radio button is linked to a data model, but none of the radio buttons can, at any time, get a reference to any of the other RadioButton instances. The data model ensures that one radio button is checked at all times.

Beside being sufficient to match user expectations, I consider this good practice, as like this, data integrity is ensured by the data model, not by the GUI.

Unfortunately, Gtk# will automatically check all radio buttons like this, as it considers each radio button to be the first in its group. For adding the various radio buttons to the same group, I would have to pass the first radio button in the group to the constructor of the other radio buttons - which I cannot, as pointed out above, as I do not have any way to get the reference to that first radio button when instantiating the others.

Setting the Active property of the radio button to false does not help, nor does invoking the Toggle method.

Is there any way to suppress this automatical selection, possibly by subclassing and overriding something I could not find yet? Alternatively, is it somehow possible to force a CheckButton to look like a radio button for this purpose?

If there is really no other solution, I will try and implement the solution suggested in another answer that involves adding a second hidden radio button for each of my radio buttons, but I would find that extremely hacky for production code.

like image 602
O. R. Mapper Avatar asked Sep 03 '15 22:09

O. R. Mapper


People also ask

How do I make a radio button Not selectable?

Answer: To make a radio button not selectable, in the button's INPUT tag you can use an onclick event handler like this: <INPUT type="radio" name="myButton" value="theValue" onclick="this. checked=false; alert('Sorry, this option is not available!') ">

How can I avoid multiple selected radio buttons?

To avoid having multiple radio buttons selected use the same name attribute. Radio buttons are normally presented in radio groups. Only one radio button in a group can be selected at the same time. The radio group must share the same name (the value of the name attribute) to be treated as a group.

Why multiple radio buttons are getting selected?

Only one radio button in a group can be checked. You have two radio buttons with different names. This means that you have two radio groups, each containing one radio button. You need to put them in the same group (by making them share a name) if you only want one of them to be selected.

How do you ensure that only one radio button is selected at a time?

To make only one radio button checked in some multiple radio buttons, you have to make all of them have the same radio button name and make sure the options Radio buttons with the same name and value are selected in unison is unchecked under Options tab in Radio button Properties dialog boxes for all of those radio ...


1 Answers

Extension to Josip's solution: Create your own radio button widget, actually containing nothing else than two gtk radio buttons, one of which always is hidden. Your factory then creates this one instead of the original gtk button...

like image 190
Aconcagua Avatar answered Jan 03 '23 02:01

Aconcagua