[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Event Handling between forms :oS

Posted on 2006-07-18
6
Medium Priority
?
159 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 200 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 1800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

834 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