Unable to declare and check whether MDI Form exist or not ?

I

ItsMe

Hi,

I've 50 MDI Forms in my project, so trying to create MDI Child Form from
this procedure.
But the problem is, unable to declare as "NewFormName". It gives me an
error. Is there any
other better way. And I would like to check whether the form already exist
or not. ? Or
else what's happening is, it opens many forms as the user clicks. Any ideas
?


Public Sub mCreateForm(ByVal ParentFormName As Form, _
ByVal NewFormName As Form)
Dim oMDIForm As New NewFormName

With oMDIForm
.lblHeader.Text = "Common Parameter - Color"
.MdiParent = ParentFormName
.Show()
.Top = 0
.Left = 0
End With
End Sub

Thanks and Best Regards
ItsMe
 
C

Cor

Hi ItsMe,
In the way you tell it, it looks like an endless loop in your mouse click
Event.
When there is no more memory to make all those forms you get an error
because there is no more memory I guess.

I hope this helps a little bit,

Cor
 
C

Cor

It could maybe give some information if you tell us what the error is.
A some error is very wide error.
 
C

Cor

Hi Fergus,
So late and so clever, did not look at the code just at the sentence: "on a
user click a lot of forms are created".

Good daynight
Cor
 
I

ItsMe

Nopes, its not endless loop, i'll be calling this procedue whenever i need
to load a form. So I'm writing this function in a module. I'll be passing
NewFormName, whenever i need to create any form.

Any Comments ?
 
F

Fergus Cooney

Hi Smee,

Now I'm confused, lol. You have a method called mCreateForm() which
doesn't seem to do any creating! Where does ChildForm come from ? Are you
intending to create the child Form in mCreateForm() ? Can you show the code
for the caller?

You said in your original query that you have 50 Mdi Forms. Is this 50
individual Mdi Form classes, or 50 instances from just a few classes?

The parent Form has an MdiChildren collection. You can go through this.
But it depends what you mean by 'already created'.

Can you tell me more about the Form Load button - when it gets pressed and
what it's supposed to do?

In fact, can you tell me <lots> more about what you're doing because it's
all a bit fuzzy in my mind. ;-)

Regards,
Fergus
 
F

Francois Soucy

I hope this will help


' Called from Ingredients menu
Private Sub MnuIngredients_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MnuIngredients.Click
Dim FormFound As Boolean = False
Dim oForm As Form

For Each oForm In Me.MdiChildren
If (oForm.Name = "Ingredients") Then 'Check if we already have
this form...
oForm.Activate() ' YES focus on it....
FormFound = True
Exit For 'It's not necessary to continu "for loop"
End If
Next

If Not FormFound Then ' Not found ... Let's create it
Dim frmIngredients As New Ingredients()
frmIngredients.MdiParent = Me
frmIngredients.Show()
End If
End Sub

Francois
 
F

Fergus Cooney

Hi Francois,

Nice routine. :)

Some teachers/programmers say that you should never exit from a routine
except from the end. I don't believe that one, ;-) so I'd have a slightly
shorter version:

' Called from Ingredients menu
Private Sub MnuIngredients_Click(....) Handles MnuIngredients.Click
Dim oForm As Form
'Exit if we already have this form...
For Each oForm In Me.MdiChildren
If oForm.Name = "Ingredients" Then
Return
End If
Next

' Not found ... Let's create it
Dim frmIngredients As New Ingredients()
frmIngredients.MdiParent = Me
frmIngredients.Show()
End Sub

Regards,
Fergus
 
I

ItsMe

Fergus,

Thank you very much for your valuable comments. But I modified my code and
corrected the mistakes.
But how can I check whether the form has already created or not? Now my code
looks like this :


Public Sub mCreateForm(ByVal ChildForm As Form, ByVal FormHeading As
String, _
ByVal ParentForm As Form)

If Not IsNothing(ChildForm) Then 'This line doesn't really work
need to check whether form is created or not
With ChildForm
.Text = Trim(FormHeading)
.MdiParent = ParentForm
.Show()
.Top = 0
.Left = 0
End With
End If
End Sub


Now what's happeing is, whenever a user clicks on the form load button.
It opens up a MDI Child Form. If the user clicks second time another form is
opening. I want to avoid this, how can i ? Any ideas ??

Regards
 
I

ItsMe

hi fergus,

sorry to make you confuse, lol.

this is a caller code (when button pressed to create MDI Child form):

Call mCreateForm(New frmAdmVendor, "Common Parameter - Vendor", Me)

So everytime, i'm creating an instance in the common class. And calling from
the main menu.

So now the problem is if i'm calling more than once, its creating two
different instances (forms). I want to check whether an instance(form)
already exist or not. If i'm able to check then i'll not create, i'll just
move the form to the front. So how do i do that ?

I hope you understand my problem.

Regards
ItsMe
 
F

Francois Soucy

Hi Fergus

I agree with you!! But many law are made to be broken!!! But I
keep your solution! I've about 15 differents form that I call by the same
manner. So if we did a little math addition, we found that I could save
about 45 lines!!! :)

Thank for your version 1.1 of "Do_not_Open_a_form_two_time!" :)

Have a nice day
Francois
 
F

Fergus Cooney

Hi Francois,

Happy to work with you :))

You can save even more lines by having a common routine.

Regards,
Fergus

<code status="untested">
Private Sub MnuIngredients_Click(....) Handles MnuIngredients.Click
CreateOrActivateForm ("Ingredients", GetType (Ingredients))
End Sub

Private Sub CreateOrActivateForm _
(sHeading As String, oFormType As Type)
Dim oForm As Form
'Exit if we already have this form...
For Each oForm In Me.MdiChildren
If oForm.Name = sHeading Then
oForm.Activate
Return
End If
Next

' Not found ... Let's create it.
oForm = New Activator.CreateInstance (oFormType)
oForm.MdiParent = Me
oForm.Show()
End Sub
</code>
 

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