0

Can we attach an event handler dynamically to a class without creating an instance of an object?

My Scenario

From Main form [Level 0] I am calling sub forms [Level 1], to these sub forms I am able to add dynamic events from the main form. But from the Sub Forms I am again calling forms [level 2]. I cannot add dynamic events from this level [Level 2]. I don't know when a form get call from Level 1, it must be an button click event or any other events.

The below code doesn't work because it requires a instance here frm is an instance

Dim frmLoad = New FormLoadEventHandler(AddressOf On_Load)
t.GetEvent("Load").AddEventHandler(frm, frmLoad)

I am loading individual assemblies into a single platform so each individual assembly has so many sub forms, from my main module i can access the initial form through the reflection. My idea is to make the font same in all forms calling from my platform

  • 3
    I may be missing something but this doesn't make sense to me... Without an instance of a form, no events would be raised. Do you mean without a _reference_ to an instance? If so, then the answer is no - but it's simple to work around - just pass the relevant reference through your code to where it's required (pass `l2frm` to `l0frm` or vice-versa) – Basic Nov 04 '13 at 08:02
  • I am loading individual assemblies into a single platform so each individual assembly has so many sub forms from my main module i can access the initial form through the reflection. My idea is to make the font same in all forms calling from my platform – Santo Puthoor Nov 04 '13 at 08:13
  • This doesn't sound like a clean architecture. What do you mean by "single platform"? Single AppDomain/Process/??? If you have control over the assemblies, you shouldn't need reflection (99% of the time). If you don't have control, changing the font might cause problems (what happens when text overflows?). Reflection is useful only when you're dealing with unknown types - is there a good reason your types are unknown? – Basic Nov 04 '13 at 09:45

2 Answers2

0

Pass the instance of your main Form to your level 2 Forms and add the event handler using the passed reference.

helb
  • 7,609
  • 8
  • 36
  • 58
0

How about just passing the font in....

'Modify a form so the constructor takes a font

Private Property CustomFont As Font
Public Sub New(FontToUse As Font)
    ' This call is required by the designer.
    InitializeComponent()

    'Store the font for later use       
    Me.CustomFont = FontToUse

    'Use the font on all controls as appropriate here
End Sub


Public Sub MakeChildren()
    'Create a child form and tell it which font to use
    Dim ChildForm As New ChildForm(CustomFont)
    ChildForm.Show()
End Sub

etc etc

Basic
  • 26,321
  • 24
  • 115
  • 201
  • Basic...I cannot edit the code, I am using reflection to open the assemblies in my main form like plugin modules. – Santo Puthoor Nov 04 '13 at 10:30
  • Ok, then putting aside for the moment that this is a really bad idea (you have no idea of your changes to font will make the forms unusable), you'll have to use reflection to read the internal variables used to store references to each form - assuming the references are preserved. Failing that, you can use the Win32 Api to find all windows for a process and get their handles ... http://stackoverflow.com/a/79205/156755 It will work but it stinks and really you should get the plugin designers to develop their modules in a standard way that allows you the access you require – Basic Nov 04 '13 at 10:53