Solved

Event Handling between forms :oS

Posted on 2006-07-18
6
150 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

16 Experts available now in Live!

Get 1:1 Help Now