Solved

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

Posted on 2009-05-14
14
296 Views
Last Modified: 2013-11-12
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
Comment
Question by:Zeus2009
  • 6
  • 5
  • 3
14 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
 

Author Comment

by:Zeus2009
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 

Author Comment

by:Zeus2009
Comment Utility
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
Comment Utility
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 150 total points
Comment Utility
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
Comment Utility
*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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:Zeus2009
Comment Utility
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
 

Author Comment

by:Zeus2009
Comment Utility
@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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 

Author Comment

by:Zeus2009
Comment Utility
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
 

Author Comment

by:Zeus2009
Comment Utility
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
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Change the parameter type to Object. Then use type casting on both Form and MyForm to match them.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Before You Read The Article Please make sure you understand these two concepts: Variable Scope (http://www.php.net/manual/en/language.variables.scope.php) and Property Visibility (http://www.php.net/manual/en/language.oop5.visibility.php).  And to …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now