• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

What is th best way for me to create an event handler (VB.net)

Hi All,

I know there is allot of subject matter on the question im going to ask...but... there is so mcuh web sh** that i cant find a decent example that does what it says on the box.

So with that in mind i wanted to ask the senior devs for advice on the following.

Q. Im in design stage of a massive VB.net project, with lots of forms menus etc, in the past i have always created a module with my globaly shared events in, i then map the events in the required form i.e "add handler", then when required i raise the event passing whatever i need at the time for a particualar method to work correctly allot of the time they are bools, ints and vars.

In one of my previous projects i had like nearly 100 shared events in a module, is this safe? the apps have always run quickly as yo would expect but im just curious of what i have explained is the best way for me to achieve a fast code safe app.

Again i have never had any problems with the way i have done things but i wanted to read the thoughts of others on the subject as there is way too much ambiguity on this particular subject.

Thanks

p.s if you reply please post example so that other readers can take advantage of your knowledge.
0
Zeus2009
Asked:
Zeus2009
  • 6
  • 5
  • 3
3 Solutions
 
CodeCruiserCommented:
Would you be doing Object Oriented Programming? Would you be using Classes and objects? What data access technique would you use? What sort of event these are?
0
 
Zeus2009Author Commented:
Hi ,

Allot of the app is OOP the ntier data access layer is created using lblGenPro so generally it takes care of itself in relation to architecture it works in a Top Down hierachy with classes for managing most data relation procs the rest is generate dyanimically from xml.

My question really should of been, if i have multiple forms in an mdi and menus in the parent mdi what is the best way to send commands from the menus in the parent mdi to the children and vice versa?

Thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
With respect to MDI apps, you can easily get the active mdi child via "Me.ActiveMdiChild" and cast it to an appropriate type to take action on it.  If you have a common method that needs to be called against any mdi child then you can either attempt a call via reflection or make all of your mdi children implement an Interface (you can also use the legacy CallByName() method...usually frowned upon though).

Going from child to parent is even easier since you almost always know the type of the mdi parent.  Just cast the MdiParent() prop in the child to the parent type and make your call...
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Zeus2009Author Commented:
Hi ,
Thanks for the reply, i tried the "TryCast(ActiveChild, frm_SomeForm).DoSomething" but there is obviously more to it than that i just thought you could cst and call directly.

I did however have an idea but im unsure of the consequences, if i have a module with a Public f_Form as frm_MyForm then when i create an instance of the my "frm_MyForm" object i assign the public f_Form with the instance reference i can then access it globally throught the entire app, is this code safe?

Please could you show an example of casting to a form that has a
Private sub ShowMessage
msgbox("My Direct Cast")
End Sub

Thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It works for me...

Simple case example:

Public Class Form1 ' <-- The MDI PARENT

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f2 As New Form2
        f2.MdiParent = Me
        f2.Show()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        TryCast(Me.ActiveMdiChild, Form2).Foo()
    End Sub

End Class

Public Class Form2 ' <-- The MDI CHILD

    Public Sub Foo()
        Label1.Text = DateTime.Now
        MessageBox.Show("Hello World!")
    End Sub

End Class
0
 
CodeCruiserCommented:
You can use the GetType method to see what type of form it is and then do the cast.

For Each F in me.MDIChildren
      If TypeOf(F) IS frmOrders Then
         CType(F, frmOrders).ShowMessage
      End If
Next
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*TryCast would be safer used like this though:
(we're supposed to check for a return of Nothing)

        Dim f2 As Form2 = TryCast(Me.ActiveMdiChild, Form2)
        If Not IsNothing(f2) Then
            f2.Foo()
        End If

Or using CodeCruiser's approach with the Active Child:

        If TypeOf Me.ActiveMdiChild Is Form2 Then
            DirectCast(Me.ActiveMdiChild, Form2).Foo()
        End If

0
 
Zeus2009Author Commented:
Thanks for the help guys, and also i have split the points purely because both replys where direct and correct in description.

thanks again
0
 
Zeus2009Author Commented:
@idle mind and crusier - p.s i forgot to ask earlier in the post you mentioned it was easy to cast from the mdi child to its parent can you post a quick sample of that please because i have just tried a little test and my app bombed out.

Thanks for your help
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sure...in this example, Form1 is the MdiParent, and Form2 is the MdiChild:

Public Class Form2
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f1 As Form1 = CType(Me.MdiParent, Form1)
        f1.Text = "Hello form an MdiChild!"
    End Sub
 
End Class

Open in new window

0
 
Zeus2009Author Commented:
Thanks thats great, i was using  Dim f1 As Form1 = CType(myParentForm, Form1) but it wouldnt execute the sub but it works fine now

Cheers

0
 
Zeus2009Author Commented:
Ok one more and i am being cheeky here "well i feel like it anyway becasue i have already accepted your ansers, but i am trying to write a function that can be used as a class to return a form evaluated by a form passed as bellow, but it wont accept "MYFORM" as a type, maybe its my lack of knowledge when it comes to type casting, can you see anything emmediately obvious that i could do to achieve a function like this.

  Private Function Find_EWS(ByVal MYFORM as form) As Form
        For Each Form As Form In Me.MdiChildren
            If TypeOf (Form) Is MYFORM Then
                Return Form
            End If
        Next
        Return Nothing
    End Function

p.s i promise this is the last question.

Thanks
0
 
CodeCruiserCommented:
Change the parameter type to Object. Then use type casting on both Form and MyForm to match them.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I also wouldn't use "Form" as the variable name...using the Type Name as the Variable Name leads to confusion and sometimes subtle errors....

...change that to "frm" or something.  =)


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now