How to remove displayed edit boxes when the user changes the number of inputs in GUI?
1 view (last 30 days)
Show older comments
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 Comments
Adam
on 26 Sep 2019
Edited: Adam
on 26 Sep 2019
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.
Accepted Answer
Murugan C
on 26 Sep 2019
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);
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!