GUI Edit Text Box Value Check
5 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Jan
2016-3-14
编辑:Jan
2016-3-19
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 个评论
Walter Roberson
2016-4-4
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.
更多回答(1 个)
Image Analyst
2016-3-18
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 个评论
Image Analyst
2016-3-19
I used the syntax from the last several versions. If you have an older version, you need to use the syntax Jan gave above.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!