Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
In previous posts I showed how we can create custom Matlab app toolstrips using controls such as buttons, checkboxes, sliders and spinners. Today I will show how we can incorporate even more complex selection controls into our toolstrip: lists, drop-downs, popups etc.
![]() Toolstrips can be a bit complex to develop so I’m proceeding slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in the Toolstrip miniseries before reading this post. Also, remember to add the following code snippet at the beginning of your code so that the relevant toolstrip classes will be recognized by Matlab: import matlab.ui.internal.toolstrip.* There are 4 types of popups in toolstrip controls:
The simple DropDown toolstrip control is very easy to set up and use: hPopup = DropDown({'Label1';'Label2';'Label3'});hPopup.Value = 'Label3';hPopup.ValueChangedFcn = @ValueChangedCallback; ![]() We can have the control hold a different value for each of the displayed labels, by specifying the input items as an Nx2 cell-array: items = {'One', 'Label1'; ...'Two', 'Label2'; ...'Three', 'Label3'}hPopup = DropDown(items);hPopup.Value = 'Two';hPopup.ValueChangedFcn = @ValueChangedCallback; This drop-down control will display the labels “Label1”, “Label2” (initially selected), and “Label3”. Whenever the selected drop-down item is changed, the corresponding popup Value will change to the corresponding value. For example, when “Label3” is selected in the drop-down, hPopup.Value will change to ‘Three’. Another useful feature of the toolstrip DropDown control is the Editable property (logical true/false, default=false), which enables the user to modify the entry in the drop-down’s editbox. Any custom text entered within the editbox will update the control’s Value property to that string. ListBox We can create a ListBox in a very similarly manner to DropDown. For example, the following code snippet creates a list-box that spans the entire toolstrip column height and has 2 of its items initially selected: hColumn = hSection.addColumn('Width',100);allowMultiSelection = true;items = {'One','Label1'; 'Two','Label2'; 'Three','Label3'; 'Four','Label4'; 'Five','Label5'};hListBox = ListBox(items, allowMultiSelection);hListBox.Value = {'One'; 'Three'};hListBox.ValueChangedFcn = @ValueChangedCallback;hColumn.add(hListBox); ![]() The DropDown and ListBox controls are nearly identical in terms of their properties, methods and events/callbacks, with the following notable exceptions:
![]() function hPopup = createPopup()import matlab.ui.internal.toolstrip.*hPopup = PopupList();% list header #1header = PopupListHeader('List Items');hPopup.add(header);% list item #1item = ListItem('This is item 1', Icon.MATLAB_16);item.Description = 'this is the description for item #1';item.ShowDescription = true;item.ItemPushedFcn = @ActionPerformedCallback;hPopup.add(item);% list item #2item = ListItem('This is item 2', Icon.SIMULINK_16);item.Description = 'this is the description for item #2';item.ShowDescription = false;addlistener(item, 'ItemPushed', @ActionPerformedCallback);hPopup.add(item);% list header #2header = PopupListHeader('List Item with Checkboxes');hPopup.add(header);% list item with checkboxitem = ListItemWithCheckBox('This is item 3', true);item.ValueChangedFcn = @PropertyChangedCallback;hPopup.add(item);% list item with popupitem = ListItemWithPopup('This is item 4',Icon.ADD_16);item.ShowDescription = false;hPopup.add(item);% Sub-popuphSubPopup = PopupList();item.Popup = hSubPopup;% sub list item #1sub_item1 = ListItem('This is sub item 1', Icon.MATLAB_16);sub_item1.ShowDescription = false;sub_item1.ItemPushedFcn = @ActionPerformedCallback;hSubPopup.add(sub_item1);% sub list item #2sub_item2 = ListItem('This is sub item 2', Icon.SIMULINK_16);sub_item2.ShowDescription = false;sub_item2.ItemPushedFcn = @ActionPerformedCallback;hSubPopup.add(sub_item2);end % createPopup() We now have two alternatives for attaching this popup to the DropDownButton or SplitButton: ![]()
In summary:
Galleries Toolstrip galleries are panels of buttons (typically large icons with an attached text label), which are grouped in “categories”. The general idea is to first create the GalleryPopup object, then add to it a few GalleryCategory groups, each consisting of GalleryItem (push-buttons) and/or ToggleGalleryItem (toggle-buttons) objects. Once this GalleryPopup is created, we can either integrate it in-line within the toolstrip section (using Gallery), or as a compact drop-down button (using DropDownGalleryButton): % Inline gallerysection = hTab.addSection('Multiple Selection Gallery');column = section.addColumn();popup = GalleryPopup('ShowSelection',true);% add the GalleryPopup creation code (see next week's post)gallery = Gallery(popup, 'MaxColumnCount',4, 'MinColumnCount',2);column.add(gallery);% Drop-down gallerysection = hTab.addSection('Drop Down Gallery');column = section.addColumn();popup = GalleryPopup();% add the GalleryPopup creation code (see next week's post)button = DropDownGalleryButton(popup, 'Examples', Icon.MATLAB_24);button.MinColumnCount = 5;column.add(button); ![]() I initially planned to include all the relevant Gallery discussion here, but it turned out to require so much space that I decided to devote a separate article for it — this will be the topic of next week’s blog post. Toolstrip miniseries roadmap The next post will discuss Galleries in depth, followed by popup forms. Following that, I plan to discuss toolstrip collapsibility, the ToolPack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures – not necessarily in this order. Matlab toolstrips can be a bit complex, so I plan to proceed in small steps, each post building on top of its predecessors. If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, please let me know. Related posts:
更多... |
![]() |
![]() |