Results 1 to 2 of 2

Thread: Events in controls added during runtime

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Events in controls added during runtime

    Hello
    In the following code, when the Checkbox is checked, 10 pictureboxes are added to the form.
    Code:
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
            Dim btnName As String
    2:
            Dim x As Short = 0
    3:
    
    4:
            For i As Short = 1 To 10
    5:
                btnName = "PictureBox" & CStr(i)
    6:
                x += 20
    7:
                Dim PictureBox As New PictureBox
    8:
    
    9:
    
    10:
                PictureBox.Name = btnName
    11:
                Me.Controls.Add(PictureBox)
    12:
                PictureBox.Location = New Point(x, 10)
    13:
                PictureBox.Text = "Hello"
                PictureBox.BorderStyle = BorderStyle.Fixed3D
                PictureBox.Height = 10
                PictureBox.Width = 10
    
    14:
    15:
    
    16:
    
    17:
            Next
        End Sub
    How do I do a Click event in these pictureboxes?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Events in controls added during runtime

    The method that handles an event looks the same regardless of whether the control is added at design time or run time. The only difference is that it won't have a Handles clause if you're using it for run time-added controls. Look at your CheckedChanged event handler to see the Handles clause. For a control added at run time, use an AddHandler statement to register the method as a handler for an event.

Tags for this Thread

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