How to prevent windows form from loading again?

C

Coderz

Before I am using MDI form to open a child form. But I decided not to
use MDI and now wondering how can I prevent a windows form from
loading or opening again.

Here's the code I used before:

Private Function IsOpen(ByVal nameForm As String) As Boolean
Dim childfrm As Form
Dim strNaam As String
Dim intPuntje As Integer

For Each childfrm In Me.MdiChildren
strNaam = childfrm.GetType.ToString
intPuntje = strNaam.LastIndexOf(".")
strNaam = Mid(strNaam, intPuntje + 2, Len(strNaam) -
intPuntje)
If LCase(strNaam) = LCase(nameForm) Then
childfrm.BringToFront()
Return True
End If
Next
Return False
End Function


http://www.sourcecodester.com - Download free source code
 
T

Teemu

Coderz said:
Before I am using MDI form to open a child form. But I decided not to
use MDI and now wondering how can I prevent a windows form from
loading or opening again.

You could try use something like this:

Private Sub OpenForm(ByVal Name As Form)

For Each OwnedForm As Form In Me.OwnedForms
If OwnedForm.Equals(Name) Then
Name.Show()
Name.Focus()
Exit Sub
End If
Next

Name.Owner = Me
Name.Show()

End Sub

Paste that code for example in Form1 (acting like MDI Parent) and then just
call:

OpenForm(Form2)

-Teemu
 
T

Teemu

Coderz said:
Before I am using MDI form to open a child form. But I decided not to
use MDI and now wondering how can I prevent a windows form from
loading or opening again.

And if you don't want multiple instances of the same form, you could try
this:

Private Sub OpenForm(ByVal Name As Form)

For Each OwnedForm As Form In Me.OwnedForms
If TypeName(OwnedForm) = TypeName(Name) Then
OwnedForm.Show()
OwnedForm.Focus()
Exit Sub
End If
Next

Name.Owner = Me
Name.Show()

End Sub

Now you can call:

OpenForm(New Form2)

And you will only get one Form2.

-Teemu
 
A

Armin Zingler

Coderz said:
Before I am using MDI form to open a child form. But I decided not to
use MDI and now wondering how can I prevent a windows form from
loading or opening again.

Here's the code I used before:

Private Function IsOpen(ByVal nameForm As String) As Boolean
Dim childfrm As Form
Dim strNaam As String
Dim intPuntje As Integer

For Each childfrm In Me.MdiChildren
strNaam = childfrm.GetType.ToString
intPuntje = strNaam.LastIndexOf(".")
strNaam = Mid(strNaam, intPuntje + 2, Len(strNaam) -
intPuntje)
If LCase(strNaam) = LCase(nameForm) Then
childfrm.BringToFront()
Return True
End If
Next
Return False
End Function


Please post to the relevant newsgroups only next time. Thx.

I use to store a reference on the object I create. With a Form, I set the
reference to Nothing as soon as the Form is closed. Next time I create the
Form only if the reference is Nothing.

And, I recommend identifying a Form by it's type, not by it's name. Names
can be misspelled and the compiler can not recognize it, whereas type names
are safer. So, if you need a general function for this purpose, I'd pass the
System.Type object and compare to that type instead of comparing names.
Results in something like "If IsOpen(gettype(MyFormType)) then". Better than
"If IsOpen("MyFomType") then"


Armin
 
C

Coderz

Please post to the relevant newsgroups only next time. Thx.

First of all thanks for the reply.

I have posted this message into 3 newsgroup:
microsoft.public.dotnet.languages.vb,microsoft.public.vsnet.general,microsoft.public.vstudio.general

Which of this is not relevant to my question?

Thank you
 
A

Armin Zingler

Coderz said:
First of all thanks for the reply.

I have posted this message into 3 newsgroup:
microsoft.public.dotnet.languages.vb,microsoft.public.vsnet.general,microsoft.public.vstudio.general

Which of this is not relevant to my question?

The vsnet and vstudio groups are about the Visual Studio environment itself.
They are not about programming tasks. (how to set a breakpoint? Why don't my
symbols get loaded? When will there be the next release? I don't like the
Search&Replace dialog! Something like that...)
In addition, if you don't have a problem with the VB language/syntax and if
you don't need a code example in VB, there are also the
m.p.dotnet.framework.* groups.


Armin
 

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