在 adhadler 事件上传递数组

Pass array on adhadler event

我的代码

Public Class LoadPictureBox

    Public Shared Sub Create(panel As Panel, array() As Movie, PosY As Integer)

        'Check height of the Movies Panel
        If array.Length Mod 5 = 0 Then
            panel.Height = (array.Length / 5 * 293) + 100
        Else
            panel.Height = (((array.Length \ 5) + 1) * 293) + 100
        End If

        'Places Labels for each movie
        Dim PosX = 50

        For index As Integer = 0 To array.Length - 1
            Dim MovieBox As New PictureBox With {
                    .BackColor = Color.Transparent,
                    .Size = New Size(182, 268),
                    .Location = New Point(PosX, PosY),
                    .Cursor = Cursors.Hand,
                    .Image = My.Resources._200,
                    .ImageLocation = array(index).Url
            }

            panel.Controls.Add(MovieBox)
            MovieBox.TabIndex = index

            'Add pictureBoxs on array
            array(index).P = MovieBox

            'Adds click even for every label
            AddHandler MovieBox.Click, AddressOf AllMoviesCLick

            PosX += 245
            If (index + 1) Mod 5 = 0 Then
                PosY += 293
                PosX = 50
            End If
        Next
    End Sub

我想将 array(index) As Movie 作为参数传递给 AddHandler MovieBox.Click, AddressOf AllMoviesCLick

类似于AddHandler MovieBox.Click, AddressOf AllMoviesCLick(array(index))

Class电影

Public Class Movie

    Private mid As Integer
    Private mtitle As String
    Private myear As String
    Private mdescription As String
    Private mrating As Double
    Private mURL As String
    Private mP As PictureBox

所以我可以使用 array.id 等

这是我第一次在这里问问题,我到处都找过,但没有找到任何解决方案

使用 Lambda Sub 直接调用您的 AllMoviesCLick 传递您的参数:

 'Here your AddHandler 
 AddHandler MovieBox.Click, Sub() AllMoviesCLick(array(index))

 'Then here your method AllMoviesCLick
  Private Sub AllMoviesCLick(movie As Movie)
      Console.WriteLine("movie id: " & movie.Whatever.ToString())
  End Sub