By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Activate and Load

P: n/a
Hi

just study the following code...
i have kept a command button and on its click event i have written the
following code:
(assume that there are two forms- form1 and form2 and command button is on
form1)

dim frm as new form2
frm.show
now everytime i click on this button new instance of form2 is created.

now the question is whenever i click that button i dont want to create new
instance always when its already created, i just want that form to be active
whether its hidden or active.

thanks

K
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Try:

Private m_frmForm2 As Form2

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

If m_frmForm2 Is Nothing Then ' First time
m_frmForm2 = New Form2()
m_frmForm2.Show()
ElseIf m_frmForm2.IsDisposed Then ' The form was closed
m_frmForm2 = New Form2()
m_frmForm2.Show()
Else
m_frmForm2.Activate()
End If

End Sub

--

Carlos J. Quintero (Visual Developer - .NET MVP)

The MZ-Tools all-in-one add-in, now for .NET: http://www.mztools.com
"Kishan Hathiwala" <ki****@hathiwala.fateback.com> escribió en el mensaje
news:u0**************@TK2MSFTNGP11.phx.gbl...
Hi

just study the following code...
i have kept a command button and on its click event i have written the
following code:
(assume that there are two forms- form1 and form2 and command button is on
form1)

dim frm as new form2
frm.show
now everytime i click on this button new instance of form2 is created.

now the question is whenever i click that button i dont want to create new
instance always when its already created, i just want that form to be active whether its hidden or active.

thanks

K

Nov 21 '05 #2

P: n/a
It depneds on how you are using form2. Carolos' example will work but it puts all the emphasis on
the Calling form to monitor the restriction. A better solution is what is known as the Singleton
pattern in which you encapulation the "effect" of Carlos' code inside of Form2 itself.

It a very simple pattern to implement:

http://c2.com/cgi/wiki?SingletonPattern

hth
Richard
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.