Relevant Codes (by Anshoo Arora)

A Test Development Resource for HP QuickTest Professional.

QTP DOTNetFactory (3): Adding a Button Control

by Anshoo Arora on August 14, 2009

The last 2 articles in this series discussed creation of a Custom Form and addition of a Text Box Control to the form. This article shows how to add a Button control to the form using DOTNetFactory. Since most of the basics have been discussed in the previous articles, this one will be quite fast-paced.

Similar to creating a Form and Text Box, we will use .CreateInstance to create a Button Control.

Set oButton = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")

Also, just like a Text Box, the button will have to be positioned somewhere in the form, thus, we will again define a Point object:

Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
 
With oPoint
	.x = 20
	.y = 50
End With
 
oButton.Location = oPoint

When adding a Button, you might also want to add some Text to it, so its easily distinguishable:

oButton.Text = "Close Form"

In this example, the button we are adding will be a Cancel button type that will exit the form once clicked.

oForm.CancelButton = oButton

Putting it all together

Let’s add a Text Box Control, and use QTP to output the entered Text using MsgBox.

Set oForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
Set oButton = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
Set oText = DotNetFactory.CreateInstance("System.Windows.Forms.TextBox", "System.Windows.Forms")
 
Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
 
'Text Box
With oPoint
	.x = 20
	.y = 20
End With
 
With oText
	.Location = oPoint
	.Width = 100
End With
 
'Button
With oPoint
	.x = 20
	.y = 50
End With
 
With oButton
	.Location = oPoint
	.Width = 100
	.Text = "Close Form"
End with
 
'Add Controls
With oForm
	.Controls.Add oText
	'Adding a Cancel Button
	.CancelButton = oButton
	.Controls.Add oButton
End with
 
'Show Dialog
oForm.ShowDialog
 
'Output the entered Text:
MsgBox oText.Text

Executing the code above will display a form control:

Adding a Button Control to a Custom Form

Adding a Button Control to a Custom Form

I entered Relevant Codes in the text box and clicked the button to get the following output from QTP:

Button Control Example: QTP Output

Button Control Example: QTP Output

In the next topic in this series, we will learn how to create a CheckBox control using DOTNetFactory.

I hope you find this article useful.

In this Series

  1. QTP & DOTNetFactory – Creating a Custom Form
  2. QTP & DOTNetFactory – Adding a Text Box Control
  3. QTP & DOTNetFactory – Adding a Button Control {Currently viewing}
  4. QTP & DOTNetFactory – Adding a CheckBox Control
  5. QTP & DOTNetFactory – Creating a Custom Form with Multiple Objects
  6. QTP & DOTNetFactory – Adding a RadioButton
  7. QTP & DOTNetFactory – Adding a ComboBox – Coming Soon!

If you have any questions, please ask them in the comments section. If your information is to be treated as confidential, please use the Contact Form to send me an e-mail instead.

{ 4 comments… read them below or add one }

1 Picia November 4, 2009 at 4:38 am

Hello,
I think the articles are great. They helped me a lot with using custom controls in QTP.
I also have a question regarding buttons. Lets assume we have a button which is not assigned to form action(like in the example assignment is to oForm.cancelButton):

With oButton2
	.Location = oPoint
	.Width = 100
	.Text = "Open another dialog"
End with

'Add Controls
With oForm
	.Controls.Add oText
	'Adding a Cancel Button
	.CancelButton = oButton
	.Controls.Add oButton
End with

Is it possible to detect click on “Open another dialog” button? OR i need to assign it to another form action i.e.

oForm.Abort=oButton2

Reply

2 Anshoo Arora November 4, 2009 at 9:25 am

Hi Picia,

Thank you for your kind words. I’m happy that you found them useful.

To answer your question, yes, it is possible to create such a scenario. The only extra bit here is creating a “DialogResult” object to retrieve the result of the buttons that are added to the form. Please see below:

Set oForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
Set oButtonOK = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
Set oButtonCancel = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
Set DiagReturn = DotNetFactory.CreateInstance("System.Windows.Forms.DialogResult", "System.Windows.Forms")
Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point","System.Drawing", x, y)

With oPoint
	.x = 5
	.y = 5
End With

With oButtonOK
	.Text = "OK"
	.Location = oPoint
	.DialogResult = DiagReturn.Ok
End With

With oPoint
	.x = 5
	.y = 35
End With

With oButtonCancel
	.Text = "Cancel"
	.Location = oPoint
	.DialogResult = DiagReturn.Cancel
End With

With oForm
	.Height = 500
	.Controls.Add oButtonOK
	.Controls.Add oButtonCancel
	.ShowDialog
End With

If oForm.DialogResult = DiagReturn.Ok Then
	'Code to open another form
	MsgBox "Opening another form.."
End If

If oForm.DialogResult = DiagReturn.Cancel Then
	'Code to do something else
	MsgBox "Doing something else.."
End If

Set oForm = Nothing
Set oButton = Nothing
Set DiagReturn = Nothing
Set oPoint = Nothing

I hope this helps..

3 Bala August 18, 2010 at 6:05 am

Hi Anshoo

I have created a form using dotnetfactory and added a listbox object and textbox object.say on selectiing a particular value from the list box,i wud like to have the selected listbox value to be displayed in my textbox object

Reply

4 prasad June 3, 2011 at 7:41 am

Hi All,
Can anyone tell me, If a form consist more than 10 button with the different name. Here how to identify which button is clicked.

Thanks in advance

Reply

Leave a Comment

{ 4 trackbacks }

Previous post:

Next post: