0

How can I use code similar to this to create multiple controls, I have been trying to figure this out for a while, but can't...

Dim newcontrol as control1 = New control1
Me.groupbox.controls.add(newcontrol)

Then add handlers, I want to name the controls like Visual Studio does it, newcontrol1, then newcontrol2, 3, etc... I don't know how many of these controls I will need, so it needs to add a new one whenever it is needed. Thanks in advance...

EDIT I am trying to do this at runtime, sorry for the confusion.

7
  • possible duplicate of vb.net programmatically creating controls Commented Jun 7, 2014 at 3:26
  • no, it is not a duplicate, I want to know hot to set a new control name based on a string that auto incraments whenever a new control is needed. Commented Jun 7, 2014 at 3:31
  • what difference does the name make? if it is added programatically, nothing but your code will see it; if it is a UC or custom control added to a form, VS will name it for you. Commented Jun 7, 2014 at 3:39
  • @Plitonix I am trying to set the contents of a string as the name of a control. Commented Jun 7, 2014 at 3:42
  • @Plutonix Tostring doesn't work, it says end of statement expected. Commented Jun 7, 2014 at 3:58

1 Answer 1

1

You cannot create a control reference from a string variable:

Dim strVar As String = "MyControl"
...
For n As Integer = 0 To 3
    Dim thisVar As String = strVar &= n.Tostring

    thisVar = New NuControl    ' already declared as String,
                               ' cant ALSO be NuControl Type!
    ...
    theForm.Controls.Add(thisVar)
 Next n

How would your code ever keep track of such things?? strVar/thisVar is a variable and can change (vary) so you'd quickly loose track of them. How would the compiler know how to handle it? Would thisVar = "" reset the string or destroy the Control?

For dynamically added controls, you often need a way to track the ones added, especially when you dont know how many there will be. To create multiple user controls with the same code at runtime:

Dim btn As Button                     ' tmp var
Dim myBtns As New List(Of Buttons)    ' my tracker

' create 3 buttons:
For n As Integer = 0 To 3
    btn = New Button                  ' create a New button
    btn.Top = ...
    btn.Left = ...                    ' set props
    btn.Name = "Button" & n.ToString  

    AddHandler ....                   ' hook up any event handlers

    theForm.Controls.Add(btn)         ' add to form
    myBtns.Add(btn)                   ' add to my list
 Next n 

What matter to the code is the ability to get at those controls we created, which is via an object reference. To disable our buttons:

 For n As Integer = 0 to myBtns.Count - 1   ' could be 3, could be 103
     myBtns(n).Enabled = False
 Next n

Note that if the controls you create can be deleted on the fly as well, that you need to dispose of them properly should you remove them from the form since you created them.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that helps, it looks like my problem was that I was doing: Dim btn As Button = New Button, instead of setting it later. Also, I realized I don't need to add an event handler, because everything is handled inside the control class.
Dim btn As Button = New Button will work, it is just not necessary to declare a new variable in a loop. Even if the code for the handlers exists somewhere, you will need to hook up the new buttons to those handlers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.