Solved

Event Handling between forms :oS

Posted on 2006-07-18
6
154 Views
Last Modified: 2010-04-23

Not quite sure of the best way to explain this and whether it is possible.

I have a bar of buttons on a MDI form and a panel  (panel1).  As i load up forms into the panel1 I want this bar of buttons to be used against subs in the currently loaded form.

i.e. if form1 is loaded in panel1 and user clicks the "print" button, this calls form1.print (all forms will have subs for all the buttons in the toolbar)

How do I get from the on_click event for the button (I am creating the buttons on the fly) on the MDI form to calling the event in the form that is currently loaded?

    Private Sub Toolbarbutton_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim f As Form
        For Each f In Panel1.Controls
            If TypeOf f Is Form Then 'Only one form is ever loaded into panel1
                 'This gives me a handle on the form - but i can't do say, f.print.  And as I don't know the "type" of form i can't do CType either can I?
            End If
        Next

    End Sub

0
Comment
Question by:DavidGreenfield
6 Comments
 
LVL 24

Assisted Solution

by:Jeff Certain
Jeff Certain earned 50 total points
ID: 17129552
So you're saying you might have different types of child controls?

You look like you're taking the right approach. I would probably suggest using either an interface or a parent class from which all your forms inherit. This would allow you to specify that every form will have the required subroutines, and allow you to call form.print without knowing the specific type, since you know the interface type.
0
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 17129571
       Dim f As Form
        For Each f In Panel1.Controls
            If TypeOf f Is Form Then 'Only one form is ever loaded into panel1
                 If TypeOf f is MyChildFormClass then
                       CType(f, MyChildFormClass).Print
                 ElseIf TypeOf f Is MyChildFormClass2 Then
                       CType(f, MyChildFormClass2).Open
                 End If
            End If
        Next

Not very comfortable, but I think it's the only think you can do here
0
 
LVL 14

Accepted Solution

by:
jjardine earned 450 total points
ID: 17129584
Create an Interface that defines all of your subs you want available.    Have all of your forms implement the interface.   Then in your loop above   check to see if it is a type of your interface.  If so then cast it to the interface and call the method.

The code shown is partial.  Just to show the interface and how to implement it..  I did not provide all code for Form1.  I hope this helps get you in the right direction.

'''Interface Definition
Interface IMyForm
  Sub Print()
  'Add other methods here.
End Interface

'''Form
Class Form1
   Implements IMyForm

   Public Sub Print() Implements IMyForm.Print
     'Do Printing here.
  End Sub

 Private Sub Toolbarbutton_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim f As Form
        For Each f In Panel1.Controls
            If TypeOf f Is Form Then 'Only one form is ever loaded into panel1
                If TypeOf f Is IMyForm Then
                  DirectCast(f,IMyForm).print
                End If
                 'This gives me a handle on the form - but i can't do say, f.print.  And as I don't know the "type" of form i can't do CType either can I?
            End If
        Next

    End Sub
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17129586
Ram,

Not true. An interface would solve the problem. As an added benefit, it would be more maintainable and extensible. The code you suggest is not at all maintainable, and will be quite a nuisance to code in the first place if there are many different form types.
0
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 17129625
Jeff, I take my words back - didn't think of interfaces at all . Agree 100%.
0
 

Author Comment

by:DavidGreenfield
ID: 17129841
jjardine your example works like a dream!  Thanks everyone else!!

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Error handling in asp.net site 5 34
start a process from a service 3 33
Generate Unique ID in VB.NET 21 68
MYSQL responding very slow 3 26
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

820 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