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:
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
- QTP & DOTNetFactory – Creating a Custom Form
- QTP & DOTNetFactory – Adding a Text Box Control {Currently viewing}
- QTP & DOTNetFactory – Adding a Button Control
- QTP & DOTNetFactory – Adding a CheckBox Control
- QTP & DOTNetFactory – Creating a Custom Form with Multiple Objects
- QTP & DOTNetFactory – Adding a RadioButton
- QTP & DOTNetFactory – Adding a ComboBox – Coming Soon!
{ 4 comments… read them below or add one }
How can I add (hard code) a text inside the text box?
Emad: You just have to use the .Text method to see this behavior…
Hi Anshoo,
Excellent work !!!!!
Thank you, Radhika!! :)
{ 5 trackbacks }