Home > Enterprise >  How to retrieve text from dynamic label and display it to message box using dynamic button?
How to retrieve text from dynamic label and display it to message box using dynamic button?

Time:08-16

I have these codes retrieving data from mysql database and display it into the dynamic panels, labels, and buttons. The problem is I don't know how to retrieve those texts from labels(lblProductName) to display it into message box when clicking the dynamic buttons(btnAddto). TIA!

Imports MySql.Data.MySqlClient
Module mdlMenu
Dim Cpanel As Panel
Dim lblProductName As Label
Dim btnAddto As Button
Dim count As Integer

Public Sub LoadMenu()
    Try
        SQLConnection()
        With sqlcom
            .CommandText = "SELECT * FROM tblproduct"
            .Connection = sqlconn
        End With
        Dim datareader As MySqlDataReader = sqlcom.ExecuteReader
        Do While datareader.Read()
            Cpanel = New Panel()
            With frmMenu

                count = .TableLayoutPanel1.Controls.OfType(Of Panel)().ToList().Count
                Cpanel.Location = New Point(10, (25 * count) * 7)
                Cpanel.Size = New Size(450, 160)
                Cpanel.Name = "Cpanel" & count
                Cpanel.AutoScroll = True
                Cpanel.BackColor = Color.White
                .TableLayoutPanel1.Controls.Add(Cpanel)

                lblProductName = New Label()
                lblProductName.Location = New Point(165, 10)
                lblProductName.AutoSize = True
                lblProductName.Name = "lblProd" & count
                lblProductName.Text = datareader.Item("ProductName").ToString & count
                lblProductName.Font = New Font(lblProductName.Font.FontFamily, 14, FontStyle.Bold)
                Cpanel.Controls.Add(lblProductName)

                btnAddto = New Button()
                btnAddto.Location = New Point(180, 115)
                btnAddto.Size = New Size(80, 40)
                btnAddto.Name = count
                btnAddto.Text = count
                btnAddto.Tag = count.ToString
                btnAddto.Font = New Font(btnAddto.Font.FontFamily, 10, FontStyle.Bold)
                AddHandler btnAddto.Click, AddressOf btnAddto_Click
                Cpanel.Controls.Add(btnAddto)
                count  = 1
          End With
      Loop
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        sqlconn.Close()
        sqlconn.Dispose()
    End Try
End Sub

CodePudding user response:

There are a couple of concepts here:

  1. How to reference a dynamic control
  2. How to bind an event to a dynamic control

Regarding number 1 on how to reference a dynamic control, it looks like you are declaring a form level variable and setting the value. So you should be able to reference lblProductName after it is set in the LoadMenu method. However, if you did not want to create a form level variable, you could simply use the Controls.Find method (documentation).

Regarding number 2 on how to bind an event to a dynamic control, you would use the AddHandler statement (documentation), which it looks like you are already doing here:

AddHandler btnAddto.Click, AddressOf btnAddto_Click

So what would the btnAddto_Click method look like putting it all together?

Private Sub btnAddto_Click(sender As Object, e As EventArgs)
    Dim button = DirectCast(sender, Button)
    Dim count = If(button.Tag IsNot Nothing, button.Tag.ToString(), String.Empty)
    Dim labels = Controls.Find("lblProd" & count, True)
    If (labels.Length > 0) Then
        Dim label = labels(0)
        MessageBox.Show(label.Text)
    End If
End Sub
  • Related