4

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?

dewalla
  • 1,317
  • 8
  • 18
  • 42

4 Answers4

4

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
Chris
  • 44,602
  • 16
  • 137
  • 156
  • Well having to click twice kind of defeats the purpose. Do you know how to get the text highlighted when you click in the edit box? – dewalla Jan 05 '12 at 16:37
  • Thanks but it seems as if it can't be done easily. see answer below – dewalla Jan 05 '12 at 16:53
1

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

Yib
  • 11
  • 1
1

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!

Edward
  • 3,292
  • 1
  • 27
  • 38
Hooplator15
  • 1,540
  • 7
  • 31
  • 58
-2

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.

Randell
  • 1
  • 1