如何将事件添加到通过单击按钮 VB.NET 动态创建的 PictureBox
How can I add events to a PictureBox dynamically created by clicking button VB.NET
使用文本框添加我动态创建的数字,例如表单中的 5 个图片框,如下所示:
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
然后,在创建图片框后单击每个...使用 AddHandler pb.Click, AddressOf _Click
我获得了焦点和 3 个按钮来处理当前图片框(只需增加/减少或切成两部分)使用此代码:
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()
现在我想通过 3 个按钮来管理它们(“+”增大尺寸“-”减小尺寸 e 切割...)但我不知道如何让图片框聚焦(涉及)。
只需保留对上次单击的 PictureBox 的引用,然后在按钮处理程序中对其进行操作
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
使用文本框添加我动态创建的数字,例如表单中的 5 个图片框,如下所示:
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
然后,在创建图片框后单击每个...使用 AddHandler pb.Click, AddressOf _Click
我获得了焦点和 3 个按钮来处理当前图片框(只需增加/减少或切成两部分)使用此代码:
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()
现在我想通过 3 个按钮来管理它们(“+”增大尺寸“-”减小尺寸 e 切割...)但我不知道如何让图片框聚焦(涉及)。
只需保留对上次单击的 PictureBox 的引用,然后在按钮处理程序中对其进行操作
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