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

조회 수: 5 (최근 30일)
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일
편집: 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.

댓글을 달려면 로그인하십시오.

채택된 답변

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);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by