在对象之间交换属性值

发布于 2024-11-19 23:46:44 字数 214 浏览 3 评论 0 原文

我通过图中的注释(.)创建了两个文本框。它们的大部分属性已经被定义;回调函数可以在窗口中进行拖放操作。我为盒子创建了一个 uicontextmenu。右键单击可以选择功能列表以进行后续操作。

我尝试添加的操作之一涉及在两个框之间交换字符串。我需要获取当前右键单击的框中的字符串,该字符串应与我随后左键单击的框中的字符串交换。我可以获得有关如何扩展 uimenu 功能以便它注册后续左键单击的建议吗?

I have created two textboxes via annotation(.) in a figure. Most of their properties have been defined; and the callback function enables drag and drop motion in the window. I created a uicontextmenu for the boxes. On right click a list of functions can be chosen from for subsequent action.

One of the actions I am trying to add involves swapping strings between the two boxes. I need to get the string of the box I currently right-clicked, which should swap with the string in the box I subsequently left-click. Can I get advice on how to go about extending the uimenu function so that it registers the subsequent left-click?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪 2024-11-26 23:46:44

您将需要手动存储最后单击的框。如果您使用 GUIDE 来设计 GUI,请使用传递给回调函数的 handles 结构。否则,如果您以编程方式生成组件,则嵌套回调函数可以访问其封闭函数内定义的变量。

编辑

这是一个完整的示例:右键单击并从上下文菜单中选择“交换”,然后选择另一个文本框来交换字符串(左键单击)。请注意,我必须禁用/启用两个步骤之间的文本框才能触发 ButtonDownFcn (请参阅此 页面 进行解释)

function myTestGUI
    %# create GUI
    hLastBox = [];          %# handle to textbox initiating swap
    isFirstTime = true;     %# show message box only once
    h(1) = uicontrol('style','edit', 'string','1', 'position',[100 200 60 20]);
    h(2) = uicontrol('style','edit', 'string','2', 'position',[400 200 60 20]);
    h(3) = uicontrol('style','edit', 'string','3', 'position',[250 300 60 20]);
    h(4) = uicontrol('style','edit', 'string','4', 'position',[250 100 60 20]);

    %# create context menu and attach to textboxes
    hCMenu = uicontextmenu;
    uimenu(hCMenu, 'Label','Swap String...', 'Callback', @swapBeginCallback);
    set(h, 'uicontextmenu',hCMenu)

    function swapBeginCallback(hObj,ev)
        %# save the handle of the textbox we right clicked on
        hLastBox = gco;

        %# we must disable textboxes to be able to fire the ButtonDownFcn
        set(h, 'ButtonDownFcn',@swapEndCallback)
        set(h, 'Enable','off')

        %# show instruction to user
        if isFirstTime
            isFirstTime = false;
            msgbox('Now select textbox you want to switch string with');
        end
    end
    function swapEndCallback(hObj,ev)
        %# re-enable textboxes, and reset ButtonDownFcn handler
        set(h, 'Enable','on')
        set(h, 'ButtonDownFcn',[])

        %# swap strings
        str = get(gcbo,'String');
        set(gcbo, 'String',get(hLastBox,'String'))
        set(hLastBox, 'String',str)
    end
end

You will need to manually store the last clicked box. If you are using GUIDE to design your GUI, use the handles structure which gets passed around to callback functions. Otherwise if you programmatically generate the components, then nested callback functions have access to variables defined inside their enclosing functions.

EDIT

Here is a complete example: right-click and select "Swap" from context menu, then choose the other textbox to swap strings with (left-click). Note that I had to disable/enable the textboxes in-between the two steps to be able to fire the ButtonDownFcn (see this page for an explanation)

function myTestGUI
    %# create GUI
    hLastBox = [];          %# handle to textbox initiating swap
    isFirstTime = true;     %# show message box only once
    h(1) = uicontrol('style','edit', 'string','1', 'position',[100 200 60 20]);
    h(2) = uicontrol('style','edit', 'string','2', 'position',[400 200 60 20]);
    h(3) = uicontrol('style','edit', 'string','3', 'position',[250 300 60 20]);
    h(4) = uicontrol('style','edit', 'string','4', 'position',[250 100 60 20]);

    %# create context menu and attach to textboxes
    hCMenu = uicontextmenu;
    uimenu(hCMenu, 'Label','Swap String...', 'Callback', @swapBeginCallback);
    set(h, 'uicontextmenu',hCMenu)

    function swapBeginCallback(hObj,ev)
        %# save the handle of the textbox we right clicked on
        hLastBox = gco;

        %# we must disable textboxes to be able to fire the ButtonDownFcn
        set(h, 'ButtonDownFcn',@swapEndCallback)
        set(h, 'Enable','off')

        %# show instruction to user
        if isFirstTime
            isFirstTime = false;
            msgbox('Now select textbox you want to switch string with');
        end
    end
    function swapEndCallback(hObj,ev)
        %# re-enable textboxes, and reset ButtonDownFcn handler
        set(h, 'Enable','on')
        set(h, 'ButtonDownFcn',[])

        %# swap strings
        str = get(gcbo,'String');
        set(gcbo, 'String',get(hLastBox,'String'))
        set(hLastBox, 'String',str)
    end
end

screenshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文