Click here to Skip to main content
15,881,680 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   4
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)


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

Comments and Discussions

 
GeneralGood Pin
borchanii6-Jul-10 21:48
borchanii6-Jul-10 21:48 
GeneralRe: Good Pin
Yasser Daheek7-Jul-10 2:08
Yasser Daheek7-Jul-10 2:08 
General.. cannot download .. Link broken Pin
tridex29-Jun-10 3:06
tridex29-Jun-10 3:06 
GeneralRe: .. cannot download .. Link broken Pin
Yasser Daheek29-Jun-10 6:21
Yasser Daheek29-Jun-10 6:21 
thanks very much for the tip (there was some typo in the link)

that means that no one yet tried to download the code WTF | :WTF: Confused | :confused:

I know it's not pleasing yet (I may put the uncompleted version 4)

thanks again
Name: Yasser Daheek AKA vbnetskywalker
B Date: 1/April/1987
Home: Syria - Ham
Phone: 963967398199
Job: Hope to be a programmer ,
but to people ... seems like it's not a job

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.