Display message when two buttons are pushed?
2 ビュー (過去 30 日間)
古いコメントを表示
Hi I'm trying to design a matching game where if the correct two check boxes are checked, a message will appear. I already know I can use msgbox(text). My problem is determining when the correct two boxes are checked.
c1 = uicheckbox(fig,'Text','',...
'Value',0,...
'Position',[115,150,200,70],...
'ValueChangedFcn',@(c1,event) c1Changed(c1));
c2 = uicheckbox(fig,'Text','',...
'Value',0,...
'Position',[335,150,200,70],...
'ValueChangedFcn',@(c2,event) c2Changed(c2));
function c1Changed(c1)
c1Val = c1.Value;
end
function c2Changed(c2)
c2Val = c2.Value;
end
Thank you in advance!
0 件のコメント
回答 (2 件)
Walter Roberson
2016 年 11 月 23 日
Run through all of your checkboxes that are involved, getting their Value properties -- which you could probably do in a single step by using get() with a vector of handles.
Then use the vector of Values to figure out if a match had occurred.
Image Analyst
2016 年 11 月 23 日
You don't need the function bothChecked_Callback(). And you don't need to have any code in the callbacks for the checkboxes like this:
c1Val = c1.Value; % Totally unnecessary
handles.c1 = c1Val; % Totally unnecessary
The value is ALREADY stored in the handles array. No need to make yet another field to hold the value that is already there.
Whenever you need to determine whether both checkboxes are checked, in whatever callback or function you want that has access to handles, simply do this:
if handles.c1.Value && handles.c2.Value
% Both are checked
msgbox('You found a match!');
else
% At least one is not checked.
end
This is a much simpler option and eliminates a lot of unneeded/unnecessary code and functions. You can put that code anywhere, like in the checkbox callbacks, or pushbutton callbacks, or any custom (non-callback) function that you pass handles to. Don't create new fields and functions that just duplicates functionality that is already there!
参考
カテゴリ
Help Center および File Exchange で Develop Apps Using App Designer についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!