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!

Dani AI

Generated

Building on what and said: use the OpenForms collection, but count by form type instead of the Name property. Names can be changed in the designer or at runtime, which would give the wrong count. Types do not change.

Add this helper once (top of the .vb file add: Imports System.Linq):

Module FormHelpers
  Public Function OpenCount(Of T As Form)() As Integer
    Return Application.OpenForms.OfType(Of T)().Count()
  End Function

  'Allow at most N instances; otherwise just focus an existing one
  Public Sub ShowUpTo(Of T As {Form, New})(maxInstances As Integer)
    Dim open = Application.OpenForms.OfType(Of T)()
    If open.Count >= maxInstances Then
      Dim first = open.First()
      first.WindowState = FormWindowState.Normal
      first.Activate()
    Else
      Dim f As New T()
      f.Show()
    End If
  End Sub
End Module

Usage examples:

  • Count how many Form1 are open: Dim n = OpenCount(Of Form1)()
  • Show a Form2 but never more than 2 at once: ShowUpTo(Of Form2)(2)

Notes:

  • If your project does not use the VB Application Framework, use System.Windows.Forms.Application.OpenForms (as above). If it does, My.Application.OpenForms is equivalent.
  • Show() opens modeless forms, so you can have several at once. ShowDialog() is modal; you will not have multiple at the same time.
  • Re ’s question: in ’s LINQ, g is just a loop variable that represents each form in the OpenForms collection. The query filters those items before counting them.

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!

: 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.