Change the check box value of a Matlab Tree Node Check Box via code

34 次查看(过去 30 天)
Dear mathworks community,
I am currently developing a Matlab GUI and my main element is a Matlab Tree (Check Box) in the Matlab App Designer.
As there are potentially more than 30 Nodes aavalaible to select, I want to add a "SELECT ALL" check box to the gui, that changes the value of all check boxes of the Tree Node to checked or unchecked. Is there a way to do so? Is there a command to change the value of the check box of the node?
If there is no easy way to do it, can you imagine a workaround?
Thanks in advance for your help!

采纳的回答

Cameron
Cameron 2023-2-14
Can you not make them second level nodes?
fig = uifigure;
cbt = uitree(fig,'checkbox','Position',[20 20 150 150]);
% First level nodes
category1 = uitreenode(cbt,'Text','All','NodeData',[]);
% Second level nodes.
p1 = uitreenode(category1,'Text','Option 1');
p2 = uitreenode(category1,'Text','Option 2');
p3 = uitreenode(category1,'Text','Option 3');
p4 = uitreenode(category1,'Text','Option 4');
p5 = uitreenode(category1,'Text','Option 5');
  3 个评论
Cameron
Cameron 2023-2-14
fig = uifigure;
cbt = uitree(fig,'checkbox','Position',[20 20 150 150]);
% Second level nodes.
p1 = uitreenode(cbt,'Text','Option 1');
p2 = uitreenode(cbt,'Text','Option 2');
p3 = uitreenode(cbt,'Text','Option 3');
p4 = uitreenode(cbt,'Text','Option 4');
p5 = uitreenode(cbt,'Text','Option 5');
%if you don't run this next line, none of the boxes will be checked.
cbt.CheckedNodes = cbt.Children;
Are Mjaavatten
Are Mjaavatten 2024-1-16
编辑:Are Mjaavatten 2024-1-16
I needed to start with all boxes checked. This function does this for two levels.
function checkbox_tree_check_all(tree)
topNodes = tree.Children;
allNodes = topNodes;
for i = 1:numel(topNodes)
allNodes = [allNodes;topNodes(i).Children];
end
tree.CheckedNodes = allNodes;
end
Are there more elegant ways?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by