Link to home
Start Free TrialLog in
Avatar of vsvb
vsvb

asked on

Problem in Load Dynamic Control in GroupBox

Hi Experts

In this example on Button1_click event , I am Dyanmiclly Creat Control array of Buttton on Form1

But i want this Button is not creat on Form1  as container but on the GroupBox control as container
(mins all Button is loaded in Groupbox insted of Form)

any one give me help how to make it this

Thanks

Add 2 control on form Button1 and GroupBox1

'##############################################
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(112, 296)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(152, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'GroupBox1
        '
        Me.GroupBox1.Location = New System.Drawing.Point(422, 52)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(264, 160)
        Me.GroupBox1.TabIndex = 3
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "GroupBox1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(840, 606)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Create the button
        'Specify the location and the size
        Dim btn(3) As Button
        Dim i As Integer
        For i = 1 To 3
            btn(i) = New Button
            btn(i).Location = New System.Drawing.Point(200, 30 * i)
            btn(i).Size = New System.Drawing.Size(100, 20)
            btn(i).Text = "New Button" + Str(i)
            'Add it to the forms control collection
            Me.Controls.Add(btn(i))
            'Link the event to the event handler
            AddHandler btn(i).Click, AddressOf Me.ClickButton
        Next
    End Sub
    Private Sub ClickButton(ByVal sender As System.Object, ByVal e As System.EventArgs)  'Handles Button1.Click,        Button2.Click(, Button3.Enter)
        Dim btn As Button
        btn = CType(sender, Button)
        MsgBox(btn.Text)
    End Sub
End Class
##############################################
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of S-Twilley
S-Twilley

u can also put this into one line

           btn(i).Location = New System.Drawing.Point(200, 30 * i)
            btn(i).Size = New System.Drawing.Size(100, 20)

can be:

          btn(i).SetBounds(200, 3 * i, 100, 20)
or a different alternative would be:

        Dim btn(2) As Button
        Dim i As Integer
        For i = 0 To 2
            btn(i) = New Button
            btn(i).SetBounds(200, 3 * (i + 1), 100, 20)
            btn(i).Text = "New Button" + (i + 1).ToString
            AddHandler btn(i).Click, AddressOf Me.ClickButton
        Next

        GroupBox1.Controls.AddRange(btn) ' you have to make sure that all elements in the array are filled
a different alternative... that makes a whole lotta sense! :D
Avatar of vsvb

ASKER

tx Twilley
have a nice day
u too