PerformClick from form2

Posted on 2004-10-28
Last Modified: 2006-11-17
Hi guys,

I've got Form1 as MDIChild with a Button1. I need to Button1.PerformClick from Form2. Form2 is a DataForm.


Question by:bmquintas
    LVL 14

    Expert Comment

    You mean you want to trap a button click on Form One from Form2? Is Form2 a child of Form1?

    If so, you I suggest this approach:

    In FORM 1

    Private WithEvents frm2 As Form2

    Then when you want to call up form 2:
    frm2 = New Form2

    In FORM 2 add a custom event:

    Public Event MyButtonClick()

    In form 2's button click event handler you have this:

    Private Sub Button.Click(sender as object, e as eventargs) Handles
        RaiseEvent MyButtonClick
    End Sub

    Now back to FORM 1
    Write an event handler for the frm2.MyButtonClick event and you are good to go.
    LVL 18

    Expert Comment

    easiset way is pass form1's object to form2 and on any instance call the function Button1.PerformClick with null parameters, if there is no processing in the function
    LVL 6

    Author Comment

    Thanks ptakja, but it still does nothing. Could it be that the button1 in question is in a TabControl?
    LVL 6

    Author Comment

    This is the whole picture:

    Form0 is a mdicontainer therefore mdiParent
    Form1 is mdichild of form0
    and form2 is called from form1

    It only works if Form1 is NOT mdichild of Form0.

    ANy solutions?
    LVL 85

    Accepted Solution

    Below is how it should look.  When Form0 loads, it loads Form1 as an MDI child.  There are two buttons on Form1.  The first opens up Form2 as a child.  The second simply displays a MsgBox that says it was clicked.  On Form2, there is one button that when clicked, causes the second button on Form1 to be clicked.


    Public Class Form0
        Inherits System.Windows.Forms.Form

        Private Sub Form0_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim f1 As New Form1
            f1.MdiParent = Me
        End Sub
    End Class

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f2 As New Form2
            f2.MdiParent = Me.MdiParent
            f2.Button_Reference = Me.Button2
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            MsgBox("Button2 on Form1 Clicked")
        End Sub
    End Class

    Public Class Form2
        Inherits System.Windows.Forms.Form

        Public Button_Reference As Button

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not (Button_Reference Is Nothing) Then
            End If
        End Sub
    End Class

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    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 …
    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 …
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles al…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    856 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