Click here to Skip to main content
15,915,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have panels created dynamically from a db I would like to add a mouse handler but the below code only recognize the main form not the panel as sender.

VB
Public Function creation_panel(ID As String, Name As String, Ext As String, Role As String, Status As String, LastActionTime As String) As Panel
    Dim newPanel As New Panel
    Try
        With newPanel
            .Name = "Panel_dyna_" + i.ToString
            .Size = New Size(150, 35)
            If Status = "Ready" Then
                .BackColor = Color.DarkGreen
            ElseIf Status = "Away" Then
                .BackColor = Color.DarkOrange
            End If
            AddHandler newPanel.MouseDown, AddressOf DynaPanel_MouseDown
            .Visible = True

            '   .Location = New System.Drawing.Point(default_x, default_y + 25)
        End With

        i += 1
        ' default_y += 25
        Debug.WriteLine(newPanel.Name)

        list_panel.Controls.Add(newPanel)

        Panel_collec.Add(newPanel)
        creation_label_db_content(newPanel, ID, Name, Ext, Role, Status, LastActionTime)


VB
Private Sub DynaPanel_MouseDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseDown
    ' Handle your Button clicks here
    '   If e.Button = MouseButtons.Left Then
    Debug.Print(sender.name)  '>>> shows the sender (only main returned when clicking the mainform and no reaction from the panels when clicking on it)

    '     End If
End Sub



Many thanks
Posted
Comments
[no name] 24-Jun-15 12:13pm    
If I were to take your code (and I did), fix the compilation errors, and run it in a new project, it correctly shows "Panel_dyna_0" when I click the panel.
ndmaster01 24-Jun-15 13:03pm    
Yes indeed , if you comment the line : creation_label_db_content(newPanel, ID, Name, Ext, Role, Status, LastActionTime)

which does add some text on the panels

here is the code of this routine. So the problem seems to come from this one

Public Function creation_label_db_content(panel As Panel, ID As String, Name As String, Ext As String, Role As String, Status As String, LastActionTime As String) As Label
Dim newLabel_Name As New Label
Dim newLabel_Ext As New Label
Dim newLabel_Role As New Label
Dim newLabel_Status As New Label

With newLabel_Name
.Name = "Label_dyna_Name" + ID
.Font = New System.Drawing.Font("Tahoma", 7.25!, System.Drawing.FontStyle.Bold)
.Text = Name.ToUpper
.Dock = DockStyle.Fill
.TextAlign = ContentAlignment.BottomRight
.Size = New System.Drawing.Size(0, 13)
.ForeColor = Color.White
End With
With newLabel_Role
.Name = "Label_dyna_Role" + ID
.Font = New System.Drawing.Font("Tahoma", 7.25!, System.Drawing.FontStyle.Bold)
.Size = New System.Drawing.Size(0, 12)
.Dock = DockStyle.Top
.TextAlign = ContentAlignment.BottomLeft
.ForeColor = Color.White
.Text = Role
End With
With newLabel_Ext
.Name = "Label_dyna_Ext" + ID
.Text = Ext
.Font = New System.Drawing.Font("Tahoma", 7.25!, System.Drawing.FontStyle.Bold)
.Size = New System.Drawing.Size(0, 12)
.Dock = DockStyle.Bottom
.TextAlign = ContentAlignment.BottomLeft
.ForeColor = Color.White

End With
'With newLabel_Status
' .Name = "Label_dyna_Status" + ID
' .Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold)
' ' .Text = Status
'End With
panel.Controls.Add(newLabel_Name)
panel.Controls.Add(newLabel_Ext)
panel.Controls.Add(newLabel_Role)
panel.Controls.Add(newLabel_Status)
Label_collec.Add(newLabel_Name)
Label_collec.Add(newLabel_Ext)
Label_collec.Add(newLabel_Role)
Label_collec.Add(newLabel_Status)
Debug.WriteLine(newLabel_Name.Name)
Debug.WriteLine(newLabel_Ext.Name)
Debug.WriteLine(newLabel_Role.Name)
Debug.WriteLine(newLabel_Status.Name)

End Function
[no name] 24-Jun-15 13:26pm    
Okay and...? If you think that I am going to wade through all of this unformatted code and debug it for you, you are mistaken.
ndmaster01 24-Jun-15 13:41pm    
;) sorry

In fact I think the error is coming from theses lines
panel.Controls.Add(newLabel_Name)
...
Label_collec.Add(newLabel_Name)
...
I think the label put on the panel override the panel handler. but I don't understand what's going on
Ralf Meier 24-Jun-15 13:43pm    
Where have you added the EventHandler from the Label(s) to you method ?

1 solution

to complete this :
(by the example of one of these Labels)
VB
With newLabel_Name
.Name = "Label_dyna_Name" + ID
.Font = New System.Drawing.Font("Tahoma", 7.25!, System.Drawing.FontStyle.Bold)
.Text = Name.ToUpper
.Dock = DockStyle.Fill
.TextAlign = ContentAlignment.BottomRight
.Size = New System.Drawing.Size(0, 13)
.ForeColor = Color.White

.Parent = panel ' replaces the panel.Controls.Add(newLabel_Name)

.AddHandler .MouseDown, AddressOf DynaPanel_MouseDown
End With


But Attention :
This manuel created Handler is not removed automaticly when disposing the Panel. For this you MUST remove it by your own Dispose-Method (or Detach-Method).
 
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