Edit box clearing on mouse click in MATLAB GUI

dewalla picture dewalla · Jan 5, 2012 · Viewed 10.5k times · Source

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?

Answer

Chris picture Chris · Jan 5, 2012

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