Click here to Skip to main content
13,903,256 members
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 25 Jun 2010
Licenced GPL3

Declarations Generator

, 29 Jun 2010
Rate this:
Please Sign up or sign in to vote.
A tool which helps in generating codes for events/properties, so that users of your classes can use events/properties of embedded classes


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:
    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:

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:


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.


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

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:

Imports System.Runtime.CompilerServices

Using the Code

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

#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) _
        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:

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


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 generates Events and Properties (recommended, stable). I started to support Attributes generating in Version (, but results was not satisfying at all (choosing the right constructor isn't working).

I hope it helps somebody. Thanks for reading.


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


About the Author

Yasser Daheek
Software Developer
Syrian Arab Republic Syrian Arab Republic
The more I learn the more I see my ignorance.

You may also be interested in...

Comments and Discussions

GeneralGood Pin
borchanii6-Jul-10 21:48
memberborchanii6-Jul-10 21:48 
GeneralRe: Good Pin
Yasser Daheek7-Jul-10 2:08
memberYasser Daheek7-Jul-10 2:08 
General.. cannot download .. Link broken Pin
tridex29-Jun-10 3:06
membertridex29-Jun-10 3:06 
GeneralRe: .. cannot download .. Link broken Pin
Yasser Daheek29-Jun-10 6:21
memberYasser Daheek29-Jun-10 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 29 Jun 2010
Article Copyright 2010 by Yasser Daheek
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid