QTP DOTNetFactory (5): Creating a Custom Form with Multiple Objects

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

In this article, we will see how we can add and use multiple objects in a user form and the code below will pretty much sum it up.. It will cover the concepts discussed in the previous 4 articles. If you have any doubts on this topic or any topics in the earlier chapters, please ask :)

With DOTNetFactory
	'Form
	Set oForm = .CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms")
	'Button
	Set oButton = .CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
	'CheckBox
	Set oCheckBox = .CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
	'TextBox
	Set oTextBox = .CreateInstance("System.Windows.Forms.TextBox", "System.Windows.Forms")
 
	'Point
	Set oPoint = .CreateInstance("System.Drawing.Point", "System.Drawing", x, y)
End With
 
CONST txtWidth = 150
CONST pntStart = 20
 
'TextBox
With oPoint
	.x = pntStart
	.y = pntStart
End With
With oTextBox
	.Location = oPoint
	.Width = txtWidth
	.Name = "txtSample"
	.Text = "Enter a value"
End With
 
'CheckBox
With oPoint
	'Place the CheckBox to the right of the TextBox
	.x = pntStart + txtWidth + 20
	.y = 20
End With
With oCheckBox
	.Location = oPoint
	.Name = "chkSample"
	.Text = "CheckBox"
End With
 
'Button
With oPoint
	.x = pntStart
	.y = pntStart + 40
End With
With oButton
	.Location = oPoint
	.Width = CInt(oForm.Width) - (3*pntStart)
	.Text = "Close Form"
End With
 
'Form
With oForm
	'Set Form Height
	.Height = 200
	.Controls.Add oTextBox
	.Controls.Add oCheckBox
	'Adding a Cancel Button
	.CancelButton = oButton
	.Controls.Add oButton
End with
 
'Show Dialog
oForm.ShowDialog
 
With oTextBox
	If LCase(.Text) = "enter a value" Then
		MsgBox "You did not enter a value in the Text Box Control"
	Else
		MsgBox "New Text Box value: " & .Text
	End If
End with
 
With oCheckBox
	Dim sStatus
	sStatus = "ON"
	If CBool(.Checked) = False Then sStatus = "OFF"
	MsgBox "CheckBox Status: " & sStatus
End with

Executing the code above will display a form control:

A DOTNetFactory Form

A DOTNetFactory Form

In the next topics in this series, ComboBox and RadioButtons will be covered.

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
  4. QTP & DOTNetFactory – Adding a CheckBox Control
  5. QTP & DOTNetFactory – Creating a Custom Form with Multiple Objects {Currently viewing}
  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?

Leave a Comment

{ 3 comments… read them below or add one }

Genesis January 28, 2012 at 2:28 am

Hey Bro, thanks a lot for wonderful help regarding Dotnetfactory.. Please do create a page for adding drop down list boxes.Thanks. God Bless.

Reply

Giel Raijmakers January 25, 2010 at 7:35 am

Hi there.
How do you do eventhandeling?
I know how to make a form (with a bit of help from your code), all the objects, but… a click event, how is this done?

cheers,
Giel

Reply

Anshoo Arora January 26, 2010 at 11:05 am

Hi Giel,

To be honest with you, I’m not sure.. I don’t know how we can create an event while a form is loaded. I say this because, when we execute oForm.ShowDialog, the execution marker stops until we perform an action. I will try to research this a bit, but since VBScript is single-threaded, I’m not sure how this will be possible..

Reply

{ 5 trackbacks }

Previous post:

Next post: