PerformClick from form2

Hi guys,

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

Help?

Thanks
LVL 6
bmquintasAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.

~IM

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
        f1.Show()
    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
        f2.show()
    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
            Button_Reference.PerformClick()
        End If
    End Sub
End Class
0
 
ptakjaCommented:
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
frm2.Show

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 Button.click
    RaiseEvent MyButtonClick
End Sub

Now back to FORM 1
Write an event handler for the frm2.MyButtonClick event and you are good to go.
0
 
armoghanCommented:
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
0
 
bmquintasAuthor Commented:
Thanks ptakja, but it still does nothing. Could it be that the button1 in question is in a TabControl?
0
 
bmquintasAuthor Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.