tags:

views:

11

answers:

2

very basic question. I have 3 forms. one main form with two buttons, that need to open one of the other two forms on clicking the button. Now when say button 2 is clicked then form 2 should open and also form form 2 person should be able to click back and come to main form. how can i do this?

A: 

Little hazy on VB but this should be good enough :)

   On click of button that shows form2 [Modified]

Dim frmOne as Form1
frmOne = Me

Dim frmTwo as Form2
    frmTwo = new Form2(frmOne)
    frmTwo.show()

Note: Form2 should have a constructor that takes form1 object.

To come back place a button on Form2 and pass the object of first form to form2.
me.hide() or me.visible = false
frmOne.show()
Sidharth Panwar
A: 

In the calling form, declare a reference to the called form, and use the withevents keyword if you want to trap the form's events (like form_closing)

Public Class MDIMain
    Private WithEvents _cases As frmGrid

then, when they click on something to open the second form, create a new instance of it:

Private Sub mnuViewCaseFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewCaseFiles.Click
    If IsNothing(_cases) Then
        _cases = New frmGrid
        _cases.WindowState = FormWindowState.Maximized
    End If
    _cases.Visible = Me.mnuViewCaseFiles.Checked
End Sub

then you can handle the second form's onclosing event:

Private Sub _cases_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles _cases.FormClosing
    _cases = Nothing
    mnuViewCaseFiles.Checked = False
End Sub
Beth