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 !
btnAddto.Tag = count.ToString
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