Is this form already open?

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am opening a dialog using;

frmdialog = New frmdialognew
frmdialog.ShowDialog()

Is there a way to see if this dialog is already open to avoid opening
multiple instances of it?

Thanks

Regards
 
August 8, 2004

Logically, since you are using the ShowDialog method, you can't
get back to the button or object to reopen the dialog again without
first closing the open one. If you have it in a button click event, you
would be creating a new instance of the dialog everytime you click
the button. Now if you were using the Show method, you could
check to see if it is open, if your design is a certain way.
You could evaluate the Form.Visible property and see if it is visible.
I do not know whether you want to see if it has been disposed of,
or just hidden from view. If it has been disposed of, you would not
know. This is where the design comes in. If you do not have any
code that disposes of the form then you can call the Show method
without showing multiple forms. If, however, the user closes the
window using the X button at the top right corner of the screen,
then you would not know. You could trap the form's closing event
and call E.Cancel = true and Me.Hide, so you can safely call the
form again with the same instance and solve this problem. Then
the user does not have to remember to click a button on the form
when closing the window. I hope that this leads you to your solution!


Joseph MCP
 
Hi,


You can add a DefInstance property to the form to make the form have
only one instance. Add this to form1

Public Shared frm As Form1

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

frm = Me

End Sub

Public Shared ReadOnly Property DefInstance() As Form1

Get

If frm Is Nothing Then frm = New Form1

Return frm

End Get

End Property



To use

form1.definstance.showdialog



Ken

-------------------------

Hi

I am opening a dialog using;

frmdialog = New frmdialognew
frmdialog.ShowDialog()

Is there a way to see if this dialog is already open to avoid opening
multiple instances of it?

Thanks

Regards
 

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