Why can't I copy values from an editable textbox?
I have a GUI where some values are displayed in an editable textbox. For some reason I cannot copy those values with the mouse. I ca开发者_StackOverflown select the text, but no drop-down menu appears when I right-click on the selected text. I have been looking all over the place. What am I missing?
You should implement the context menu yourself, if you need one, by using uicontextmenu
uicontrol, and adding items to it using uimenu
. See here: http://www.mathworks.com/help/techdoc/ref/uicontextmenu.html
It's true that editable text boxes don't bring up a context menu by default when you right click, but there are a few ways around this if you're wanting to copy text to the clipboard:
As Mikhail mentions in his comment, you can still highlight the text and press Ctrl + C to copy it to the clipboard.
As Itamar mentions in his answer, you can create your own context menu for the editable text box using the functions UICONTEXTMENU and UIMENU. Here's a sample implementation that uses the function CLIPBOARD to add the editable text string to the clipboard:
hFigure = figure; %# Create a figure hEdit = uicontrol(hFigure,'Style','edit',... %# Create an editable text box 'String','Enter your name here',... 'Position',[30 50 130 20]); hCMenu = uicontextmenu; %# Create a context menu uimenu(hCMenu,'Label','Copy',... %# Create a menu item 'Callback',@(hObject,eventData) clipboard('copy',get(hEdit,'String'))); set(hEdit,'UIContextMenu',hCMenu); %# Add context menu to control
Now you can right click on the control to bring up a menu with one option: "Copy". Note that by selecting this menu item it will copy the editable text string to the clipboard without having to highlight the text first.
You can set the
'ButtonDownFcn'
property for your editable text box so that a right click on the control will automatically copy the text string to the clipboard without having to highlight the text or select a menu item. First you would have to save this m-file function to the path:function right_click_copy(hObject,eventData) hFigure = get(hObject,'Parent'); %# Get the parent object while ~strcmp(get(hFigure,'Type'),'figure') %# Loop until it is a figure hFigure = get(hFigure,'Parent'); %# Keep getting the parents end if strcmp(get(hFigure,'SelectionType'),'alt') %# Check for a right click clipboard('copy',get(hObject,'String')); %# Copy the object string to %# the clipboard end end
This function uses the
'SelectionType'
property of the parent figure to check which mouse button was pressed and the CLIPBOARD function to copy the object string to the clipboard. Now you can create your editable text control as follows:hFigure = figure; %# Create a figure hEdit = uicontrol(hFigure,'Style','edit',... %# Create an editable text box 'String','Enter your name here',... 'Position',[30 50 130 20],... 'ButtonDownFcn',@right_click_copy);
This is the quickest and easiest option of the three, since it involves only a single mouse click to copy the editable text string to the clipboard.
Did you just want to make the editable textbox 'Enable'?
set(handles.editbox1,'Enable','on');
(assuming that you have the 'handle' to that GUI object.)
This should make the edit box editable.
精彩评论