Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Declarations Generator

4.50/5 (2 votes)
29 Jun 2010GPL33 min read 20K   127  
A tool which helps in generating codes for events/properties, so that users of your classes can use events/properties of embedded classes

Introduction

If you're developing a [User Control] (or [Class]),
If your [User Control] contains other [Controls] (Button, TextBox, Label, .......etc.)
If you wanted to expose Events/Properties of embedded controls,

You have two choices:

  1. Re-write a declaration of every Event you need, and raise them, one by one. If you want to expose all Events/ Properties, that's going to take forever. What if you have more than one Embedded Control, then you start and your student will complete it.
  2. Make the "Accessible Level" (Modifier) of the embedded controls so they're permitted to be seen out of your [User Control], like "Public".
    Then users of your [User Control] will have to write something like this to use Events:
    VB.NET
    Dim WithEvents EmbeddedControl As New EmbeddedControlType
    
    Private Sub SomeSub()
    
        'now " UserControl1" is an object of Your UserControlType
        EmbeddedControl = UserControl1.EmbeddedControl
    End Sub
    
    Private Sub HandlerSub(ByVal Sender As Object, ByVal e As EventArgs) _
            Handles EmbeddedControl.EventName
    
            'do your thing 
    End Sub

    Notice that the user has to be aware of every single thing he is doing, because IDE won't help him creating the "Sub" "HandlerSub",………… besides that he made unnecessary things.

So …………..
Here's a tool which helps you in Generating Declarations of Events/Properties for Embedded Controls (Option number 1), so users of your [User Control] will have to write something like this to use Events:

VB.NET
Private Sub UserControl1_EmbeddedControlEventName( _
    ByVal Sender As Object,  ByVal e As System.EventArgs) _
    Handles UserControl1. EmbeddedControlEventName

        'do your thing
    End Sub

Besides that, the user will use the IDE to create the "Sub" "Tool_EmbeddedToolEventName".

Here is a screenshot of the tool:

DeclarationsGenerator.gif

Future Ideas

  1. Generate Properties/Events Attributes and XML-Commenting (process has started, but results are not satisfying at all in the current version).
    I could wait till the tool is ready, but I chose not to wait, because I'm not going to develop this idea anytime soon, and I wanted people to use now it if they liked it (not to wait for me).
  2. Searching in other Assemblies by allowing user to add Assemblies to search for Data Types into, it won’t take much time to program, but I didn’t support it yet.

Background

Obviously, the tool is all about Reflection, and it needs the Namespace System.Reflection to be imported:

VB.NET
Imports System.Reflection

One more thing, I used Extension Methods to make the code architecture more convenient and more robust, and it needed the Namespace System.Runtime.CompilerServices to be imported:

VB.NET
Imports System.Runtime.CompilerServices

Using the Code

For example, the code that is responsible for generating the Declaration of an Event is:

VB.NET
#Region "Extending the 'EventInfo' Type"
    <Extension()> _
     Private Function GetEventDeclaretion(ByVal eve As EventInfo, _
                                          ByVal ControlName As String _
                                          ) As String

        Dim DelegateType As Type = eve.EventHandlerType
        Dim Invoke As MethodInfo = DelegateType.GetMethod("Invoke")
        Dim Pars As ParameterInfo() = Invoke.GetParameters()

        Dim EventName As String = ControlName & eve.Name

        Dim ParamsNamesArr = (From p In Pars Select p.Name).ToArray
        Dim ParamsNamesStr As String = " ( " & String.Join(" , ", ParamsNamesArr) & " )"

        Dim paramsDeclarationsArr = (From p In Pars _
                                     Select (p.Name & " As " & p.ParameterType.ToString) _
                                    ).ToArray
        Dim paramsDeclarationsStr As String = " ( " & String.Join(" , ", _
			paramsDeclarationsArr) & " )"

        Dim EventInvokeMethodeName As String = String.Format("{0}{1}{2}", _
                                    ControlName, eve.Name, paramsDeclarationsStr)

        Dim RaiseEventStatment As String = vbTab & "RaiseEvent " & _
				EventName & ParamsNamesStr

        Dim EventDeclaration As String = "Public Event " & _
				EventInvokeMethodeName & vbCrLf
        EventDeclaration &= String.Format("Private Sub m_{0} _
				Handles {1}.{2}{4}{3}{4}End Sub{4}", _
                                      EventInvokeMethodeName, ControlName, eve.Name, _
                                      RaiseEventStatment, vbCrLf)
        Return EventDeclaration
    End Function
#End Region

So later, you can use it as next:

VB.NET
Dim Eve As EventInfo = GetType(SomeType).GetEvent("SomeEvent")
Dim Declaration As String = Eve.GetEventDeclaretion()

History

The idea was created a while ago when I wanted to make a UserControl which contains the famous combination (BrowseButton + PathTextBox). I wanted users to have access to embedded controls members. I searched all over the internet, everybody said that I have to hardcode them, so I made this tool, and used it. It was like magic to me.

Some days ago, I wanted to share this tool with you guys, so some future ideas came to my mind, but because I don't intend to develop it anytime soon, I put it the way it was.

Version 0.0.0.3 generates Events and Properties (recommended, stable). I started to support Attributes generating in Version (0.0.0.4), but results was not satisfying at all (choosing the right constructor isn't working).

I hope it helps somebody. Thanks for reading.

License

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