Results 1 to 4 of 4

Thread: [2005] How to add array of controls programmatically

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Location
    Bahrain
    Posts
    43

    [2005] How to add array of controls programmatically

    Hello Everybody ,

    If any one can help me in this simple question I will be very grateful.

    Let say we have a textbox and a button in a form. I want If I write 3 in the textbox and click ok then an array of 3 buttons (or any controls) will apear in the form.
    if I write 2 then array of 2 buttons will appear and so on.

    Thank you for your time.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] How to add array of controls programmatically

    The tricky part of creating controls dynamically is to set its location.... At run time, you'll never know how many controls will be created. Supposed your user enter 1000 in the textbox, you can easily create 1000 buttons but to fit them all in your form will be a problem. So a work around for this is to create controls and place them on a panel which has the scrolling ability.

  3. #3
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] How to add array of controls programmatically

    Have you searched the forums for the answer. There are loads and loads of examples of dynamically creating controls.

    As for how many to create you could read the number from the text box
    VB Code:
    1. Dim numControls As Integer = Cint(textBox.Text)
    2.  
    3. For i As Integer = 0 To numControls
    4.  
    5. 'Place you control creation and adding code here
    6.  
    7.  
    8. Next

    Also, the point made by Stanav is very good. Too many and you run into problems.

    You could dictate a certain number per row by updating the x location and then update both the y and x location points when you want a new row of the controls for examples. Scrolling is a good option.
    Last edited by stimbo; Nov 10th, 2006 at 08:24 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4

    Thread Starter
    Member
    Join Date
    May 2006
    Location
    Bahrain
    Posts
    43

    Re: [2005] How to add array of controls programmatically

    first thanks you all,

    This is just simple program, I want only take the idea of dynamic creating controls and
    view it in the form at specific location.

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