Solved

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

Posted on 2009-05-14
14
303 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
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 85

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 85

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 85

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 85

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 85

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Introduction Many of the most common information processing tasks require sorting data sets.  For example, you may want to find the largest or smallest value in a collection.  Or you may want to order the data set in numeric or alphabetical order. …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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