2010-12-13 75 views
3

我有一個GUI,其中一些值顯示在editable textbox中。出於某種原因,我無法用鼠標複製這些值。我可以選擇文本,但是當我右鍵單擊所選文本時,不會顯示下拉菜單。我一直在尋找各地。我錯過了什麼?爲什麼我無法從可編輯文本框中複製值?

+2

它被稱爲上下文菜單,你是對的 - 默認情況下沒有在MTALAB中。但是你仍然可以使用Ctrl + C – Mikhail 2010-12-13 10:51:13

回答

2

確實,可編輯的文本框在默認情況下不會顯示右鍵菜單,但是如果您想將文本複製到剪貼板,可以使用幾種方法:

  1. 由於Mikhail提到了他的意見,你仍然可以突出顯示文本,然後按Ctrl鍵+ç它複製到剪貼板。

  2. 作爲Itamar mentions in his answer,您可以使用函數UICONTEXTMENUUIMENU爲可編輯文本框創建自己的上下文菜單。下面是一個使用功能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 
    

    現在你可以右鍵點擊控制,彈出一個菜單,其中一個選項:「複製」。請注意,通過選擇此菜單項,它將把可編輯的文本字符串複製到剪貼板,而不必先突出顯示文本。

  3. 您可以爲您的可編輯文本框設置'ButtonDownFcn' property,以便右鍵單擊控件將自動將文本字符串複製到剪貼板,而不必突出顯示文本或選擇菜單項。首先,你將不得不此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' property檢查哪個鼠標按鈕被按下,並且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); 
    

    這是三個最快捷,最容易的選擇,因爲它涉及到只有一個鼠標點擊可編輯的文本字符串複製到剪貼板。

-1

你只是想讓可編輯文本框'啓用'?

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

(假設你有「把手」到GUI對象。)

這應該使編輯框中編輯。

相關問題