This is Part2 of QTP and DotNetFactory series by Saket. In case you have missed the first part you can refer it via this link Part1: QTP and DotNetFactory – Basics

QTP DotNetFactory Part-2

In Part1 of this series we discussed the basics of DotNetFactory, why DOTNetFactory and how to use it with a simple example of creating your own custom message box. In this part you will learn to create a custom user form using DotNetFactory utility object in QTP.

Before we proceed, give a thought why you will need to have a user form in your script? What can we do by creating a user form?

Consider a case where you need to run your part of your script based on an input data from the user. In such case, it will be better if you can create an interface to interact with your script and so you will need a user form.

I have seen many such scenarios on various forums where users wanted to have an option(checkbox, radiobuttons etc) to proceed, like the recent query on QTP forums

To create a custom user form again we will use the same Set Statement to retrieve the COM Interface using CreateInstance, but this time with a different type name. We will use System.Windows.Form as type name and the same assembly (System.Windows.Forms) as Form is also a part of same namespace.

Set MyForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")

Now, we have already created the custom user form, the only thing remaining is to display the form. A form has two methods to display Show and ShowDialog. The only difference between the two is when you use ShowDialog then QTP waits until you perform an event on your user form.

We will use ‘Show’ Method first in the example. Show method for a Form does not accept arguments like Message box.

MyForm.Show

That’s it, and your user form will be displayed. Below is the complete code

Example 3

Set MyForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")
MyForm.Show

In the same way, you can display using ‘ShowDialog’

Example 4

Set MyForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")
MyForm.ShowDialog

This gives you output as

User Form

From the above example, you are now able to display a user form. But isn’t it seems like it is incomplete and we missed something. Yes, a user form must have a title (caption) and some controls on it to interact. So now we will add a title to the form. We will learn to create the different controls on it on later parts in this series.

To add a title to the user form, we have Text property of Form to use, which will enable to add a title of the custom user form using

MyForm.Text = “My Custom User Form”

Below is the complete code

Example 5

Set MyForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")
MyForm.Text = "My Custom User Form"
MyForm.Show

This will give you output as

User Form with Title

Similarly you can use other properties of form as well. For example you can use

Maximizebox – used to hide the miximize button at top right of the form
Minimizebox – used to hide the minimize button at top right of the form
Height – to set the height of form
Width – to set the width of form
Location – to set the location to display on screen, etc

Below is the code that uses properties mentioned above:

Example 6

Set MyForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")
MyForm.Text = "My Custom User Form"
MyForm.Maximizebox = False
MyForm.Minimizebox = False
MyForm.Width = 300
MyForm.Height =150
MyForm.Location.X = 100
MyForm.Location.Y = 200
MyForm.Show

This above code will give you the output as

Customizing User Form

Note: When working with such scenarios (mentioned above), always use the ShowDialog method to display the form,  as we need to have some input from the user to proceed further with our script.

In the next part of series we will add some controls to the form created above.