Dynamic Class Instantiate


Joined
Jan 13, 2013
Messages
1
Reaction score
0
Hi,

I am new to this forum and require some help from you guys.

1. I have a class name HRModule, AdminModule, EquipmentModule and etc

2. In each of the module there will be some line that will be generate as sub menu when the main menu was clicked.

3. In the sub menu currently when it called by the main menu it will instantiate a class of forms.usercontrol

4. My question is how can I instantiate the forms.usercontrol from a string variable when the sub menu was click instead of directly initialize the object everytime the user click on the main menu.

Thanks in advance.

Sample Code

Code:
HRModule Class
Namespace HRMIS.HumanResource

    Partial Public Class HumanResourceModule
        Implements IModule
#Region "Implementation of IModule"

        Public ReadOnly Property AppCode() As String Implements IModule.Code
            Get
                Return "HR"
            End Get
        End Property

        Public ReadOnly Property AppName() As String Implements IModule.Name
            Get
                Return "Personnel Details"
            End Get
        End Property

        Public ReadOnly Property DisplayOrder() As Integer Implements IModule.DisplayOrder
            Get
                Return 1
            End Get
        End Property

        Public ReadOnly Property Menus() As List(Of AppsMenu) Implements IModule.Menus
            Get
                Return If(_menus, (InlineAssignHelper(_menus, GetMenus())))
            End Get
        End Property

#End Region
        Private _menus As List(Of AppsMenu)

        Public Function GetMenus() As List(Of AppsMenu)
            Dim menus = New List(Of AppsMenu)()
            Dim DeptMenus = New List(Of AppsMenu)()

            menus.Add(New AppsMenu(Me, "Employee", "Employee", New Pages.Employee.Page(), New List(Of AppsMenu)()))
            menus.Add(New AppsMenu(Me, "Role and Function", "Role and Function", New Pages.RoleFunction.Page(), New List(Of AppsMenu)()))

            Return menus
        End Function
        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function

    End Class
End Namespace
Code:
AppsMenu Class
Namespace HRMIS.Framework.UserControl

    Public Class AppsMenu

        Public Sub New(AppsModule As IModule, strCode As String, strName As String, AppPage As IPage, children As IList(Of AppsMenu))
            AppModule = AppsModule
            Code = strCode
            Name = strName
            Page = AppPage
            AppChildren = children
        End Sub

        Public Property AppModule() As IModule
            Get
                Return m_Module
            End Get
            Private Set(value As IModule)
                m_Module = value
            End Set
        End Property
        Private m_Module As IModule

        Public Property Code() As String
            Get
                Return m_Code
            End Get
            Private Set(value As String)
                m_Code = value
            End Set
        End Property
        Private m_Code As String

        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Private Set(value As String)
                m_Name = value
            End Set
        End Property
        Private m_Name As String

        Public Property Page() As IPage
            Get
                Return m_Page
            End Get
            Private Set(value As IPage)
                m_Page = value
            End Set
        End Property
        Private m_Page As IPage

        Public Property AppChildren() As IList(Of AppsMenu)
            Get
                Return m_Children
            End Get
            Private Set(value As IList(Of AppsMenu))
                m_Children = value
            End Set
        End Property
        Private m_Children As IList(Of AppsMenu)

    End Class
End Namespace
Code:
MainForm Class
Namespace HRMIS
    Partial Public Class frmMain

        Public Sub New()

            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            LoadAdminMenu()

            '            rdMain.ShowToolCloseButton = False
            '           rdMain.ShowDocumentCloseButton = False
        End Sub
        Private Sub LoadAdminMenu()

            Me.Text = "HRMIS - Human Resource Application"

            Dim HRAdminNode As New RadTreeNode("HR Admin")
            HRAdminNode.Name = "HRAdmin"
            HRAdminNode.Value = "HRAdmin"
            HRAdminNode.ImageIndex = 0

            Dim HROrga As New RadTreeNode("Organization Structure")
            HROrga.Name = "OrgaStructure"
            HROrga.Value = "OrgaStructure"
            HROrga.ImageIndex = 1

            rtvMenu.Nodes.Add(HRAdminNode)
            rtvMenu.Nodes.Add(HROrga)

            AddHandler rtvMenu.SelectedNodeChanged, AddressOf rtvMenuSelectedNodeChanged
        End Sub

        Private Sub rtvMenuSelectedNodeChanged(sender As Object, e As Telerik.WinControls.UI.RadTreeViewEventArgs)

            If e.Node.Value = "HRAdmin" Then
                HRAdmin()
            ElseIf e.Node.Value = "OrgaStructure" Then
                HROrganization()
            End If

        End Sub

        Private Sub LoadMenu(modules As List(Of IModule))

            rpvApplication.Pages.Clear()
            rpvContent.Pages.Clear()

            For Each AppModule As IModule In modules
                'register menu

                Dim page = New RadPageViewPage()
                page.Text = AppModule.Name

                Dim tree = New RadTreeView()
                tree.DisplayMember = "Name"
                tree.ChildMember = "AppsMenu\AppChildren"
                tree.DataSource = AppModule.Menus 'menus
                tree.Dock = DockStyle.Fill
                AddHandler tree.SelectedNodeChanged, AddressOf MenuSelectedNodeChanged

                page.Controls.Clear()
                page.Controls.Add(tree)

                rpvApplication.Pages.Add(page)
            Next
        End Sub


        Private Sub MenuSelectedNodeChanged(sender As Object, e As Telerik.WinControls.UI.RadTreeViewEventArgs)

            Dim AppMenu = TryCast(e.Node.DataBoundItem, AppsMenu)

            If AppMenu IsNot Nothing AndAlso AppMenu.Page IsNot Nothing Then
                Dim caption = String.Format("{0} - {1}", AppMenu.AppModule.Name, AppMenu.Name)
                Dim page = rpvContent.Pages.FirstOrDefault(Function(x) x.Text.Contains(caption))

                If page Is Nothing Then

                    Dim control = AppMenu.Page.Content
                    control.Dock = DockStyle.Fill

                    page = New RadPageViewPage()
                    page.Controls.Clear()
                    page.Controls.Add(control)
                    page.Text = String.Format("{0} - {1}", AppMenu.AppModule.Name, AppMenu.Name)

                    rpvContent.Pages.Add(page)

                    Application.DoEvents()
                    AppMenu.Page.LoadContent()
                End If
                rpvContent.SelectedPage = page
            End If
        End Sub

        Private Sub rpvApplication_SelectedPageChanged(sender As Object, e As EventArgs)

            Dim page As RadPageViewPage

            page = rpvApplication.SelectedPage
            If page Is Nothing Then
                Return
            End If
            If page.Controls.Count = 0 Then
                Return
            End If

            Dim control = page.Controls(0)
            If control.GetType = GetType(RadTreeView) Then
                Dim tree = TryCast(control, RadTreeView)

                If tree Is Nothing Then
                    Return
                ElseIf tree.SelectedNode IsNot Nothing Then
                    Dim AppMenu = TryCast(tree.SelectedNode.DataBoundItem, AppsMenu)
                    Dim caption = String.Format("{0} - {1}", AppMenu.AppModule.Name, AppMenu.Name)
                    page = rpvContent.Pages.FirstOrDefault(Function(x) x.Text.Contains(caption))
                    If page Is Nothing Then

                        control = AppMenu.Page.Content
                        control.Dock = DockStyle.Fill

                        page = New RadPageViewPage()
                        page.Controls.Clear()
                        page.Controls.Add(control)
                        page.Text = String.Format("{0} - {1}", AppMenu.AppModule.Name, AppMenu.Name)
                        rpvContent.Pages.Add(page)

                        Application.DoEvents()
                        AppMenu.Page.LoadContent()
                    End If
                    rpvContent.SelectedPage = page

                    tree.SelectedNode.Selected = True
                End If

                If tree.Nodes.Count > 0 Then
                    tree.ExpandAll()
                End If
            End If
        End Sub

        Private Sub frmMain_FormClose(sender As System.Object, e As System.EventArgs) Handles MyBase.FormClosed
            Application.Exit()
        End Sub

        Private Sub HR()
            Dim modules As List(Of IModule)

            modules = New List(Of IModule)()
            modules.Add(New HumanResourceModule)
            modules.Add(New Payroll.Payroll)

            LoadMenu(modules)
        End Sub

        Private Sub Admin()
            Dim modules As List(Of IModule)

            modules = New List(Of IModule)()
            modules.Add(New SecurityModule)
            modules.Add(New ParameterModule)

            LoadMenu(modules)

        End Sub

        Private Sub HROrganization()
            Dim modules As List(Of IModule)

            modules = New List(Of IModule)()
            modules.Add(New HROrganization.Organization)
            LoadMenu(modules)

        End Sub

        Private Sub HRAdmin()
            Dim modules As List(Of IModule)

            modules = New List(Of IModule)()
            modules.Add(New HRParameter.HRParameterModule)
            LoadMenu(modules)

        End Sub

        Private Sub HRSupplies()
            Dim modules As List(Of IModule)

            modules = New List(Of IModule)()
            modules.Add(New HREquipment.HREquipmentModule)
            LoadMenu(modules)

        End Sub


        Private Sub Payroll()
            Dim modules As List(Of IModule)

            modules = New List(Of IModule)()
            modules.Add(New Payroll.PayrollParameter)
            modules.Add(New Payroll.Payroll)
            modules.Add(New Payroll.PayrollPayment)
            LoadMenu(modules)

        End Sub

    End Class
End Namespace
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top