MATLAB Answers

How to remove displayed edit boxes when the user changes the number of inputs in GUI?

16 ビュー (過去 30 日間)
Ahmed Abed
Ahmed Abed 2019 年 9 月 25 日
コメント済み: Ahmed Abed 2019 年 9 月 26 日
Hello everyone. I am designing a gui that generates a variable number of edit boxs that will be used for further analysis. First, the user must select number of layers to be analysed and based on that, the required number of edit boxes appears. The number of layers is between 3 - 10; when selecting three layers for example, the result is as shown in image im01; when changing the number of layers to 8 for example, the results is as shown in image im02. However, if the user changes his mind at this stage and changes the number of layers again to a lower number than the last one, such as 4, then the results is as shown in image im03. The previously displayed edit and static text boxes stay displayed in addition to the new ones. So I need help in removing everything that was displayed based on the previous selection and displaying what was selected in the last step only.
Thank you in advance, your support is greatly appreciated!
The code I used to generate the edit and static boxes is as follows
function NumberOfLayers_Callback(hObject, eventdata, handles)
% hObject handle to NumberOfLayers (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns NumberOfLayers contents as cell array
% contents{get(hObject,'Value')} returns selected item from NumberOfLayers
NumberOfLayers=get(hObject,'value');
N = NumberOfLayers+2;
TH='Input 1';
MO='Input 2';
PR='Input 3';
for K = 1 : N
ypos = (N-K+1)*30;
handles.static1(K) = uicontrol('Style', 'text', 'String', sprintf('Layer %#d', K), 'Units', 'Pixels', 'Position', [0 ypos 50 30]);
if K<N
handles.edit1(K) = uicontrol('Style', 'edit', 'String', '', 'Max', 2, 'Units', 'Pixels', 'Position', [50 ypos+5 200 30]);
else
handles.static2(K) = uicontrol('Style', 'text', 'String', sprintf('XXX %#d', ''), 'Units', 'Pixels', 'Position', [50 ypos+5 180 20]);
end
handles.edit2(K) = uicontrol('Style', 'edit', 'String', '', 'Max', 2, 'Units', 'Pixels', 'Position', [250 ypos+5 200 30]);
handles.edit3(K) = uicontrol('Style', 'edit', 'String', '', 'Max', 2, 'Units', 'Pixels', 'Position', [450 ypos+5 200 30]);
end
h_static2 = uicontrol('Style', 'text', 'String', sprintf('%s %s %s', TH, MO, PR), 'Units', 'Pixels', 'Position', [50 (N*30+30) 600 40]);

  2 件のコメント

Adam
Adam 2019 年 9 月 26 日
If you always want to just start the whole thing again just call
delete( handles.edit2 )
before recreating them, or equivalent on whichever components you want to remove. I'm not convinced keep recreating everything is the best option, but since that is what you seem to be doing this looks like the simplest change to make.
Ahmed Abed
Ahmed Abed 2019 年 9 月 26 日
Thank you for your help Adam.

サインインしてコメントする。

採用された回答

Murugan C
Murugan C 2019 年 9 月 26 日
You should delete handles information which you were created.
Please use below code. in 'NumberOfLayers_Callback'.
NumberOfLayers=get(hObject,'value');
N = NumberOfLayers+2;
TH='Input 1';
MO='Input 2';
PR='Input 3';
try
delete(handles.static1(1:end));
delete(handles.edit1(1:end));
delete(handles.edit2(1:end));
delete(handles.edit3(1:end));
delete(handles.static2(1:end));
delete(handles.h_static2);
catch
disp('Create New Static Text box and Edit box');
end
for K = 1 : N
ypos = (N-K+1)*30;
handles.static1(K) = uicontrol('Style', 'text', 'String', sprintf('Layer %#d', K), 'Units', 'Pixels', 'Position', [0 ypos 50 30]);
if K<N
handles.edit1(K) = uicontrol('Style', 'edit', 'String', '', 'Max', 2, 'Units', 'Pixels', 'Position', [50 ypos+5 200 30]);
else
handles.static2(K) = uicontrol('Style', 'text', 'String', sprintf('XXX %#d', ''), 'Units', 'Pixels', 'Position', [50 ypos+5 180 20]);
end
handles.edit2(K) = uicontrol('Style', 'edit', 'String', '', 'Max', 2, 'Units', 'Pixels', 'Position', [250 ypos+5 200 30]);
handles.edit3(K) = uicontrol('Style', 'edit', 'String', '', 'Max', 2, 'Units', 'Pixels', 'Position', [450 ypos+5 200 30]);
end
handles.h_static2 = uicontrol('Style', 'text', 'String', sprintf('%s %s %s', TH, MO, PR), 'Units', 'Pixels', 'Position', [50 (N*30+30) 600 40]);
guidata(hObject,handles);

  1 件のコメント

Ahmed Abed
Ahmed Abed 2019 年 9 月 26 日
This solution worked very well, thank you very much for your help

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by