How to add controls during runtime?

kco1122

Member
Joined
Mar 29, 2006
Messages
21
Programming Experience
5-10
I've searched and searched but can't see to find anything relating to this.. I'm creating a recipe style database and after creating the base design utilizing panels and such, I'm down to adding in the ingredients list. Since I don't want to jam the screen full of empty fields, I'm opting to let a user decide how many fields they need, at which point the app would add in 'x' controls.

To make things easier (I think), I created a custom user control that consists of 2 text boxes and 1 combo box.

I already know how to add in the custom control to the toolbox and how to utilize it during the design time.. but I'm not sure how I can add 'x', let along 1 occurence during runtime.

I tried something like this:

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] test [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] UserControl
test = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IngredientFieldEntry
test.CreateControl()
test.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(391, 20)
test.Size = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(100, 100)
test.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]test.BringToFront()
[/SIZE]

Where IngredientFieldEntry is the name of the custom control. I thought that perhaps it was hiding behind the panels or hiding behind the groupbox that I am putting the ingredient controls into but it doesn't seem to be the case.. can anyone give me a hand as to what I'm doing wrong?

Oh.. and when it does get added to the form, how does it know to sit within the groupbox that I had setup for it? Can't figure that out either..

Thank you.
 
Oh tell us... you added it to the Controls collection of the Groupbox? Ex: Groupbox1.Controls.Add(test)
 
Back
Top