Activate and Load

  • Thread starter Thread starter Kishan Hathiwala
  • Start date Start date
K

Kishan Hathiwala

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
 
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
 
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
 

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

Back
Top