Solved

Event Handling between forms :oS

Posted on 2006-07-18
6
151 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

24 Experts available now in Live!

Get 1:1 Help Now