dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: creating a specified number of controls

  1. #1
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125

    creating a specified number of controls

    Hello,

    I would like to create a number of controls ( System.Windows.Forms.Form ) ,
    but not a fixed number..
    I'm asking the user in howmuch 'groups' they would like to divide there data, then I would like to create the same number of textboxes so that they can fill them in.

    Is there any way I can create as much as the user enters?
    Tried something like this:
    Code:
    For y = 1 To number Step 1
    Me.Label(y) = New System.Windows.Forms.Label
    But that obviously doesn't work..

    Thx in advance !
    Bert Willekens,

  2. #2
    Join Date
    Jan 2006
    Posts
    293

    Re: creating a specified number of controls

    You have to add the control to the controls collection of the form in order for it to show. An example of creating 5 textboxes is below...
    Code:
            For Y As Integer = 0 To 4
                'create a new textbox
                Dim MyTextBox As New TextBox
                'modify some of the properties
                MyTextBox.Width = 50
                'position the controls however you want
                MyTextBox.Left = Y * 50
                'add them to the controls collection
                Me.Controls.Add(MyTextBox)
            Next

  3. #3
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125

    Re: creating a specified number of controls

    Thank You! This works like a charm!

    But it brings me to another problem.. How are they named?
    How can I get data out them?
    Bert Willekens,

  4. #4
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125

    Re: creating a specified number of controls

    Thank You! This works like a charm!
    Bert Willekens,

  5. #5
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125

    Re: creating a specified number of controls

    Hm, having another problem..

    I drew 3 textboxes, p0, p1, p2,

    But now I want to get the data out if it..
    Get the error that 'p0 is not a member of ..form1' because it 'isn't there yet'..
    Is there any way to solve this ?
    Bert Willekens,

  6. #6
    Join Date
    Jan 2006
    Posts
    293

    Re: creating a specified number of controls

    If you want to refer to the controls in an array, then you just add the references to an array when you add them to the controls collection. Just have a seperate class level array that contains your added textboxes. That way you can just refer to them by index elsewhere on your form...

  7. #7
    Join Date
    Sep 2006
    Posts
    17

    Re: creating a specified number of controls

    I have the same kind of question, I have created the controls, in my case labels, and can access them with the click routine that I set when I added the handler but can't figure a way to access what I put in them outside of that event.

    gigemboy,
    Can you post some source code for how you tie the referances to that array so I can access them?

    Thanks

    Howard

  8. #8
    Join Date
    Jan 2006
    Posts
    293

    Re: creating a specified number of controls

    You could set a class level textbox array, and just add the references to that array when you add the controls, like below:
    Code:
    'class level, outside your subs, to hold references of added textboxes
            
            Private MyTextBoxes(4) As TextBox
    
    'in a sub
    
            For Y As Integer = 0 To 4
                'create a new textbox
                Dim MyTextBox As New TextBox
                'modify some of the properties
                MyTextBox.Width = 50
                'position the controls however you want
                MyTextBox.Left = Y * 50
                'add them to the controls collection
                Me.Controls.Add(MyTextBox)
                'add references to array so we can access them by index
                MyTextBoxes(Y) = MyTextBox
            Next
    
    'now you should be able to access the textboxes by index...
            
            MyTextBoxes(0).Text = "this"
            MyTextBoxes(1).Text = "is"
            MyTextBoxes(2).Text = "a"
            MyTextBoxes(3).Text = "test"
            MyTextBoxes(4).Text = "!"

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)