Solved

Event Handling between forms :oS

Posted on 2006-07-18
6
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

719 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