Close the opened form

A

anthony

Hi,

I have a button (Button1) to open the main form. If I want to have another
button to close this opened form, how do I do it without making Mainform a
global variable?

Thanks!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim MainFrom As New frmMain

MainFrom.Show()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

' Close the form opened by Button1

End Sub
 
H

Herfried K. Wagner [MVP]

anthony said:
I have a button (Button1) to open the main form. If I want to have
another
button to close this opened form, how do I do it without making Mainform a
global variable?

\\\
Private m_MainForm As MainForm
..
..
..

' In the open button's 'Click' event handler...
If m_MainForm IsNothing OrElse m_MainForm.IsDisposed Then
m_MainForm = New MainForm()
m_MainForm.Show()
End If
..
..
..

' In the close button's 'Click' event handler...
If Not m_MainForm Is Nothing AndAlso Not m_MainForm.IsDisposed Then
m_MainForm.Close()
End If
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top