Relevant Codes (by Anshoo Arora)

A Test Development Resource for HP QuickTest Professional.

QTP DOTNetFactory (2): Adding a Text Box Control

by Anshoo Arora on August 14, 2009

In this article, we will discuss how to add the Text Box control to the user form we created in the previous article, Creating a Custom Form. CreateInstance has also been discussed in the previous article, and just like a Form, we will use .CreateInstance again for the Text Box control.

Set oText = DotNetFactory.CreateInstance("System.Windows.Forms.TextBox", "System.Windows.Forms")

Above, the TypeName is System.Windows.Forms.TextBox. This means, just like the Form Control, a TextBox is also a part of the System.Windows.Forms namespace. Unlike the Form Control however, we cannot use .Show and .ShowDialog methods. Instead, we will insert the Text Box in the Form Control.

Set oForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
Set oText = DotNetFactory.CreateInstance("System.Windows.Forms.TextBox", "System.Windows.Forms")
oForm.ShowDialog

After executing the statements above, you will notice that the text box didn’t appear in the Form? Well, that is because even though we created an edit box object, we have not yet added it to the Form (oForm) Control. We have not even defined the size and where it will be located on the form. Before we add the oText objec to the form, we must first understand the System.Drawing namespace.

Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)

Now, oPoint will enable us to place individual controls to be added to the form on given x and y coordinates. Below is a demonstration of how to place the Text Box control 20 points from the left, and 20 points to the top of the form:

Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
 
With oPoint
	.x = 20
	.y = 20
End With
 
'Our Text Control will now have the coordinates added above
oText.Location = oPoint

and executing the following statement will add the Text Box control to the form:

oForm.Controls.Add oText

When we add the Text Box control to the form, it will be placed on the stated x and y coordinates. Finally, when we put all the pieces together, we will have the following code and form:

Set oForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
Set oText = DotNetFactory.CreateInstance("System.Windows.Forms.TextBox", "System.Windows.Forms")
 
'System.Drawing.Point Instance
Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
 
'Text Box Location 
With oPoint
	.x = 20
	.y = 20
End With
 
'Text Box Properties
With oText
	.Location = oPoint
	.Width = 100
End With
 
oForm.Controls.Add oText
 
oForm.ShowDialog

Form:

Text Box Control added to a Custom Form

Text Box Control added to a Custom Form

In the next topic in this series, we will learn how to create and add a Button 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 {Currently viewing}
  3. QTP & DOTNetFactory – Adding a Button Control
  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 Radhika November 6, 2009 at 8:04 am

Hi Anshoo,

Excellent work !!!!!

Reply

2 Anshoo Arora November 6, 2009 at 9:31 am

Thank you, Radhika!! :)

3 Emad October 5, 2011 at 6:48 pm

How can I add (hard code) a text inside the text box?

Reply

4 Anshoo Arora October 11, 2011 at 4:47 pm

Emad: You just have to use the .Text method to see this behavior…

oTextBox.Text = "SomeText"

Leave a Comment

{ 5 trackbacks }

Previous post:

Next post: