Effacer les filtres
Effacer les filtres

GUI Edit Text Box Value Check

8 vues (au cours des 30 derniers jours)
Jay
Jay le 14 Mar 2016
Commenté : Jay le 4 Avr 2016
During runtime I want my GUI to confirm there are values in 4 edit boxes before the user can continue filling out the 5th edit box.
The edit box values will be of numbers and not strings.
To do this I want to input code in the edit box 5 callback. Something like (pseudo-code):
if editbox1 & editbox2 & editbox3 & editbox4 isempty
listbox3 = 'All boxes in first section not populated'
else
end.
How do the edit boxes hold their values? I assume they are of the type string.
Do I need to create and array(vector), populate the array with values (string, number or Boolan value), convert said values in the array from string to numbers, check population and keep the values of type number or, can the if statement compare the 4 edit boxes in sequence to check population and use the values as type number?

Réponse acceptée

Jan
Jan le 14 Mar 2016
Modifié(e) : Jan le 19 Mar 2016
You can insert the check in the callbacks of the first 4 edit fields. The callbacks can check the validity of the contents at first:
% In the OpeningFcn: [EDITED, was CreateFcn]
handles.EditOk = false(1, 4);
set(handles.Edit5, 'Enable', 'off');
...
guidata(ObjectH, handles); % [EDITED] Store handles in the figure
function Edit1_Callback(ObjectH, EventData)
handles = guidata(ObjectH);
Str = get(ObjectH, 'String');
Value = sscanf(Str, '%f', 1);
if ~isempty(Value) && ~isnan(Value)
StrNew = sprintf('%.8g', Value); % In the wanted format
if ~strcmp(Str, StrNew)
set(ObjectH, 'String', StrNew);
% Perhaps a warning message?
end
handles.EditOk(1) = true;
else
handles.EditOk(1) = false;
end
if all(handles.EditOk)
set(handles.Edit5, 'Enable', 'on');
else
set(handles.Edit5, 'Enable', 'off');
end
guidata(ObjectH, handles); % [EDITED] Store handles in the figure
The same can be applied in all 4 edit fields - or you can use the same callback for all fields and some further trick to identify the index of the edit box.
Now the 5th edit field is activated only if the 4 edit boxes contain valid values.
  24 commentaires
Walter Roberson
Walter Roberson le 4 Avr 2016
No, the output function is not a callback. None of the callbacks can have outputs.
If you defined edit1_Callback to take only (hObject, eventdata) and you programmed this in GUIDE then you will get that error about too many input arguments, because GUIDE automatically adds code to fetch the handles structure and add it as the third parameter. Do not mix programming styles: if you are using GUIDE then use the GUIDE three-input style, and if you are coding your own callbacks, expect only two inputs unless you coded otherwise.
Jay
Jay le 4 Avr 2016
Thankyou Walter.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 18 Mar 2016
Maybe something like this (if using R2014b or later) (off the top of my head, untested)...
edit1Number = str2double(handles.edit1.String);
% Check for a non-empty edit box and a valid number (no letters or symbols).
if isempty(handles.edit1.String) || isnan(edit1Number)
% They didn't enter a number.
% They entered a character, symbols, or something else not allowed.
% Assign some default and notify the user.
edit1Number = defaultValue;
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
% Put it into the text box too, replacing their bad entry.
handles.edit1.String = num2str(edit1Number);
end
Repeat for validation of the other edit text boxes.
  4 commentaires
Image Analyst
Image Analyst le 19 Mar 2016
I used the syntax from the last several versions. If you have an older version, you need to use the syntax Jan gave above.
Jay
Jay le 20 Mar 2016
Ok. I appreciate the explanations the both of you have provided to help me better understand what the commands are actually doing and how they interact with one another.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by