2016-04-23 39 views
-5

即使單擊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

你必須在運行時指定的事件處理程序的PictureBox的事件處理程序喜歡這裏描述:

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

我不習慣到VB,但它應該是類似以下內容:

AddHandler picbox.Click, AddressOf HandleButtonClick 

當然,您的事件處理函數中的邏輯也需要修改,因爲它將發送者強制轉換爲一個按鈕,點擊圖像當然是一個圖像。

這應該讓你走上正確的軌道。

+0

好吧,先生,我需要做更多的研究.. btw tnx ...:D –

相關問題