Hello guys!

I have so many form to manage. I want to count the instances of a particular form. For example, if I show my form1 two times then there must be a counter for my form1 and if I also shown my form2 another counter for form2.
I want to do this to minimize the instances of forms to be shown so that my resources will not burden to much.

I want my code to be generic so that I can easily call it for my other forms.
I really need sample codes or snippets. Any help is most appreciated.
Note: Sorry for my grammar guys!

Recommended Answers

All 11 Replies

create a module and add following sub into it...

Friend Sub MinimizeForms(ByVal frm As String)
		Dim var = From g In My.Application.OpenForms Where CType(g, Form).Name = frm
		For Each f As Form In var
			f.WindowState = FormWindowState.Minimized
		Next
	End Sub

call this sub from where ever you want:

MinimizeForms("Form1")

Thank you for your response GeekByChoice!
Does it count the instances of each form?
I know it silly but I'm just a beginner and I really don't know what's the meaning of this code:

Dim var = From g In My.Application.OpenForms Where CType(g, Form).Name = frm

can you explain it to me?

Correction.
If you want to know how many open forms you have:

Dim count As Integer = My.Application.OpenForms.Count

Every time you open a form, it will be added to the OpenForms collection.
And every time you close a form with the .Close() and/or .Dispose() methods, it will be removed from the OpenForms collection.
So, OpenForms will keep a running track of what forms are opened and how many.

commented: i am sorry but he dont want the number of ALL open forms. he want the count of forms with a given name. +0

the example i gave you would just minimize all open forms with the Name you provided.
but if u need just a counter then here it is...

if you want only the count of named forms then you change
Dim var = From g In My.Application.OpenForms Where CType(g, Form).Name = frm
to
Dim var = (From g In My.Application.OpenForms Where CType(g, Form).Name = frm).Count

Thanks Oxiegen!
Where should I put this code?

Dim count As Integer = My.Application.OpenForms.Count

Please explain further.

ow leahrose87, did you change your mind somehow?
Didnt you ask for the count of open forms by a particular name?

Dim count As Integer = My.Application.OpenForms.Count << will show you ALL open forms, no matter if its Form1, Form2 or Form3

If indeed it is what you wanted then please make you questions more clear!

@GeekByChoice: what is "g" stands for? in your code:

Dim var = (From g In My.Application.OpenForms Where CType(g, Form).Name = frm).Count

Thanks!

No I didn't change my mind. I just don't understand what your trying to say because I am just a beginner and I never used those codes before.
And it is clear to me now!
Thanks for your help GeekByChoice!

My apologies. It was I that didn't read the entire question.

using this code:

Dim count As Integer = My.Application.OpenForms.Count

how will I know that a particular form is open two times?

You don't.
I didn't read your question fully and thus misunderstood.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.