dewalla
dewalla

Reputation: 1327

Edit box clearing on mouse click in 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?

Upvotes: 4

Views: 10844

Answers (4)

Randell
Randell

Reputation: 1

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.

Upvotes: -2

Hooplator15
Hooplator15

Reputation: 1550

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!

Upvotes: 1

Yib
Yib

Reputation: 11

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

Upvotes: 1

Chris
Chris

Reputation: 46316

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

Upvotes: 4

Related Questions