Max number of checked checkboxes/radiobuttons

Hi i have GUI with 18 checkboxes/radiobuttons. Dont know what is better for me...Is there way to set that only two of them can be checked at the same time?? Thanks.

 Réponse acceptée

Walter Roberson
Walter Roberson le 12 Avr 2013

0 votes

Only by using the callbacks to check and enforce it.

9 commentaires

Jakub
Jakub le 12 Avr 2013
And if i use radiobuttons instead of checkboxes? is there any difference?
No difference. radiobuttons can be managed by a uibuttongroup() but uibuttongroup() allows either 0 or 1 item to be selected.
Jakub
Jakub le 12 Avr 2013
Ok, thanks. Can you give me a siplme example please. How to check how many checkboxes are checked. and if two are checked, uncheck one and check another which i clicked on.
Image Analyst
Image Analyst le 12 Avr 2013
If you don't use a button group you can have each checkbox or radio button's callback call the same function, let's say it's called "CountSelectedStates(handles)". Then in that function, you check the state of each checkbox or radio button. The trouble comes in when you have three selected. You're going to have to remember the last state, say an 1 by 18 array of booleans, and figure out which of the two "old" states should be turned off now that the user has selected a third button or box. So if two are selected, and they select a third, how do you decide which one of the two to turn off?
If you use something like
function CountSelectedStates(hobject, event, handles)
then hobject will be the handle of the box that was just checked or un-checked; by get(hobject, 'Value') you can figure out if it was just turned on; if it was, then you could set() it off again. In this way you do not need to remember the order of the boxes being checked, you just refuse to let the third one be checked.
Or another approach, using the same calling sequence, would be when the second box was checked, to set() all of the "off" boxes to 'enable', 'off' so that no more can be checked until one of the two active ones is unchecked (at which point you would re-enable all of them.)
Jakub
Jakub le 14 Avr 2013
Thank you for advices, i think ill do that when third chechbox is going to be checked, the previous two will be unchecked.
Jakub
Jakub le 14 Avr 2013
I dont know how to deal with the function to count selected states...I would appreciate a help, thank you.
function CountSelectedStates(hobject, event, handles)
% Get states of all the checkboxes.
selectedStates(1) = get(handles.checkbox1, 'value');
selectedStates(2) = get(handles.checkbox2, 'value');
selectedStates(3) = get(handles.checkbox3, 'value');
selectedStates(4) = get(handles.checkbox4, 'value');
% Count how many are checked right now.
totalNumberSelected = sum(selectedStates);
% Check which one was just toggled
if hObjects = handles.checkbox1
% If you want to implement Walter's suggestion.
% They just clicked on checkbox 1.
% more code.....
end
if totalNumberSelected >= 3
% Clear states of all the checkboxes, like you wanted.
selectedStates(1) = get(handles.checkbox1, 'value');
selectedStates(2) = get(handles.checkbox2, 'value');
selectedStates(3) = get(handles.checkbox3, 'value');
selectedStates(4) = get(handles.checkbox4, 'value');
% Keep the one they clicked on checked.
set(hObject, 'value', 'on');
end
and so on.
Some small corrections to the above:
if hobject == handles.checkbox1
and to clear the check boxes, do not get() the values, instead
set(handles.checkbox1, 'value', 0);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by