how to create controls(like button , textbox) dynamically , i want to create these for 10 or may be more than 10 times .

so i think i have to go through loops and probably i can do that but the problem is that how to create controls in runtime. is this possible to achieve that using control array ?

one more question is that how to display color palatte window in vs2008 so we can create color of our own wish?

Recommended Answers

All 10 Replies

Yes you can create controls at runtime using an array.
To display a control dialog Click Here to start.

thanx for quick reply
I need to add window for color palette so that it will be used while designing the form but the link provided by you does not seem to solve this as it deals with color dialog box at runtime(probably, as I didn't read the article completely)

the another thing is that
would you provide sample code to create controls dynamically ?

i used the following to generate controls at runtime(it may be incomplete . . .)

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static x As Integer
        Dim btn As New Button
        btn.Text = "this is button"
        btn.Top = 100 + x
        btn.Left = 100
        x = x + 100
        Me.Controls.Add(btn)
    End Sub

it generate but the problem is that how to use those controls which are created dynamically.

for ex:- how to execute specific code on the click event of first dynamically created button and execute something other code for second dynamically generated button.
In short , i want to work on their events . . .

When you create the new button use the Addhandler statement to add the a click handler to the button. It's ok for them to all point to the same handler.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static x As Integer
        Dim btn As New Button
        btn.Name = "btn"+((x/100) +1).ToString.PadLeft(2)
        btn.Text = "this is button"
        btn.Top = 100 + x
        btn.Left = 100
        x = x + 100
        AddHandler btn.Click, AddressOf btn_Click
        Me.Controls.Add(btn)
    End Sub

In the click handler declare a temporary button to equal sender cast as a button.

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ClickedButton As Button = DirectCast(sender, Button)
        Dim btnNo As Integer = Integer.Parse(ClickedButton.Name.Substring(ClickedButton.Name.Length - 2))
        Select Case btnNo
            Case 1
                'do something
            Case 2
                'do something
            etc.....
    End Sub

Now you have access to all the properties of the button. If you use a consistent naming scheme("btn01","btn02", etc.) you can parse the name and use a select block to choose different actions depending on the button clicked

thanx , i added something then its really working

your method will generate btn.Name like btn 1, btn 1.5 ,btn 2 and so on

so , i add the following :-

Static y As Integer
btn.Name = "btn" + (y + 1).ToString.PadLeft(2)
y = y + 1

but a few things still i want to ask:-

1.what does mean by this line Dim ClickedButton As Button = DirectCast(sender, Button)

2.still one more question i asked in my thread(quiet different topic) is
how to display color palatte window in vs2008 so we can create color of our own wish?

sender is of type Object and you got to let the variable ClickedButton know that it is more than just an object.
I already provided you a link about color dialog.
But her is some code from the MSDN ColorDialog class web page:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyDialog As New ColorDialog()
    ' Keeps the user from selecting a custom color.
    MyDialog.AllowFullOpen = False 
    ' Allows the user to get help. (The default is false.)
    MyDialog.ShowHelp = True 
    ' Sets the initial color select to the current text color,
    MyDialog.Color = TextBox1.ForeColor

    ' Update the text box color if the user clicks OK  
    If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        TextBox1.ForeColor = MyDialog.Color
    End If 
End Sub

1.what does mean by this line Dim ClickedButton As Button = DirectCast(sender, Button)

sender represents the control that intitiated the event, in this case a Button. Since it is declared as an object and you know which type of controls are using this handler, you can access all the properties of the control, by declaring a temporary object of that control type(ClickedButton) and have it equal sender cast to the same type(= DirectCast(sender, Button)). And since comnplex objects like this are always accessed by reference(ByRef), any property you change in the temporary control will be reflected in the control on your form that it is referencing.

how to display color palatte window in vs2008 so we can create color of our own wish?

In the design toolbox is a ColorDialog control, that is designed for that.

last questions , now i am facing difficulties to add controls to the groupbox , so how to add these controls to the groupbox or other container .

now i got the solution
thanx for your assistance to this thread

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.