Click here to Skip to main content
16,017,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!


VB.NET
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


What I have tried:

I tried the event handler event but it comes with error, I tried everything else but with no luck. Hope to help me with this. Thanks!
Posted
Updated 16-Aug-22 0:37am
Comments
[no name] 15-Aug-22 17:17pm    
as VB is about event purposes, you can go like this :

public sub btn_addtoclck(sender as object, e as eventarg)
if sender is typeof button then
dim temp_btn as button = sender // you allocate a copy of the clicked button
msgbox(temp_btn.Text)
end if

end sub
[no name] 15-Aug-22 17:26pm    
another solution is to fetch again your dataset,
with the number of the panel ( count )
because of container and controls parent.
[no name] 16-Aug-22 0:11am    
as a suggestion, to reduce your code a bit.

rewrite the scope in the loop.

there are redundant code when you initialise your objects

keep the dynamic line in the loop :
count = .TableLayoutPanel1.Controls.OfType(Of Panel)().ToList().Count
Cpanel.Location = New Point(10, (25 * count) * 7)

Cpanel.Name = "Cpanel" & count

.TableLayoutPanel1.Controls.Add(Cpanel)

lblProductName = New Label()

lblProductName.Name = "lblProd" & count
lblProductName.Text = datareader.Item("ProductName").ToString & count

Cpanel.Controls.Add(lblProductName)

btnAddto.Name = count
btnAddto.Text = count
btnAddto.Tag = count.ToString

AddHandler btnAddto.Click, AddressOf btnAddto_Click // very important to locate the event on the button dynamically
Cpanel.Controls.Add(btnAddto)
count += 1
end with

all the other lines can exists before the loop.
[no name] 16-Aug-22 0:23am    
In the form structure,
all your controls are reachable by indexes.
it exists "parents containers" containing "childs controls"

your form is a "parent container", all the controls in the form are "childs controls".
a groupbox is a parent container too
a panel is a parent controls too.

with the following lines you can reach by indexes the "childs controls" in a "parent container" :

form1.controls.item(0) is the first control reachable in a form1
cpanel3.controls.item(2) is the third control in a cpanel3
groupbox2.controls.item(5) is the sixth control in the groupbox2


It's "root level" ( super parent ) contains sublevel 1 ( others controls or others parent controls ), which contains sublevel 2 ( others controls/parents controls in , and so and so ... )
Member 12839286 16-Aug-22 6:22am    
I already followed your suggestions. Thanks for that, I now understand the concept. I gather all you ideas and I already have the solution.

You are creating a panel per product containing a label and a button for that product and giving each control a name based on count

So you have a few options:
1) You will know when a, and which button was clicked (the sender argument). So you can look at it's Parent property Control.Parent Property (System.Windows.Forms) | Microsoft Docs[^]
The Panel is a container, so you can loop through it's controls (see the comments from Member 15627495) until you find the label (TypeOf Operator - Visual Basic | Microsoft Docs[^])

2) You could derive the actual name of the label from the name of the button that was clicked as they are all "grouped" by using count

3) Easier would be to store the name of the label you want in the Tag property of the button - Control.Tag Property (System.Windows.Forms) | Microsoft Docs[^] or derive it from the Tag you are already storing !
VB
btnAddto.Tag = count.ToString
VB
Dim associatedLabelName = "lblProd" & sender.Tag.ToString()
Then recursively search for that control name - example at Finding Controls on Forms -Deborah's Developer MindScape[^]

Caveat: I haven't got access to VB.NET at the moment, hence only links and no code samples
 
Share this answer
 
Comments
Member 12839286 16-Aug-22 6:27am    
Thanks for those links and suggestion, I got an idea and knowledge about control. I already have a solution for my problem and it works perfect.
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 = frmMenu.Controls.Find("lblProd" & count, True)
    If (labels.Length > 0) Then
        Dim label = labels(0)
        MessageBox.Show(label. Text)
    End If
End Sub



I have these lacking codes on my program the If statement above. Finding control on my frmMenu, specifically a label named "lblProd". Controls.Find returns an array of Control. 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900