Copy/Paste from ListBox at runtime

조회 수: 2 (최근 30일)
Cristian Berceanu
Cristian Berceanu 2024년 12월 9일
댓글: Adam Danz 2024년 12월 9일
Hello,
Is there a way to interactively select the entries in an AppDesigner ListBox and then copy them to a different application? Right-clicking on the elements in the list does not pop-up any context menu and also CTRL-C and CTRL-V do not seem to work...
Regards,
Cristian

채택된 답변

Adam Danz
Adam Danz 2024년 12월 9일
편집: Adam Danz 2024년 12월 9일
Add a right-click "copy" option to ListBox
Create a context menu for the list box and set its MenuSelectedFcn to use clipboard.
For single-selection list boxes
fig = uifigure;
lb = uilistbox(fig,"Items",["Rhodope","Rila","Pirin"]);
lb.ContextMenu = uicontextmenu(fig);
m1 = uimenu(lb.ContextMenu ,'Text','Copy','MenuSelectedFcn',@(~,menuData)clipboard('copy',string(menuData.ContextObject.Value)));
For multiselect list boxes
m1 = uimenu(lb.ContextMenu ,'Text','Copy','MenuSelectedFcn',@(~,menuData)clipboard('copy',strjoin(menuData.ContextObject.Value,newline)));
  댓글 수: 1
Adam Danz
Adam Danz 2024년 12월 9일
> I hoped it would be just some option I could tick in the configuration options of the ListBox.
There is no such option but if you are applying this to many list boxes, you can wrap it into a function that you can easily apply to list boxes.
fig = uifigure;
lb = uilistbox(fig,"Items",["Rhodope","Rila","Pirin"]);
% Add copy option
applyCopyOption(lb)
% Add this function to your app
function applyCopyOption(listboxHandle)
fig = ancestor(listboxHandle,'figure');
listboxHandle.ContextMenu = uicontextmenu(fig);
uimenu(listboxHandle.ContextMenu ,'Text','Copy','MenuSelectedFcn',@(~,menuData)clipboard('copy',string(menuData.ContextObject.Value)));
% For multiselect listboxes:
% uimenu(lb.ContextMenu ,'Text','Copy','MenuSelectedFcn',@(~,menuData)clipboard('copy',strjoin(menuData.ContextObject.Value,newline)));
end

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

추가 답변 (2개)

Voss
Voss 2024년 12월 9일
You can use the clipboard function.
You'd have to implement any context menu or ctrl+c behavior.
An example using a context menu:
f = uifigure();
lb = uilistbox(f,'MultiSelect','on');
cm = uicontextmenu(f);
uimenu(cm,'Text','Copy','MenuSelectedFcn',@(~,~)clipboard('copy',mat2str(string(lb.Value))));
lb.ContextMenu = cm;
To have ctrl+c do the copying, you can set the uifigure's KeyPressFcn or WindowKeyPressFcn. See more: uifigure properties

Cristian Berceanu
Cristian Berceanu 2024년 12월 9일
Thank you for clarifying! I hoped it would be just some option I could tick in the configuration options of the ListBox. Seems it's not.
Regards, Cristian

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by