Click here to Skip to main content
15,919,423 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: How to set font size of a control dynamically Pin
AliAmjad22-Oct-07 22:37
AliAmjad22-Oct-07 22:37 
AnswerRe: How to set font size of a control dynamically Pin
Mitesh Darji23-Oct-07 3:13
Mitesh Darji23-Oct-07 3:13 
QuestionSend a value to another window's application Text box Pin
Arum7322-Oct-07 20:22
Arum7322-Oct-07 20:22 
AnswerRe: Send a value to another window's application Text box Pin
AliAmjad22-Oct-07 22:44
AliAmjad22-Oct-07 22:44 
AnswerRe: Send a value to another window's application Text box Pin
Dave Kreskowiak23-Oct-07 2:15
mveDave Kreskowiak23-Oct-07 2:15 
Questionreports Pin
Jaiwant122-Oct-07 19:28
Jaiwant122-Oct-07 19:28 
AnswerRe: reports Pin
pmarfleet22-Oct-07 21:16
pmarfleet22-Oct-07 21:16 
QuestionI need help creating a Dynamic Method Call (Event Handler) Pin
Motobahta22-Oct-07 12:22
Motobahta22-Oct-07 12:22 
I am trying to write an object whose purpose is to act as a Generic Event Handler. This object has two methods. Register, and Listener

- The first is called Register(inWebControl, inSubscriber). This takes in a WebControl and creates a Dynamic Method to handle specific Events. This Dynamic Method calls the second method.
//basically just call another generic handler Hmmm | :| -
//Note the Web Controls are eitehr home-grown or 3rd Party or .Net
//all with different Arg Types


- The second method is a Sub that takes a KeyValue as String (generated in the Dynamic Method) the Sender of the Event, and the specific EventArgs Object.

My Code...

Public Class EventManager<br />
<br />
 //Register Method <br />
Public Sub Register(inWebControl as WebControl, inSubscriber as SubscribingObject)<br />
     <br />
 //Event Listener <br />
Public Shared Sub Listener(inKey As String, Sender As Object, ParamArray Args() As Object)<br />
<br />
End Class


I've seen other examples of this in the C# arena, which I have been able to glean as much as possible to get it to half-way work. I really need some help though in the following areas and specifically how do some of this in VB.net


1. The code below works Smile | :) as long as the Object and method is Shared (Static)Sniff | :^) , but I would really like to make it work as an instance method.
2. The EventListener method is firing Big Grin | :-D and I get the Key and the Sender Big Grin | :-D , but never get the Arguments Sigh | :sigh: .

With all that said -(hopefully enough to trigger help Confused | :confused: ). Here is the current code that I have that works (and a brief statement of what I "think" is happening).


Public Sub Register(ByVal inControl As WebControl, ByVal inSubscriber As clsWebBinder)

// The Key is what I use to determine who the Subscriber is
Dim Key As String = inSubscriber.Key<br />
Dim BindType As System.Type


//First Generate a Type - I've generated a Module in Memory and now somewhat realize I don't
//need the module or this type, I can just create a dynamic method - but still don't have the
//big picture yet.
Dim m_EventInfo As EventInfo = inControl.GetType.GetEvent(inSubscriber.TargetEventName)

//This is determining the parameters and the data necessary to create the dynamic event
//handling method. Is there a way to debug a dynamic method?
Dim InvokeMethod As MethodInfo = m_EventInfo.EventHandlerType.GetMethod("Invoke")<br />
Dim parms As ParameterInfo() = InvokeMethod.GetParameters()<br />
Dim ParmTypes(parms.Length - 1) As Type<br />
Dim i As Integer<br />
For i = 0 To parms.Length - 1<br />
<br />
     ParmTypes(i) = parms(i).ParameterType<br />
<br />
Next i<br />
<br />
Dim tB As TypeBuilder = MyModule.DefineType(Key, TypeAttributes.Class Or TypeAttributes.Public)


Confused | :confused:
//Now I create the Dynamic Method on that Type
//I realize this next section is setup for Static, but it is the only way I know to
// make it work... I really don't have a good understanding of what is going on here
// and couldn't really find any decent help (in vB)

Dim handler As MethodBuilder = tB.DefineMethod("DynamicHandler", MethodAttributes.Public Or MethodAttributes.Static, InvokeMethod.ReturnType, ParmTypes)<br />
<br />
Dim iLG As ILGenerator = handler.GetILGenerator()


//this Emit method loads my Key as the String Value
iLG.Emit(OpCodes.Ldstr, inSubscriber.Key)

//I think this loads the Sender?
iLG.Emit(OpCodes.Ldarg_0)

//I'm not sure what this loads (perhaps the Sender as well?)
iLG.Emit(OpCodes.Ldarg_1)

//I've tried adding a Ldarg_2 to get the arguments, but I get the Mad | :mad: "Invalid Program" Mad | :mad: error.


//This executes the Call to Listener and then exits the method
iLG.Emit(OpCodes.Call, Me.GetType().GetMethod("Listener"))<br />
iLG.Emit(OpCodes.Ret)


// This generates the Actual Method and Type in the Dynamic Module
BindType = tB.CreateType

// Now I create a Delegate and Add a Handler
Dim EventHandler As MethodInfo = BindType.GetMethod("DynamicHandler")<br />
Dim dM As [Delegate] = [Delegate].CreateDelegate(m_EventInfo.EventHandlerType, EventHandler)<br />
<br />
m_EventInfo.AddEventHandler(inControl, dM)<br />
<br />
End Sub


Any HELP would be greately appreciated!
AnswerRe: I need help creating a Dynamic Method Call (Event Handler) Pin
Dave Kreskowiak23-Oct-07 2:13
mveDave Kreskowiak23-Oct-07 2:13 
GeneralRe: I need help creating a Dynamic Method Call (Event Handler) Pin
Motobahta23-Oct-07 5:41
Motobahta23-Oct-07 5:41 
GeneralRe: I need help creating a Dynamic Method Call (Event Handler) Pin
Dave Kreskowiak25-Oct-07 5:31
mveDave Kreskowiak25-Oct-07 5:31 
Questionreadxml slowdown Pin
cstrader23222-Oct-07 11:46
cstrader23222-Oct-07 11:46 
AnswerRe: readxml slowdown Pin
Tom Deketelaere22-Oct-07 22:32
professionalTom Deketelaere22-Oct-07 22:32 
AnswerRe: readxml slowdown Pin
Dave Kreskowiak23-Oct-07 2:11
mveDave Kreskowiak23-Oct-07 2:11 
GeneralRe: readxml slowdown Pin
cstrader23223-Oct-07 2:53
cstrader23223-Oct-07 2:53 
GeneralRe: readxml slowdown Pin
Dave Kreskowiak23-Oct-07 3:42
mveDave Kreskowiak23-Oct-07 3:42 
QuestionRandom Access File VS. Stream Reader Objects Pin
AAGTHosting22-Oct-07 11:34
AAGTHosting22-Oct-07 11:34 
AnswerRe: Random Access File VS. Stream Reader Objects Pin
Guffa22-Oct-07 22:46
Guffa22-Oct-07 22:46 
QuestionVB 2005 include a .exe file Pin
FloridaHillBilly22-Oct-07 11:04
FloridaHillBilly22-Oct-07 11:04 
AnswerRe: VB 2005 include a .exe file Pin
svanwass22-Oct-07 11:18
svanwass22-Oct-07 11:18 
GeneralRe: VB 2005 include a .exe file Pin
FloridaHillBilly22-Oct-07 13:15
FloridaHillBilly22-Oct-07 13:15 
GeneralRe: VB 2005 include a .exe file Pin
Tom Deketelaere22-Oct-07 22:39
professionalTom Deketelaere22-Oct-07 22:39 
QuestionCanceling background worker Pin
svanwass22-Oct-07 9:58
svanwass22-Oct-07 9:58 
AnswerRe: Canceling background worker Pin
Dave Kreskowiak23-Oct-07 2:08
mveDave Kreskowiak23-Oct-07 2:08 
QuestionConnection string in VB.Net 2003 Pin
kendo1722-Oct-07 7:12
kendo1722-Oct-07 7:12 

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.