QTP DOTNetFactory (2): Adding a Text Box Control

by Anshoo Arora ON August 14, 2009 · Posted In All, DOTNetFactory, QTP · 9 comments

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!

Subscribe to Relevant Codes (by Anshoo Arora)

Hello! We're always posting interesting articles on Relevant Codes. Why not subscribe so you don't miss out?

Previous post:

Next post: