在 MATLAB GUI 中单击鼠标即可清除编辑框

发布于 2024-12-24 22:27:34 字数 108 浏览 2 评论 0原文

我想在 MATLAB GUI 中有一个“编辑”框,上面写着“在此处键入搜索”。 当用户在框内单击时,我希望“此处类型搜索”消失,并为用户提供一个空的编辑框以开始输入...

有什么想法吗?

I want to have an "edit" box in a MATLAB GUI that says "TYPE SEARCH HERE".
When the user clicks inside the box I want the "TYPE SEARCH HERE" to disappear and give the user an empty edit box to start typing in...

Any ideas?

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

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

发布评论

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

评论(4

女皇必胜 2024-12-31 22:27:34

至少在我的系统上,当我使用以下代码设置用户输入框/窗口时,

prompt    = 'Enter search terms:';
dlg_title = 'My input box';
num_lines = 1;
defAns    = {'TYPE_SERACH_HERE'};

answer = inputdlg(prompt, dlg_title, num_lines, defAns);

默认文本 TYPE_SEARCH_HERE 会突出显示,因此我可以开始输入以将其替换为我想要的内容。

编辑 或者,如果您有现有的 uicontrol 编辑框,您可以执行如下操作:

function hedit = drawbox()

  hedit = uicontrol('Style', 'edit',...
      'String', 'deafult',...
      'Enable', 'inactive',...
      'Callback', @print_string,...
      'ButtonDownFcn', @clear);

end

function clear(hObj, event) %#ok<INUSD>

  set(hObj, 'String', '', 'Enable', 'on');
  uicontrol(hObj); % This activates the edit box and 
                   % places the cursor in the box,
                   % ready for user input.

end

function print_string(hObj, event) %#ok<INUSD>

  get(hObj, 'String')

end

At least on my system, when I use the follow code to set up a user input box/window

prompt    = 'Enter search terms:';
dlg_title = 'My input box';
num_lines = 1;
defAns    = {'TYPE_SERACH_HERE'};

answer = inputdlg(prompt, dlg_title, num_lines, defAns);

the default text TYPE_SEARCH_HERE appears highlighted, so I can just start typing to replace it with what ever I want.

Edit Alternatively, if you have an existing uicontrol edit box you could do something like the following:

function hedit = drawbox()

  hedit = uicontrol('Style', 'edit',...
      'String', 'deafult',...
      'Enable', 'inactive',...
      'Callback', @print_string,...
      'ButtonDownFcn', @clear);

end

function clear(hObj, event) %#ok<INUSD>

  set(hObj, 'String', '', 'Enable', 'on');
  uicontrol(hObj); % This activates the edit box and 
                   % places the cursor in the box,
                   % ready for user input.

end

function print_string(hObj, event) %#ok<INUSD>

  get(hObj, 'String')

end
梦醒时光 2024-12-31 22:27:34

Chris,您必须单击 uicontrol 边框才能发生 ButtonDownFcn。如果您在编辑框内单击,则不会发生这种情况

Chris, you've got to click in the uicontrol border to make the ButtonDownFcn happen. It won't happen if you click inside the edit box

半﹌身腐败 2024-12-31 22:27:34

好的,我已经找到了解决问题的方法,并且完美无缺!

然而,我很沮丧,因为我完全不知道它为什么起作用...

  1. 在 GUIDE 中创建一个编辑文本框,然后右键单击它以打开属性检查器。
  2. 将文本“TYPE TEXT HERE”添加到“string”属性
  3. 找到名为“Enable”的属性并将其切换为“inactive”
  4. 创建一个buttonDownFnc(也可以在属性检查器中完成)
  5. 使用以下代码:

    函数 myEditBoxTagGoesHere_ButtonDownFcn(hObject, eventdata, 句柄)

    % 将“启用”状态切换为 ON

    set(hObject, '启用', '开启');

    %创建UI控件

    uicontrol(handles.myEditBoxTagGoesHere);

如果有人能解释为什么 uicontrol 在单击鼠标左键时突出显示文本,那就太好了!

Okay, so I have a solution to the problem and it works flawlessly!!

However, I am quite upset because I have absolutely no idea WHY it works...

  1. Create an edit text box in GUIDE and right click on it to open up property inspector.
  2. add text "TYPE TEXT HERE" to "string" property
  3. Find the property nammed "Enable" and switch it to "inactive"
  4. Create a buttonDownFnc (can be done in property inspector also)
  5. Use following code:

    function myEditBoxTagGoesHere_ButtonDownFcn(hObject, eventdata, handles)

    % Toggel the "Enable" state to ON

    set(hObject, 'Enable', 'On');

    % Create UI control

    uicontrol(handles.myEditBoxTagGoesHere);

If someone could explain why uicontrol highlights the text upon left mouse click, that would be great!

老子叫无熙 2024-12-31 22:27:34

Hooplator15,它之所以有效,是因为当启用关闭时,编辑文本就像按钮:

  • 如果 Enable == 'on'(编辑文本启用),函数 _ButtonDownFcn 将在 5 像素边框内按下鼠标时执行;

  • 否则,它会在 5 像素边框内或编辑文本上方按下鼠标时执行,如按钮。

Hooplator15, it works because edit texts are like push buttons when Enable is Off:

  • If Enable == 'on' (edit text Enable), the function _ButtonDownFcn executes on mouse press in 5 pixel border;

  • Otherwise, it executes on mouse press in 5 pixel border or over edit text, like a button.

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