How to add and remove edit boxes dynamically in gui and also how to obtain particular handle of that editbox

15 views (last 30 days)
I want to add dynamically edit boxes by pressing a button and remove particular edit box by pressing another button.Also I want to access the string entered in particular edit box and after removing the particular(it might be first,last or middle) edit box, all the edit boxes should align itself in order.I was able to create number of edit boxes dynamically using uicontrol but cannot differentiate 'tag' name and was unable to delete particular edit box. I can't use method like setting 'visibility off' and 'on' because user can enter n number of edit boxes.

Accepted Answer

Joseph Cheng
Joseph Cheng on 8 Jan 2015
well you can get away from tags and directly work with the handle object. for my example here lets say you have a GUIDE gui with 2 buttons (add and delete) and an edit box to choose which user created edit box you want to delete. We start off by creating some user defined areas for where the user's edit boxes can go in the OpeningFcn for the GUI.
handles.userEditpos =zeros(6,4);
handles.userEditpos(:,1)=51;
handles.userEditpos(:,2)=[320;289;259;231;200;170];
handles.userEditpos(:,3)=49;
handles.userEditpos(:,4)=20;
handles.userEdit=[];
So as you can see i limit the edit boxes to 6 which can be modified to generate based off of the last one. But for simplicity sake i hard coded the positions. the two variables i created are usereditpos as a look up table, and userEdit which will be the uicontrol handle objects. normally in GUIDE you would use the tags but the tags are just predefined handle objects created through the GUIDE layout and stored in the structure handles. later (below) i'll be storing the uicontrol in the handles structure but in a matrix handles.userEdit.
In the add edit button callback you'd have something like this to create the edit box.
numofEdits=length(handles.userEdit);
handles.userEdit(numofEdits+1)=uicontrol('style','edit','position',handles.userEditpos(numofEdits+1,:));
guidata(hObject, handles);
and use
toDel = str2num(cell2mat(get(handles.edit1,'String')));
delete(handles.userEdit(toDel))
in the delete button to delete the handles entry. I left out a portion of the code as i don't have the time to type it out to reorder and position the edit box entry, position and locations inside handles.userEdit but this should be enough for you to see what you can implement in your code.
  7 Comments
Stephania Vaglica
Stephania Vaglica on 8 Nov 2016
Edited: Stephania Vaglica on 8 Nov 2016
I did a weird sort of work around. I created a slider as a uicontrol and set it to invisible. In the GUI opening function, I set the slider value to zero. Then, every time the add button was executed, I would add+1 to the value of the slider. Every time the delete button was executed, I would subtract 1 from the value of the slider. I then used the slider's position value instead of numofedits to control the length of the userEdit vector.
So my add button looks like this:
pos=get(handles.slider1,'value');
handles.userEdit(pos+1)=uicontrol('style','edit','position',handles.userEditpos(pos+1,:))
set(handles.slider2,'value',pos+1);
guidata(hObject, handles);
My delete function looks like this:
pos=get(handles.slider2,'value');
delete(handles.userEdit(pos));
set(handles.slider2,'value',pos-1);
guidata(hObject, handles);
If you choose to go this route, keep in mind that you should change your min and max value of the slider to however many edit boxes you anticipate. Then, I recommend setting the visibility to off so that people cannot interact with the component in your GUI

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!