• 0

[vb.net] Form exists?


Question

hi, i need to know from other form if my form is visible my form has a timer that hides the form after certain time and i want to know if a form is already visible, how can i do this?

it is something like msn alerts when someone signs in, i don't want all forms showing in the same place.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

does the application have a forms collection? If so, you should be able to look into the collection and see if the one you are talking about is there.

Edit: hmm...nope my c# didn't have that.

What you could do though is keep your own collection of forms that are loaded. Then you pass that collection around to each form, and you will be able to tell that way.

Edited by chorpeac
Link to comment
Share on other sites

  • 0

Nope, although a forms collection would be very handy, there is none. I made this example to show how you can handle dynamically created forms with a collection. If you can't figure it out, post here and I'll repost the code with some comments.

Module MultipleDynamicForms
 ? ?Dim AppForms As New Collection

 ? ?Public Sub Main()
 ? ? ? ?Dim x As Integer
 ? ? ? ?For x = 1 To 5
 ? ? ? ? ? ?AppForms.Add(New TemplateForm(x.ToString))
 ? ? ? ? ? ?AppForms(x).Show()
 ? ? ? ?Next
 ? ? ? ?While AppForms.Count > 0
 ? ? ? ? ? ?Application.DoEvents()
 ? ? ? ?End While
 ? ? ? ?Application.Exit()
 ? ?End Sub

 ? ?Public Class TemplateForm
 ? ? ? ?Inherits System.Windows.Forms.Form

 ? ? ? ?Public MyKey As String

 ? ? ? ?Public Sub New(ByVal Key As String)
 ? ? ? ? ? ?MyKey = Key
 ? ? ? ? ? ?Me.CloseButton = New System.Windows.Forms.Button
 ? ? ? ? ? ?Me.SuspendLayout()
 ? ? ? ? ? ?Me.CloseButton.Location = New System.Drawing.Point(32, 32)
 ? ? ? ? ? ?Me.CloseButton.Name = "CloseButton"
 ? ? ? ? ? ?Me.CloseButton.Size = New System.Drawing.Size(160, 40)
 ? ? ? ? ? ?Me.CloseButton.TabIndex = 0
 ? ? ? ? ? ?Me.CloseButton.Text = "Close"
 ? ? ? ? ? ?Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
 ? ? ? ? ? ?Me.ClientSize = New System.Drawing.Size(224, 108)
 ? ? ? ? ? ?Me.Controls.Add(Me.CloseButton)
 ? ? ? ? ? ?Me.Name = "TemplateForm"
 ? ? ? ? ? ?Me.Text = "TemplateForm"
 ? ? ? ? ? ?Me.ResumeLayout(False)
 ? ? ? ?End Sub

 ? ? ? ?Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
 ? ? ? ? ? ?If disposing Then
 ? ? ? ? ? ? ? ?If Not (components Is Nothing) Then
 ? ? ? ? ? ? ? ? ? ?components.Dispose()
 ? ? ? ? ? ? ? ?End If
 ? ? ? ? ? ?End If
 ? ? ? ? ? ?MyBase.Dispose(disposing)
 ? ? ? ?End Sub

 ? ? ? ?Private components As System.ComponentModel.IContainer

 ? ? ? ?Friend WithEvents CloseButton As System.Windows.Forms.Button

 ? ? ? ?Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
 ? ? ? ? ? ?Me.Close()
 ? ? ? ?End Sub

 ? ? ? ?Private Sub TemplateForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
 ? ? ? ? ? ?AppForms.Remove(MyKey)
 ? ? ? ?End Sub
 ? ?End Class
End Module

Link to comment
Share on other sites

  • 0
Nope, although a forms collection would be very handy, there is none. I made this example to show how you can handle dynamically created forms with a collection. If you can't figure it out, post here and I'll repost the code with some comments.

Module MultipleDynamicForms
 ? ?Dim AppForms As New Collection

 ? ?Public Sub Main()
 ? ? ? ?Dim x As Integer
 ? ? ? ?For x = 1 To 5
 ? ? ? ? ? ?AppForms.Add(New TemplateForm(x.ToString))
 ? ? ? ? ? ?AppForms(x).Show()
 ? ? ? ?Next
 ? ? ? ?While AppForms.Count > 0
 ? ? ? ? ? ?Application.DoEvents()
 ? ? ? ?End While
 ? ? ? ?Application.Exit()
 ? ?End Sub

 ? ?Public Class TemplateForm
 ? ? ? ?Inherits System.Windows.Forms.Form

 ? ? ? ?Public MyKey As String

 ? ? ? ?Public Sub New(ByVal Key As String)
 ? ? ? ? ? ?MyKey = Key
 ? ? ? ? ? ?Me.CloseButton = New System.Windows.Forms.Button
 ? ? ? ? ? ?Me.SuspendLayout()
 ? ? ? ? ? ?Me.CloseButton.Location = New System.Drawing.Point(32, 32)
 ? ? ? ? ? ?Me.CloseButton.Name = "CloseButton"
 ? ? ? ? ? ?Me.CloseButton.Size = New System.Drawing.Size(160, 40)
 ? ? ? ? ? ?Me.CloseButton.TabIndex = 0
 ? ? ? ? ? ?Me.CloseButton.Text = "Close"
 ? ? ? ? ? ?Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
 ? ? ? ? ? ?Me.ClientSize = New System.Drawing.Size(224, 108)
 ? ? ? ? ? ?Me.Controls.Add(Me.CloseButton)
 ? ? ? ? ? ?Me.Name = "TemplateForm"
 ? ? ? ? ? ?Me.Text = "TemplateForm"
 ? ? ? ? ? ?Me.ResumeLayout(False)
 ? ? ? ?End Sub

 ? ? ? ?Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
 ? ? ? ? ? ?If disposing Then
 ? ? ? ? ? ? ? ?If Not (components Is Nothing) Then
 ? ? ? ? ? ? ? ? ? ?components.Dispose()
 ? ? ? ? ? ? ? ?End If
 ? ? ? ? ? ?End If
 ? ? ? ? ? ?MyBase.Dispose(disposing)
 ? ? ? ?End Sub

 ? ? ? ?Private components As System.ComponentModel.IContainer

 ? ? ? ?Friend WithEvents CloseButton As System.Windows.Forms.Button

 ? ? ? ?Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
 ? ? ? ? ? ?Me.Close()
 ? ? ? ?End Sub

 ? ? ? ?Private Sub TemplateForm_Closing(ByVal sender As Object, ByVal e As System:)omponentModel.CancelEventArgs) Handles MyBase.Closing
 ? ? ? ? ? ?AppForms.Remove(MyKey)
 ? ? ? ?End Sub
 ? ?End Class
End Module

thanks... your code makes sense to me :) i'll try it when i get home.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.