I really don't know where to start with this, I want to create a form dynamically.
I have a section on the form that has 3 combobox and 2 buttons. What I want is when the user clicks on the '+' button it add another 3 combobox and 2 buttons below the first, and so on and so on. Also when the user clicks the '-' button I want to remove the row of 3 combobox etc.
I have attached an image to try and illistrate further.
You should create a UserControl that contains that set of child controls. You can then add an instance of that UserControl in a single operation to add the entire set of child controls.
You should then add a TableLayoutPanel to your form. You can then simply add a new instance of your UC to that and it will grow appropriately. You can then resize your form by the same amount that the TLP resized itself. I will be something like:
vb.net Code:
Dim oldHeight = myTLP.Height
myTLP.Controls.Add(New SomeUserControl)
Me.Height += myTLP.Height - oldHeight
That's all there is to it. You can then get each UC by index from the TLP.
Another alternative, depending on the circumstances, might be to use the DataRepeater control.
Thanks for your help, I have got this working now, I do however have a couple of questions.
1) I am still unclear how I can tell if the user has clicked on the '-' button on the usercontrol. What I want to do is if the user clicks the '-' button then it removes the corosponding usercontrol.
2) I also need to get the value of all of the coboboxs on the various usercontrol, therefore I need to loop through the usercontrols on the form and get each value, not sure how to do this.
1. Handle the Button's Click event as normal. From there you have a couple of choices. You could either have the UC Remove itself from its Parent's Controls collection, or you could have the UC raise an event that the form can handle and it can Remove the UC.
2. Like all controls, the TLP has a Controls collection. You loop through that to get eacg child, i.e. your UCs.