'Create a function that shows user controls in a panel

I created several user controls that i want to display on a main panel. Thus, I have this function that takes in input the control I want to display by setting it to visible, and visible = false for all other ones :

Public Sub ShowControl(ByVal Controle As Control)
    MainPanel.BringToFront()
    Try
        With MainPanel
            For Each contr As Control In .Controls
                If contr.Name <> Controle.Name Then
                    contr.Visible = False
                Else
                    ' Display the control
                    contr.Visible = True
                End If
            Next

        End With

    Catch ex As Exception
    End Try

It works well when my panel has different user controls like this :

            Public WithEvents uc1 As New User_Control1
        Public WithEvents uc2 As New User_Control2
        Public WithEvents uc3 As New User_Control3

        MainPanel.Controls.Add(uc1)
        MainPanel.Controls.Add(uc2)
        MainPanel.Controls.Add(uc3)

But not if I create several instances from the same user control :

            Public WithEvents uc1 As New User_Control1
        Public WithEvents uc2 As New User_Control1
        Public WithEvents uc3 As New User_Control1

        MainPanel.Controls.Add(uc1)
        MainPanel.Controls.Add(uc2)
        MainPanel.Controls.Add(uc3)

I know that this is because in my function the name Controle.name will remain the same (User_Control1), but is there any way to make it works ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source