This is the 4th article in the DOTNetFactory Series. The first 3 articles cover the basics of creating a user form and adding a control to it. This article will be a little more fast-paced and will cover how to create a Custom form and add a CheckBox control to it.
Similar to creating a Form, a Text Box and a Button Control, we will use .CreateInstance to create a CheckBox Control as well.
Set oCheckBox = DOTNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms")
Also, just like a Text Box and a Button, the CheckBox 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 = 20 End With oCheckBox.Location = oPoint
Also, we must add some Text to the CheckBox so its easy to identify if we should check or, or keep it unchecked. It can be done by using the .Text method:
oCheckBox.Text = "CheckBox"
Putting it all together
Set oForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms") Set oButton = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms") Set oCheckBox = DOTNetFactory.CreateInstance("System.Windows.Forms.CheckBox", "System.Windows.Forms") Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y) 'CheckBox Location With oPoint .x = 20 .y = 20 End With 'CheckBox Properties With oCheckBox .Text = "CheckBox" 'Give the checkbox a name property .Name = "chkSample" .Location = oPoint End With 'Button Location With oPoint .x = 20 .y = 50 End With 'Button Properties With oButton .Location = oPoint .Width = 100 .Text = "Close Form" End with 'Add Controls With oForm .Controls.Add oCheckBox 'Adding a Cancel Button .CancelButton = oButton .Controls.Add oButton End with 'Show Dialog oForm.ShowDialog 'Output the entered Text: MsgBox "CheckBox Selected: " & oCheckBox.Checked
Executing the code above will display a form control:
I had the checkbox in the “ON” position and after clicking the button, I received the following output from QTP:
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
- QTP & DOTNetFactory – Creating a Custom Form
- QTP & DOTNetFactory – Adding a Text Box Control
- QTP & DOTNetFactory – Adding a Button Control
- QTP & DOTNetFactory – Adding a CheckBox Control {Currently viewing}
- QTP & DOTNetFactory – Creating a Custom Form with Multiple Objects
- QTP & DOTNetFactory – Adding a RadioButton
- 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 trackbacks }