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:
- 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.
- 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()
EmbeddedControl = UserControl1.EmbeddedControl
End Sub
Private Sub HandlerSub(ByVal Sender As Object, ByVal e As EventArgs) _
Handles EmbeddedControl.EventName
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
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
- 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).
- 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:
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) _
).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:
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.