How to remove displayed edit boxes when the user changes the number of inputs in GUI?
Mostra commenti meno recenti
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 Commenti
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
il 26 Set 2019
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Images in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!