Why can't I copy values ​​from an editable text field?

I have a GUI where some values ​​are displayed in an editable text box . For some reason, I cannot copy these values ​​with the mouse. I can select the text, but no drop-down menu appears when I right-click on the selected text. I searched everywhere. What am I missing?

+3
source share
3 answers

It is true that editable text fields do not by default invoke a context menu when you right-click, but there are several ways to get around this if you want to copy text to the clipboard:

  • As Mikhail mentions in his comment, you can still select the text and press Ctrl+ Cto copy it to the clipboard.

  • As Itamar mentions in his answer , you can create your own context menu for an editable text field using the UICONTEXTMENU and UIMENU functions . Here's an example implementation that uses the CLIPBOARD function to add an 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
    

    , : "". , , , , .

  • 'ButtonDownFcn' , , . m :

    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
    

    'SelectionType' , , , 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],...
                      'ButtonDownFcn',@right_click_copy);
    

    , , .

+2

, , uicontextmenu uicontrol uimenu. . : http://www.mathworks.com/help/techdoc/ref/uicontextmenu.html

+3

"Enable"?

(handles.editbox1, 'Enable', 'on');

(assuming you have a handle to this GUI object.)

This should make the editable field editable.

-1
source

Source: https://habr.com/ru/post/1779977/


All Articles