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

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

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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
Nov 20 '05 #2

P: n/a
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 ?
"Cor" <no*@non.com> wrote in message
news:3f***********************@reader22.wxs.nl...
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

Nov 20 '05 #3

P: n/a
Cor
It could maybe give some information if you tell us what the error is.
A some error is very wide error.
Nov 20 '05 #4

P: n/a
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
Nov 20 '05 #5

P: n/a
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

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
Oh, Its You!!

|| Public Sub mCreateForm(ByVal ParentFormName As Form, _
|| ByVal NewFormName As Form)

^^ NewFormName is a parameter variable.

|| Dim oMDIForm As New NewFormName

^^ NewFormName is a Type

* This can't be the code you're using because it won't compile.

* This will create a Form with the same type as NewFormName:

Dim oMDIForm As Form
oMDIForm = DirectCast (Activator.CreateInstance (NewFormName.GetType),
Form)

* ParentFormName and NewFormName are bad names because the variables aren't names - they're Forms.
* Better is:

Public Sub mCreateForm (ByVal ParentForm As Form, _
ByVal FormType As Type) Dim oMDIForm As Form
oMDIForm = DirectCast (Activator.CreateInstance (FormType), Form)

I'm off to bed. Good night :-)

Regards,
Fergus
MVP [Windows Keyboard, PC Power Switch]

Nov 20 '05 #6

P: n/a
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
Nov 20 '05 #7

P: n/a
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

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:uq**************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #8

P: n/a
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

"ItsMe" <it*******@yahoo.com> wrote in message
news:uo**************@TK2MSFTNGP10.phx.gbl...
hi fergus,
....

Nov 20 '05 #9

P: n/a
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
Nov 20 '05 #10

P: n/a
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

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #11

P: n/a
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>
Nov 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.