Results 1 to 4 of 4

Thread: Dynamic form build...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Dynamic form build...

    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.

    Thanks for the help

    Simon
    Attached Images Attached Images  

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Dynamic form build...

    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:
    1. Dim oldHeight = myTLP.Height
    2.  
    3. myTLP.Controls.Add(New SomeUserControl)
    4. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Re: Dynamic form build...

    Hi jmcilhinney,

    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.

    Any help would be really apreshiated

    Thanks

    Simon

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Dynamic form build...

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width