简体   繁体   English

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

[英]Which auto generated picturebox was clicked? VB.net

I have the following code. 我有以下代码。 Im trying to find out which of the 64 pictureboxes was clicked: 我试图找出单击了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

Thanks. 谢谢。

In order to handle a click event, the first thing you'll need is a click handler. 为了处理单击事件,您需要的第一件事是单击处理程序。 Could be something as simple as this: 可能像这样简单:

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

When you create your PictureBox controls, bind the handler to their click event: 创建PictureBox控件时,将处理程序绑定到其click事件:

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

What this should do is invoke the PictureBox_Click method any time the user clicks on the PictureBox . 用户应该在PictureBox_Click单击PictureBox时调用PictureBox_Click方法。 Within that method, sender is the element which was clicked: 在该方法中, 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM