单击了哪个自动生成的图片框? VB.net

时间:2019-03-04 13:20:17

标签: vb.net

我有以下代码。我试图找出点击了64个图片框中的哪个:

        For i As Integer = 1 To 8
        For j As Integer = 1 To 8
            SpilleBræt(i, j) = New PictureBox 'Opretter picturebox
            If (i + j) Mod 2 = 1 Then
                Me.SpilleBræt(i, j).BackgroundImage = Skak.My.Resources.DarkTile
            Else
                Me.SpilleBræt(i, j).BackgroundImage = Skak.My.Resources.LightTile
            End If

            'Placering, størrelse, m.v.
            Me.SpilleBræt(i, j).Location = New System.Drawing.Point((i - 1) * 103, (j - 1) * 103)
            Me.SpilleBræt(i, j).Size = New System.Drawing.Size(100, 100)
            Me.SpilleBræt(i, j).Name = "SpilleBrik" & i & j
            Me.PanelSpilleBræt.Controls.Add(Me.SpilleBræt(i, j))
        Next j
    Next i

谢谢。

1 个答案:

答案 0 :(得分:0)

为了处理单击事件,您需要的第一件事是单击处理程序。可能像这样简单:

Private Sub PictureBox_Click(ByVal sender As Object, ByVal e As EventArgs)  
    ' Do something in here
End Sub

创建PictureBox控件时,请将处理程序绑定到其click事件:

AddHandler Me.SpilleBræt(i, j).Click, AddressOf PictureBox_Click
Me.PanelSpilleBræt.Controls.Add(Me.SpilleBræt(i, j))

这应该做的是,只要用户单击PictureBox_Click,就调用PictureBox方法。在该方法中,sender是被单击的元素:

Private Sub PictureBox_Click(ByVal sender As Object, ByVal e As EventArgs)  
    Dim clickedBox As PictureBox
    clickedBox = CType(sender, PictureBox)
    ' clickedBox is the element which was clicked
End Sub