This is the 6th article in the DOTNetFactory Series. The first 5 articles cover the basics of creating a user form and adding a control to it. Like the previous article, this will also be a little fast-paced and will cover how to create a Custom form and add a RadioButton control to it. If you have any doubts with the content in this article, or the previous, please post your comments and I will try to answer them as soon as possible.
Similar to creating a Form, a Text Box and a Button Control, we will use .CreateInstance to create an instance of RadioButton Control as well.
Set oRadioButton = DOTNetFactory.CreateInstance("System.Windows.Forms.RadioButton", "System.Windows.Forms")
Similar to the objects we’ve worked in the preceeding lessons, we will again have to define a point where the RadioButton will be placed.
Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y) With oPoint .x = 20 .y = 20 End With oRadioButton.Location = oPoint
Also, we must add some Text to the RadioButton so its easily distinguishable. It can be done by using the .Text method:
oRadioButton.Text = "RadioButton"
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 oRadioButton = DOTNetFactory.CreateInstance("System.Windows.Forms.RadioButton", "System.Windows.Forms") Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y) 'RadioButton Location With oPoint .x = 20 .y = 20 End With 'RadioButton Properties With oRadioButton .Text = "RadioButton" 'Give the RadioButton a name property .Name = "btnSample" .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 oRadioButton 'Adding a Cancel Button .CancelButton = oButton .Controls.Add oButton End with 'Show Dialog oForm.ShowDialog 'Output the entered Text: MsgBox "RadioButton Selected: " & oRadioButton.Checked
Executing the code above will display a form control like below:
I had the RadioButton selected and after clicking the WebButton, I received the following output from QTP:
In the next topic in this series, we will learn how to create a ComboBox control using DOTNetFactory.
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
- QTP & DOTNetFactory – Adding a RadioButton {Currently Viewed}
- QTP & DOTNetFactory – Adding a ComboBox – Coming Soon!
If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.

{ 5 comments… read them below or add one }
Hi Anshoo,
I want to see one qtp script where DotNetFactory example in inculded in it. Is it possible to show that flow.
Thanks
raj
Hi Rajshree,
You can download the RelevantCodes[1]One framework that has an entire form with such custom controls right at the initiation of the test. When you download the framework, you will find the entire DOTNetFactory code in
Engine.vbsunderPrivate Function IsFormConform().Hi Anshoo,
Thanks for the quick reply, really appreciate it. Anshoo this too advance for me. I working on qtp from last one year, i dont have any framework knowledge. I use record and playback, able to write few functions, loop and all. Is it possble to add DotNetFactory in recorded script ? may be this is really stupid question :)
Thanks
Raj
Hi Rajshree,
You can surely add DOTNetFactory code in an already recorded script using the Expert View to do so. It depends though how well it will help you. What kind of functionality do you want to create using DOTNetFactory?
Hi Anshoo,
In the above sections you have clearly explained about adding differnt controls using dotnetfactory.Going a step ahead I would like to include certain conditions based on the controls present in the form
Say In the form created using dotnet factory,based on radiobutton selection ,textboxes should be enabled or disabled
Again based upon listbox selection within the form ,corresponding value should be displayed in textbox.Please can you throw some light on this.
{ 4 trackbacks }