Fire Panel的Click事件

时间:2016-07-07 10:59:58

标签: vb.net winforms visual-studio-2015

我一直在寻找如何为Click自动触发Panel事件。我找到了为Button执行此操作的代码,但它不一样。

我的问题是:我正在制作一个自动图库生成器,当您单击包含该用户ID的父块时,我想要触发该事件。问题是当我点击事件时图像和标签在块之前,然后如果我点击图像或标签我想要点击该块。

这是我的代码:

Public Class users
    Private Sub users_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' COMPROVE CONNECTION
        Try
            ' ESTABLISH CONNECTION TO SERVER
            conn = New DataBase(DbServer, DbName, DbUser, Password)
            ' PICK UP DATA FROM USERS 
            Dim DataTable As New DataTable
            DataTable = conn.ConsultSql("SELECT `id_user`,`full_name`,`img` FROM `user` WHERE `user_type` = 1")
            ' PRINT PHARMACIST
            Dim b As Integer = 0
            While b < DataTable.Rows.Count
                Dim RowData As DataRow = DataTable.Rows(b)
                PrintUsers(RowData)
                b += 1
            End While
        Catch ex As Exception
            MessageBox.Show("Ocurrió el siguiente error: " & ex.Message, "Error al loguearse", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Sub PrintUsers(ByVal RowData As DataRow)
        Dim iNumber As String
        Console.WriteLine(RowData("full_name"))
        iNumber = RowData("full_name")
        ' GENERATE ID 
        Dim oPaneldId As New Panel
        oPaneldId.Name = RowData("id_user")
        ' GENERATE TEXT NAME 
        Dim oTextField As New Label
        oTextField.Name = "name" & iNumber
        oTextField.Text = iNumber
        ' GENEREATE PHOTO
        Dim oPictureBox As New PictureBox
        oPictureBox.Name = "img" & iNumber
        Dim MyWebClient As New System.Net.WebClient
        'BYTE ARRAY HOLDS THE DATA
        Dim ImageInBytes() As Byte = MyWebClient.DownloadData(RowData("img"))
        'CREATE A MEMORY STREAM USING THE BYTES
        Dim ImageStream As New IO.MemoryStream(ImageInBytes)
        'CREATE A BITMAP FROM THE MEMORY STREAM
        oPictureBox.Image = New System.Drawing.Bitmap(ImageStream)
        oPictureBox.Size = New Size(100, 130)
        ' ADDING TO THE BODY
        oPaneldId.Controls.Add(oTextField)
        oPaneldId.Controls.Add(oPictureBox)
        AddHandler oPaneldId.Click, AddressOf Me.SelectUser
        AddHandler oPictureBox.Click, AddressOf Me.ClickFather
        AddHandler oTextField.Click, AddressOf Me.ClickFather
        body.Controls.Add(oPaneldId)
    End Sub

    Public Sub SelectUser()
        Console.WriteLine("Funciona")
    End Sub

    Public Sub ClickFather(sender As Object, e As EventArgs)
        Console.WriteLine(sender.GetType.Name)
        Console.WriteLine(sender.parent.Name)
        sender.parent.click
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

将面板中其他控件的单击事件处理程序添加到用于面板单击事件的同一事件处理程序中。所以你的代码将改为

AddHandler oPaneldId.Click, AddressOf Me.SelectUser
AddHandler oPictureBox.Click, AddressOf Me.SelectUser
AddHandler oTextField.Click, AddressOf Me.SelectUser