0

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).

djv
  • 15,168
  • 7
  • 48
  • 72
Mark2k
  • 3
  • 2
  • Is this what you're asking: [How do I find out which control has focus in .NET Windows Forms?](https://stackoverflow.com/questions/660173/how-do-i-find-out-which-control-has-focus-in-net-windows-forms) – Andrew Morton Dec 03 '21 at 15:51
  • In CreateIMages, you initialize a PictureBox before the For, then recreate inside the For. The first one is discarded after having its position, tag, and color set. So don't do that. You can remove all that and replace the assignment in the For with `Dim pb = New PictureBox()` – djv Dec 03 '21 at 15:53
  • Focus might be the wrong word here. You can't focus a PictureBox – djv Dec 03 '21 at 15:56
  • 1
    @djv There may not be a visual indicator for the focus, but you can set the focus to it in code and `Me.ActiveControl` will then return that control. – Andrew Morton Dec 03 '21 at 16:08
  • 1
    @AndrewMorton combined with your link, that could work. But the button de-focuses every time you click it, which makes for some non-standard behavior. But setting and reading focus could be replaced by setting and reading a private variable. – djv Dec 03 '21 at 16:11
  • 1
    @djv Yes, I agree that setting a variable with the appropriate scope to remember which picturebox was last clicked could be a more sensible approach. – Andrew Morton Dec 03 '21 at 16:13
  • @djv ok for wrong word so..let me explain in other words... i have 5 picturebox after dynamically has been created i click on one of them..so now ... how resize the picturebox just clicked if i don't have any reference (like an index) ? thanks in advance – Mark2k Dec 03 '21 at 16:16

1 Answers1

1

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
djv
  • 15,168
  • 7
  • 48
  • 72