如何将事件调用到另一个事件(控件是动态创建的)

时间:2016-04-23 10:53:59

标签: vb.net

即使我点击PictureBox或标签,我也想调用按钮事件。

或者你可以给我另一种方法来创建我包含的图像。 (也许他们称之为OjectListView我猜 - 注意:我不知道回合OLV)。

我没有其他选择所以我使用按钮就像在这张图片中一样

this image

我不是请你帮助我的专家。提前谢谢。

Imports MySql.Data.MySqlClient

Public Class Form2<br>
    Dim Sql As String<br>
    Public conn As MySqlConnection<br>
    'Public cmd As New MySqlCommand<br>
    'Public reader As MySqlDataReader<br>
    Public adapter As MySqlDataAdapter<br>
    Public table As New DataTable
    Dim x As Integer = 5
    Dim y As Integer = 5

    Public Sub HandleButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim btn As Button = DirectCast(sender, Button)
        Dim str As String = btn.Tag
        MessageBox.Show(str)
    End Sub

    Private Sub HandleLabelClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim lbl As Label = DirectCast(sender, Label)
        Dim str As String = lbl.Text
        MessageBox.Show(str)
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(1197, 738)
        conn = New MySqlConnection("server=localhost; user=root; pass=; port=3306; database=db_sample_animelist")
        Try
            conn.Open()
        Catch ex As Exception
            MessageBox.Show("Error Connecting to Database.", "Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End
        End Try
        ' This line of code is to get all data to database
        Sql = "SELECT * FROM tbl_animelist"
        adapter = New MySqlDataAdapter(Sql, conn)
        adapter.Fill(table)
        Dim i As Integer = 1
        ' This line of code is to display data from database
        For Each row In table.Rows
            Dim list_button As New Button
            list_button.Name = "list_button" & i
            list_button.Size = New Size(472, 184)
            list_button.Location = New Point(x, y)
            'list_button.FlatStyle = FlatStyle.Flat
            list_button.Tag = row.item("id")
            AddHandler list_button.Click, AddressOf Me.HandleButtonClick

            Dim picbox As New PictureBox
            picbox.Name = "picbox" & i
            picbox.Size = New Size(121, 162)
            picbox.Location = New Point(10, 10)
            picbox.BackColor = Color.Transparent
            Try
                Dim arrImage() As Byte
                arrImage = row.item("image")
                Dim mstream As New System.IO.MemoryStream(arrImage)
                picbox.Image = Image.FromStream(mstream)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            list_button.Controls.Add(picbox)
            'AddHandler picbox.Click, AddressOf HandlePicClick

            Dim lbl_title As New Label
            lbl_title.Name = "lbl_title" & i
            lbl_title.AutoSize = False
            lbl_title.Size = New Size(330, 20)
            lbl_title.AutoEllipsis = True
            lbl_title.BackColor = Color.DodgerBlue
            lbl_title.Location = New Point(136, 10)
            lbl_title.Text = row.item("name")
            'label.Font = New Font(label.Font, FontStyle.Bold)
            lbl_title.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
            lbl_title.Tag = i
            list_button.Controls.Add(lbl_title)
            AddHandler lbl_title.Click, AddressOf HandleLabelClick

            Dim lbl_rating As New Label
            lbl_rating.Name = "lbl_rating" & i
            lbl_title.AutoSize = False
            lbl_rating.Size = New Size(76, 20)
            lbl_rating.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
            lbl_rating.Location = New Point(136, 35)
            lbl_rating.BackColor = Color.Transparent
            lbl_rating.Text = "Rating :"
            list_button.Controls.Add(lbl_rating)
            AddHandler lbl_rating.Click, AddressOf HandleLabelClick

            Dim lbl_rate As New Label
            lbl_rate.Name = "lbl_rate" & i
            lbl_rate.AutoSize = False
            lbl_rate.Size = New Size(49, 20)
            lbl_rate.Location = New Point(211, 35)
            lbl_rate.Text = row.item("rating")
            lbl_rate.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
            lbl_rate.BackColor = Color.Transparent
            list_button.Controls.Add(lbl_rate)
            AddHandler lbl_rate.Click, AddressOf HandleLabelClick

            Dim lbl_ep As New Label
            lbl_ep.Name = "lbl_ep" & i
            lbl_ep.AutoSize = False
            lbl_ep.Size = New Size(76, 20)
            lbl_ep.Location = New Point(136, 55)
            lbl_ep.Text = "Episodes :"
            lbl_ep.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
            lbl_ep.BackColor = Color.Transparent
            list_button.Controls.Add(lbl_ep)
            AddHandler lbl_ep.Click, AddressOf HandleLabelClick

            Dim lbl_ep_count As New Label
            lbl_ep_count.Name = "lbl_ep_count" & i
            lbl_ep_count.AutoSize = False
            lbl_ep_count.Size = New Size(142, 20)
            lbl_ep_count.Location = New Point(211, 55)
            lbl_ep_count.Text = row.item("episodes")
            lbl_ep_count.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
            lbl_ep_count.BackColor = Color.Transparent
            list_button.Controls.Add(lbl_ep_count)
            AddHandler lbl_ep_count.Click, AddressOf HandleLabelClick

            Dim lbl_aired As New Label
            lbl_aired.Name = "lbl_aired" & i
            lbl_aired.AutoSize = False
            lbl_aired.Size = New Size(76, 20)
            lbl_aired.Location = New Point(136, 75)
            lbl_aired.Text = "Aired :"
            lbl_aired.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
            lbl_aired.BackColor = Color.Transparent
            list_button.Controls.Add(lbl_aired)
            AddHandler lbl_aired.Click, AddressOf HandleLabelClick

            Dim lbl_aired_date As New Label
            lbl_aired_date.Name = "lbl_aired_date" & i
            lbl_aired_date.AutoSize = False
            lbl_aired_date.AutoEllipsis = True
            lbl_aired_date.Size = New Size(255, 20)
            lbl_aired_date.Location = New Point(211, 75)
            lbl_aired_date.Text = row.item("aired")
            lbl_aired_date.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
            lbl_aired_date.BackColor = Color.Transparent
            list_button.Controls.Add(lbl_aired_date)
            AddHandler lbl_aired_date.Click, AddressOf HandleLabelClick

            Dim lbl_genres As New Label
            lbl_genres.Name = "lbl_genres" & i
            lbl_genres.AutoSize = False
            lbl_genres.Size = New Size(76, 20)
            lbl_genres.Location = New Point(136, 95)
            lbl_genres.Text = "Genres :"
            lbl_genres.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
            lbl_genres.BackColor = Color.Transparent
            list_button.Controls.Add(lbl_genres)
            AddHandler lbl_genres.Click, AddressOf HandleLabelClick

            Dim lbl_genre_list As New Label
            lbl_genre_list.Name = "lbl_genre_list" & i
            lbl_genre_list.AutoSize = False
            lbl_genre_list.AutoEllipsis = True
            lbl_genre_list.Size = New Size(255, 20)
            lbl_genre_list.Location = New Point(211, 95)
            lbl_genre_list.Text = row.item("genres")
            lbl_genre_list.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
            lbl_genre_list.BackColor = Color.Transparent
            list_button.Controls.Add(lbl_genre_list)
            AddHandler lbl_genre_list.Click, AddressOf HandleLabelClick

            Dim lbl_synopsis As New Label
            lbl_synopsis.Name = "lbl_synopsis" & i
            lbl_synopsis.AutoSize = False
            lbl_synopsis.AutoEllipsis = True
            lbl_synopsis.Size = New Size(330, 68)
            lbl_synopsis.Location = New Point(136, 115)
            lbl_synopsis.Text = row.item("synopsis")
            lbl_synopsis.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
            lbl_synopsis.BackColor = Color.Transparent
            lbl_synopsis.MaximumSize = New Size(330, 68)
            list_button.Controls.Add(lbl_synopsis)
            AddHandler lbl_synopsis.Click, AddressOf HandleLabelClick

            Panel1.Controls.Add(list_button)
            ' This line of code is to set location for every button
            x += 480
            If i Mod 2 = 0 Then
                x = 5
                y += 184 + 5
            End If
            ' Every loop it will add one for the purpose of naming a control
            i += 1
        Next
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

您必须在运行时将picturehandler分配给picturebox事件处理程序,如下所述:

https://msdn.microsoft.com/en-us/library/dfty2w4e(v=vs.110).aspx

我不习惯vb,但应该是以下内容:

AddHandler picbox.Click, AddressOf HandleButtonClick

当然,事件处理程序中的逻辑也需要修改,因为它将发送者强制转换为按钮,在点击图像的情况下,该按钮当然是图像。

这可以让你走上正轨。