Home > Blockchain >  How can I add events to a PictureBox dynamically created by clicking button VB.NET
How can I add events to a PictureBox dynamically created by clicking button VB.NET

Time:12-04

Using a textbox to add a number I dynamically created for example 5 pictureboxes in a form, like this:

Sub CreateImages()
    Dim pb As New PictureBox
    Dim i As Integer = TextBoxNumberImages.Text
    Dim Z As Integer = 10

    pb.Top = 50
    pb.Left = 50
    pb.Tag = Color.Black
    pb.BackColor = Color.Red

    For i = 1 To i
        pb = New PictureBox
        pb.Width = 120
        pb.Height = 330
        pb.Left  = Z
        pb.Tag = False
        pb.Name = "Image"   Str(i)
        pb.BorderStyle = BorderStyle.FixedSingle

        pb.BackColor = Color.Green
        AddHandler pb.Click, AddressOf _Click

        Me.Controls.Add(pb)
        pb.Refresh()
        Z  = 125
    Next
End Sub

then, after the picturebox has been created clicking on each..using AddHandler pb.Click, AddressOf _Click I got the focus and 3 buttons to handle the current picturebox (just increase / decrease or cut in two pieces) with this code:

Public Sub _Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim pb As PictureBox = DirectCast(sender, PictureBox)
    Dim selected As Boolean = DirectCast(pb.Tag, Boolean)

         
    Dim d As Integer
    For d = 1 To TextBoxNumberImages.Text
        If pb.Name = "Image"   Str(d) Then
            NOMEPIC = pb.Name
            pb.BackColor = Color.Blue
            '   MsgBox(pb.Location.X & "X")
            '   MsgBox(pb.Location.Y & "Y")
            BtnAdd.Top = pb.Location.Y   330
            BtnAdd.Left = pb.Location.X
            BtnSub.Top = pb.Location.Y   330
            BtnSub.Left = pb.Location.X   40
            BtnNew.Top = pb.Location.Y   330
            BtnNew.Left = pb.Location.X   80
            Label1.Top = pb.Location.Y   360
            Label1.Left = pb.Location.X
            Label1.Text = "Art. " & d
            txtArt.Top = pb.Location.Y   380
            txtArt.Left = pb.Location.X
        End If
    Next
    BtnAdd.Visible = True
    BtnSub.Visible = True
    BtnNew.Visible = True
    Label1.Visible = True
    txtArt.Visible = True
    pb.Refresh()

Now I would like to manage them through the 3 buttons (" " increase size "-" decrease size e cut...) but I don't know how get the picturebox focused(involved).

CodePudding user response:

Just hold a reference to the last clicked PictureBox, then act on that in the button handlers

Private selectedPictureBox As PictureBox

Private Sub _Click(sender As Object, e As EventArgs)
    selectedPictureBox = DirectCast(sender, PictureBox)
    ' ...
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    If selectedPictureBox Is Nothing Then Exit Sub
    selectedPictureBox.Width  = 1
    selectedPictureBox.Height  = 1
End Sub

Private Sub btnSub_Click(sender As Object, e As EventArgs) Handles btnSub.Click
    If selectedPictureBox Is Nothing Then Exit Sub
    selectedPictureBox.Width -= 1
    selectedPictureBox.Height -= 1
End Sub
  • Related