Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Windows Forms

Declarations Generator

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
29 Jun 2010GPL33 min read 19.4K   127   5  
A tool which helps in generating codes for events/properties, so that users of your classes can use events/properties of embedded classes
Imports System.Reflection

Public Class frm_Main


#Region "Form Events"
    Private Sub frm_Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.status_label.Text = "Populating Tree with Assemblies & Types Names ....."
        Me.Show()
        PopulateTree()
        Me.status_label.Text = "Ready"
        pnl_Controls.Enabled = True
        Me.ToolStrip_Controls.Enabled = True
    End Sub

#End Region

#Region "Controls Events"

    Private Sub lbl_EventsNames_lbl_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbl_EventsPropertiesNames_lbl.TextChanged
        ToolTip.SetToolTip(lbl_EventsPropertiesNames_lbl, lbl_EventsPropertiesNames_lbl.Text)
    End Sub

    Private Sub txt_CtrlName_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_CtrlName.Validated
        txt_CtrlName.Text = txt_CtrlName.Text.Trim
    End Sub

    Private Sub Rad_GenEvents_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rad_GenEvents.CheckedChanged
        PopulatePropertiesEvnetsList()
    End Sub

#Region "Tool Strip"

    Private Sub ToolStripButton_About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_About.Click
        AboutBox.ShowDialog()
    End Sub

    Private Sub ToolStripBtn_GenDeclarations_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripBtn_GenDeclarations.Click
        If Me.txt_CtrlName.Text.Trim = "" Then
            MsgBox("Control Name must be set, before Generation starts")
            Me.txt_CtrlName.Focus()
            Exit Sub
        End If

        Dim T As Type = CType(chkLst_EventsNames.Tag, Type)
        If T Is Nothing Then Exit Sub

        Me.status_label.Text = "Generating Events Declarations......."
        Dim Output As String = ""
        Dim CtrlName = txt_CtrlName.Text
        Dim Members2Generate = From chkItem As String In chkLst_EventsNames.CheckedItems
        If Rad_GenEvents.Checked Then
            Output = T.GenerateEventsDeclarations(CtrlName, Members2Generate)
        Else
            Output = T.GeneratePropertiesDeclarations(CtrlName, Members2Generate)
        End If

        Me.status_label.Text = "Ready"
        Dim CodeViewerForm As New frm_CodeViewer() With {.GeneratedCode = Output}
        CodeViewerForm.ShowDialog()

    End Sub

    Private Sub ToolStripBtn_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripBtn_Close.Click
        Close()
    End Sub

#End Region

#Region "trv_Classes"

    Private Sub trv_Classes_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles trv_Classes.KeyUp
        If e.KeyCode = Keys.Return Then
            PopulatePropertiesEvnetsList()
        End If
    End Sub

    Private Sub trv_Classes_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles trv_Classes.NodeMouseDoubleClick
        PopulatePropertiesEvnetsList()
    End Sub

#End Region

#End Region

#Region "Added Methods"
    Private Sub PopulateTree()

        Dim Asses = System.AppDomain.CurrentDomain.GetAssemblies

        For Each ass In (From a In Asses Select a Order By a.GetName.Name)

            If ass.GetName.Name = Assembly.GetExecutingAssembly.GetName.Name Then
                Continue For
            End If

            Dim tvnAss As New TreeNode(ass.GetName.Name) ', New TreeNode() {}) '= trv_Classes.Nodes.Add(ass.GetName.Name, ass.GetName.Name)
            Application.DoEvents()
            For Each T In (From AssT In ass.GetTypes _
                           Where AssT.IsClass And AssT.GetEvents.Count > 0 _
                           Select AssT Order By AssT.Name)

                Dim tvn_T As TreeNode = tvnAss.Nodes.Add(T.Name, T.Name, 1, 1)
                tvn_T.Tag = T
            Next

            If tvnAss.Nodes.Count > 0 Then trv_Classes.Nodes.Add(tvnAss)
        Next

    End Sub

    Sub PopulatePropertiesEvnetsList()

        If trv_Classes.SelectedNode Is Nothing Then Exit Sub

        Dim T As Type = CType(trv_Classes.SelectedNode.Tag, Type)

        If T Is Nothing Then
            Me.lbl_EventsPropertiesNames_lbl.Text = "Choose a node that Represents a Type"
            Exit Sub
        End If

        Dim ListNewItems As IEnumerable(Of String)
        If Rad_GenEvents.Checked Then

            If T.GetEvents.Count = 0 Then GoTo SomethingWrong
            Me.lbl_EventsPropertiesNames_lbl.Text = "Events of type '" & T.Name & "'"
            ListNewItems = From Ev In T.GetEvents Select Ev.Name

        ElseIf Rad_GenProperties.Checked = True Then

            If T.GetProperties.Count = 0 Then GoTo SomethingWrong
            Me.lbl_EventsPropertiesNames_lbl.Text = "Propertys of type '" & T.Name & "'"
            ListNewItems = From Prop In T.GetProperties Select Prop.Name
        Else
            ListNewItems = Enumerable.Repeat(Of String)("", 1)
            Exit Sub
        End If

        Me.chkLst_EventsNames.Tag = T
        Me.chkLst_EventsNames.Items.Clear()
        Me.chkLst_EventsNames.Items.AddRange(ListNewItems.ToArray)


        Exit Sub
SomethingWrong:
        Me.lbl_EventsPropertiesNames_lbl.Text = "Seems like the type you selected doesn't have Events/Properties."

    End Sub
#End Region

#Region "SplitBttn_CheckOptions"


    Private Sub SplitBttn_CheckOptions_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SplitBttn_CheckOptions.ButtonClick
        ToolStripMnu_CheckAll.PerformClick()
    End Sub

    Private Sub ToolStripMnu_CheckAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMnu_CheckAll.Click
        For i = 0 To chkLst_EventsNames.Items.Count - 1
            chkLst_EventsNames.SetItemChecked(i, True)
        Next
    End Sub

    Private Sub ToolStripMnu_UnCheckAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMnu_UnCheckAll.Click
        For i = 0 To chkLst_EventsNames.Items.Count - 1
            chkLst_EventsNames.SetItemChecked(i, False)
        Next
    End Sub

    Private Sub ToolStripMnu_InvertCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMnu_InvertCheck.Click
        For i = 0 To chkLst_EventsNames.Items.Count - 1
            chkLst_EventsNames.SetItemChecked(i, _
                                              Not chkLst_EventsNames.GetItemChecked(i) _
                                               )
        Next
    End Sub
#End Region


End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
Syrian Arab Republic Syrian Arab Republic
The more I learn the more I see my ignorance.

Comments and Discussions