Thursday, May 26, 2016

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 syJ
How to crte a project in Visual Studio?
Exists many ways to crte a new project in VS. For example, when you open your VS program, in theStart Pageyou will see the optionNewProject ...If you can´t see the Start Page you can also click inFile > New Project > Select Visual Basic > Forms AppliionRename your project name if you want and clickOK.It´s sy!
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 theProject Name > Add > Forms. In this case I will erate a form at running time.
The …
Click,View > .This will open the .vbfile of the form.Betweenthis lines:PublicClassForm1EndClassWrite this (this is to erate a form that will need it later)DimfrmAsNew.Forms.Form
Now, Click incomboboxthat is in the top, left side. SelectFormX(events)and in theComboboxin the top right side selectLoad. This will erate theLoad Event of the FormWrite the next lines of :
PrivateSubForm1_Load(senderAsObject, eAsEventArgs)HandlesMe.LoadButton1.Text ="Check if forms is running”EndSub
Next, do the same to the button. Select in the leftButton2and in the right sideClickor more simple do double-click in the button in the designer.Write this: (This is the button that will open the form.Here I am customizing the form and then I will show it when click inButton2)
PrivateSubButton2_Click(senderAsObject, eAsEventArgs)HandlesButton2.Clickfrm.Size=NewSize(200, 200)frm.StartPosition=FormStartPosition.CenterScreenfrm.BackColor=Color.FromArgb(30, 30, 30)frm.Name="Form2"frm.Text="Form2"frm.Show()EndSub
In an empty space (between the lines:PublicClassForm1EndClass), paste the next . This is aPublic Functionthat we will use to check if form is opened or notPublicFunctionIsFormOpen(ByValfrmAsForm)AsBoolnIfAppliion.OpenForms.OfType(OfForm).Contains(frm)ThenReturnTrueElseReturnFalseEndIfEndFunctionRept the process used to erate theClickevent to theButton2but now for theButton1. (The button1 is the button I´m using to check if forms is running). Paste this :IfIsFormOpen(frm)Then'frmis name of the second formMsgBox("The form is running",vbOKOnly+vbInformation)ElseMsgBox("The form is not running",vbOKOnly+vbInformation)EndIf
Now is time to test. Debug you project. Click the Button2 to open thefrm. 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 rd!

No comments:

Post a Comment