Removing custom control from form

kco1122

Member
Joined
Mar 29, 2006
Messages
21
Programming Experience
5-10
Hi.. I created a custom control made up of several text boxes and other controls to be used in a recipe program (the control stores ingredient info).

Since I don't know how many ingredients a recipe needs, I give the user the option to enter the number of ingredients they need and I then add in additional custom controls as needed using:

VB.NET:
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CreateRecipeInstructionFields([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] iFields)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] iCount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ctrlRecInstructEntry [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] RecipeInstructionEntry
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ToolStripProgressBar1.Value = iFields - 1
[/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] iCount < iFields
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ToolStripProgressBar1.Increment(1)
ctrlRecInstructEntry = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] RecipeInstructionEntry
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].grpRecInstuctLst.Controls.Add(ctrlRecInstructEntry)
ctrlRecInstructEntry.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]ctrlRecInstructEntry.Name = [/SIZE][SIZE=2][COLOR=#800000]"RecipeInstructionEntry"[/COLOR][/SIZE][SIZE=2] & iTotalRecInstructCtrls
ctrlRecInstructEntry.lblRecipeEntry.Text = iTotalRecInstructCtrls + 1 & [/SIZE][SIZE=2][COLOR=#800000]")"
[/COLOR][/SIZE][SIZE=2]ctrlRecInstructEntry.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(RecipeInstruction_XLoc, (YRecInstructSize + YCtrlSpace) + iLastRecInstructCtrlCreated_YLoc)
iLastRecInstructCtrlCreated_YLoc = (YRecInstructSize + YCtrlSpace) + iLastRecInstructCtrlCreated_YLoc
ctrlRecInstructEntry.BringToFront()
ctrlRecInstructEntry.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]iTotalRecInstructCtrls += 1
iCount += 1
ctrlRecInstructEntry = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Loop
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ToolStripProgressBar1.Value = 0
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

This works fine.. works great actually.. what I'm trying to figure out now is..

If I suddenly need to remove one of these controls -OR- they cancel the entry and I need to revert the form back to its original setting with only 1 control, how do I remove the excess?

I tried things like
VB.NET:
[SIZE=2]
ctrlRecInstructEntry = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] RecipeInstructionEntry
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].grpRecInstuctLst.Controls.Remove(ctrlRecInstructEntry)
[/SIZE]

Or even doing the remove with the name in a string (since the names are dynamically generated I can't refer to them specifically during program time).

Any suggestions on how I can do this? And although i know I could prob clear out the entire form and create the form from scratch during runtime, I'm trying to avoid that.

-Ko
 
Remove a control from ControlCollection with Remove(instance) or RemoveAt(index) or Clear() methods.

Not sure if you meant the second code example literally? If you create a new instance you can't remove it before even having added it to the Controls collection.

To remove by instance you first get the instance either from anywhere you stored a reference to it (which I see you don't) or by first search it up by name from all controls. In your case that could be with help of the external iTotalRecInstructCtrls counter used, or simpler by using RemoveAt with the index (.Controls.Count-1) to remove the last item added.
 
Thanks for gettin back to me... you mentioned the following:

To remove by instance you first get the instance either from anywhere you stored a reference to it (which I see you don't)

How would I store a reference to the control I just created? Could you give me an example of that?

In the meantime I'll try the idea of searching all controls for what I need.. that would probably work also but I guess that's probably not the most resource-friendly way to do it.

Thanks.
 
Storing is done in a variable as long as it is in scope. When you declare a local variable in that Sub it goes out of scope when End Sub. The control instance itself which is added to the grpRecInstuctLst.Controls collection doesn't go out of scope, but the variable that held the reference was lost. If all there is in your grpRecInstuctLst.Controls is a number of RecipeInstructionEntry then there is no point in declaring another global collection of these.
 
Back
Top