필터 지우기
필터 지우기

Max number of checked checkboxes/radiobuttons

조회 수: 1 (최근 30일)
Jakub
Jakub 2013년 4월 12일
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.

채택된 답변

Walter Roberson
Walter Roberson 2013년 4월 12일
Only by using the callbacks to check and enforce it.
  댓글 수: 9
Image Analyst
Image Analyst 2013년 4월 14일
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.
Walter Roberson
Walter Roberson 2013년 4월 14일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by