QTP10.0 and DotNetFactory – Creating a Button Control

Now that you have learned creating custom user form in the earlier parts of this series, we will now see how we can add various controls on it. These controls could be a text box, button, checkbox, radio button etc.
We will start with adding a button control first, let’s understand why we will need a button on the form. Normally we use a command button as a user interface element that provides a simple way to trigger an event or mostly to confirm an action. So if you are creating a user form then it must have some events to trigger with, like you are displaying some information or taking some input, which could be used further in your script.

In the previous example of creating a user form, we have seen that we have created a form without any controls on it. We will now create a button control. The action for this button control will be to close the User form.

Similar to creating a form as in the last part, we will use ‘System.Windows.Forms.Button’ as type name. Button belongs to the same namespace, so we will use ‘System.Windows.Forms’ as assembly and Set statement to create the interface. But first of all we will need a user form on which we can place our control.

Create the user form and button in the same way:

1. Set MyForm = DotNetFactory.CreateInstance(
2. "System.Windows.Forms.Form", "System.Windows.Forms")
3.   
4. Set MyButton = DotNetFactory.CreateInstance(
5. "System.Windows.Forms.Button", "System.Windows.Forms")

We have created user form and button control, now the button needs to be placed on the user form. Form has a method as ‘Controls. Add’, we will use this to add button control on to the form.

1. MyForm.Control.Add MyButton

Now as the button is added, we can display the user form using ‘Showdialog’

1. MyForm.ShowDialog

The code will be like

Example 7

1. Set MyForm = DotNetFactory.CreateInstance(
2. "System.Windows.Forms.Form", "System.Windows.Forms")
3. Set MyButton = DotNetFactory.CreateInstance(
4. "System.Windows.Forms.Button", "System.Windows.Forms")
5. MyForm.Controls.Add MyButton
6. MyForm.ShowDialog

This will gives you output as

Blank Image with Button

It was so simple, but it is not finished yet. We need to have some text on button to identify it and its purpose. Also it must have to be placed at some proper location other wise whatever controls we add further will go the same location and our form will be of no use. Also it will give a better look to the user form.

Button also has some properties which we can use, like Height, Width, Text, Location, Enabled etc. Here we will use ‘Text’ property to set the caption of button.

1. MyButton.Text = "Close"

To set the location we will use ‘Location’ Property. But here we will need to understand how we can set the location as it will require co-ordinates (X and Y) to exactly point the location on screen. To set position we will need to have another object of position, which will set directly to the button property. For this we will use ‘System.Drawing’ Namespace. Let’s create a Position first with Set statement again using ‘System.Drawing.Point’ as type name and assembly would be ‘System.Drawing’.

1. Set Pos = DotNetFactory.CreateInstance(
2. "System.Drawing.Point","System.Drawing",x,y)

You can see here we have passed arguments this time as x and y because System.Drawing.Point type requires integer value for x- and y- coordinates that defines a point in two dimensional planes. So we need to pass these values and set it to the property ‘Location’.

1. Pos.X = 100
2. Pos.Y = 100
3. MyButton.Location = Pos

That’s it, we have now done with the text and location of button. But when you run this you will see that this is not yet functional. Forms has two properties ‘AcceptButton’ and ‘CancelButton’ to allow users to click a button by pressing the ‘Enter’ or ‘ESC’ keys. ‘CancelButton’ property closes the form when the button is clicked or ‘ESC’ key pressed. If you use ‘CancelButton’ property then the button will be clicked and your form will be closed but if you use ‘AcceptButton’ property then your form does not close.

As we have kept this button to close the form, we will now make this button of type ‘Cancel’ which will enable this button to close the form.We will use ‘CancelButton’ property of Form to do this

1. MyForm.CancelButton = MyButton

Below is the complete code

Example 8

01. Set MyForm = DotNetFactory.CreateInstance(
02. "System.Windows.Forms.Form", "System.Windows.Forms")
03. Set MyButton = DotNetFactory.CreateInstance(
04. "System.Windows.Forms.Button", "System.Windows.Forms")
05. Set Pos = DotNetFactory.CreateInstance(
06. "System.Drawing.Point","System.Drawing",x,y)
07.   
08. With Pos
09. .X = 100
10. .Y = 100
11. End With
12.   
13. With MyButton
14. .Text = "Close"
15. .Location = Pos
16. End With
17.   
18. With MyForm
19. .Controls.Add MyButton
20. .CancelButton = MyButton
21. .Text = "My Custom User Form"
22. .ShowDialog
23. End With
24.   
25. msgbox "User Form Closed"
26.   
27. Set MyForm = Nothing
28. Set MyButton = Nothing
29. Set Pos = Nothing

When you will run this, it will give you output as

Close Button

Clicking the button will close the user form. And as we have a statement to output a message box after the ‘ShowDialog’ statement, it will pop up a message box once our user form is closed.

User Form Closed

Further to this we will now see what if we need more than one button on the user form. I will explain this in a simple way using ‘DialogResult’.

DialogResult represents the result of the form. If the form is displayed as a dialog box, setting this property with a dialog result value sets the value of the dialog box result for the form. We are using our custom form as a dialog box, so we can use this property to get the result from the from. We can set the DialogResult property of button, which is assigned to the DialogResult property of a form when clicked on the button. To retrieve the COM interface for this we can use ‘System.Windows.Forms.DialogResult’ from ‘System.Windows.Forms’ assembly.

1. Set dResult = DotNetFactory.CreateInstance(
2. "System.Windows.Forms.DialogResult", "System.Windows.Forms")

Let us create a form with two button control and clicking on each button will give us output accordingly.

Continuing the above example we will add one more button on our user form and place it beside the first button. I will rename the button object created above as MyButton1 and create another with MyButton2.

01. Set MyButton1 = DotNetFactory.CreateInstance(
02. "System.Windows.Forms.Button", "System.Windows.Forms")
03. Set MyButton2 = DotNetFactory.CreateInstance(
04. "System.Windows.Forms.Button", "System.Windows.Forms")
05.   
06. With Pos
07. .X = 50
08. .Y = 100
09. End With
10.   
11. With MyButton1
12. .Text = "Close"
13. .Location = Pos
14. .DialogResult = dResult.Cancel
15. End With
16.   
17. With Pos
18. .X = 150
19. .Y = 100
20. End With
21.   
22. With MyButton2
23. .Text = "OK"
24. .Location = Pos
25. .DialogResult = dResult.OK
26. End With

You can see we have assigned the result for dialog using DialogResult Property of button. For Button1 we have assigned Cancel and for Button2 we have assigned OK. Similarly we can use different enumeration as well like Yes, No, Ignore etc, By default it is None.

Now when a button is clicked on the form the form will be closed and dialog result will be assigned to Form. Which we can get using the DialogResult property of Form.

1. If MyForm.DialogResult = dResult.OK Then
2.    msgbox "OK Button Pressed"
3. else
4.    msgbox "Close Button Pressed"
5. End If

Putting it all together

Example 9

01. Set MyForm = DotNetFactory.CreateInstance(
02. "System.Windows.Forms.Form", "System.Windows.Forms")
03. Set MyButton1 = DotNetFactory.CreateInstance(
04. "System.Windows.Forms.Button", "System.Windows.Forms")
05. Set MyButton2 = DotNetFactory.CreateInstance(
06. "System.Windows.Forms.Button", "System.Windows.Forms")
07. Set dResult = DotNetFactory.CreateInstance(
08. "System.Windows.Forms.DialogResult", "System.Windows.Forms")
09. Set Pos = DotNetFactory.CreateInstance(
10. "System.Drawing.Point","System.Drawing",x,y)
11.   
12. With Pos
13. .X = 50
14. .Y = 100
15. End With
16.   
17. With MyButton1
18. .Text = "Close"
19. .Location = Pos
20. .DialogResult = dResult.Cancel
21. End With
22.   
23. With Pos
24. .X = 150
25. .Y = 100
26. End With
27.   
28. With MyButton2
29. .Text = "OK"
30. .Location = Pos
31. .DialogResult = dResult.OK
32. End With
33.   
34. With MyForm
35. .Controls.Add MyButton1
36. .Controls.Add MyButton2
37. .Text = "My Custom User Form"
38. .ShowDialog
39. End With
40.   
41. If MyForm.DialogResult = dResult.OK Then
42.   msgbox "OK Button Pressed"
43. else
44.   msgbox "Close Button Pressed"
45. End If
46.   
47. Set MyForm = Nothing
48. Set MyButton1 = Nothing
49. Set MyButton2 = Nothing
50. Set dResult = Nothing
51. Set Pos = Nothing

This gives the output as

Custom User Form

Clicking on Close button gives output as

Close Button Pressed

Clicking on OK button gives output as

OK Button Pressed

Keep subscribed to LearnQTP.com, there are more interesting articles to come in this series!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值