Tuesday, August 4, 2015

Check if form is running



Verify if Form is opened

Hello! Welcome to more one tutorial. This time I will show to you how to determine if a specific Form is running. It´s easy J

How to create a project in Visual Studio?

Exists many ways to create a new project in VS. For example, when you open your VS program, in the Start Page you will see the option New Project ... 
If you can´t see the Start Page you can also click in File > New Project > Select Visual Basic > Windows Forms Application
Rename your project name if you want and click OK.
It´s easy! 

Start in the Form Design

For this tutorial we will need a two buttons. One is to verify if the specific form we want is opened or not. The other is to show that form (this second button is just to show you that this works fine). You can add the form manually by right-clicking the Project Name > Add > Windows Forms. In this case I will generate a form at running time.

The code …

Click, View > Code. This will open the .vb file of the form.
Between this lines:
Public Class Form1
End Class
Write this (this is to generate a form that will need it later)
Dim frm As New Windows.Forms.Form

Now, Click in combobox that is in the top, left side. Select FormX(events) and in the Combobox in the top right side select Load. This will generate the Load Event of the Form
Write the next lines of code:

Private Sub Form1_Load(sender As Object, e As EventArgsHandles Me.Load
        Button1.Text = "Check if forms is running”
End Sub

Next, do the same to the button. Select in the left Button2 and in the right side Click or more simple do double-click in the button in the designer.
Write this: (This is the button that will open the formHere I am customizing the form and then I will show it when click in Button2)

Private Sub Button2_Click(sender As Object, e As EventArgsHandles Button2.Click
        
        frm.Size = New Size(200, 200)
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.BackColor = Color.FromArgb(30, 30, 30)
        frm.Name = "Form2"
        frm.Text = "Form2"
        frm.Show()    
End Sub

In an empty space (between the lines:
Public Class Form1
End Class
), paste the next code. This is a Public Function that we will use to check if form is opened or not
    Public Function IsFormOpen(ByVal frm As FormAs Boolean
        If Application.OpenForms.OfType(Of Form).Contains(frmThen
            Return True
        Else
            Return False
        End If
    End Function
Repeat the process used to generate the Click event to the Button2 but now for the Button1. (The button1 is the button I´m using to check if forms is running). Paste this code:
        If IsFormOpen(frmThen 'frm is name of the second form
            MsgBox("The form is running"vbOKOnly + vbInformation)
        Else
            MsgBox("The form is not running"vbOKOnly + vbInformation)
        End If

Now is time to test. Debug you project. Click the Button2 to open the frm. Then click Button1 to see if is running or not. You will get this message “The form is running”
Now close the second form (frm, the black one) and press the button1 again and what you get? Exactly, “The form is not running”
Thanks for read!

No comments:

Post a Comment