Solved

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

Posted on 2009-05-14
14
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24385241
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
ID: 24385352
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 86

Expert Comment

by:Mike Tomlinson
ID: 24387643
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Zeus2009
ID: 24389996
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 24390219
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
ID: 24390240
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 24390349
*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
 

Author Closing Comment

by:Zeus2009
ID: 31581357
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
ID: 24400034
@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 86

Expert Comment

by:Mike Tomlinson
ID: 24400057
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
ID: 24400568
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
ID: 24400594
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
ID: 24400630
Change the parameter type to Object. Then use type casting on both Form and MyForm to match them.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24400658
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

705 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