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:
In the next topics in this series, ComboBox and RadioButtons will be covered.
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
- QTP & DOTNetFactory – Creating a Custom Form with Multiple Objects {Currently viewing}
- QTP & DOTNetFactory – Adding a RadioButton
- QTP & DOTNetFactory – Adding a ComboBox – Coming Soon!
{ 3 comments… read them below or add one }
Hey Bro, thanks a lot for wonderful help regarding Dotnetfactory.. Please do create a page for adding drop down list boxes.Thanks. God Bless.
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
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..{ 5 trackbacks }