QTP DOTNetFactory (6): Adding a RadioButton Control

by Anshoo Arora ON August 19, 2009 · Posted In All, DOTNetFactory, QTP · 11 comments

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:

DOTNetFactory: Adding a Radio Button Control

DOTNetFactory: Adding a Radio Button Control

I had the RadioButton selected and after clicking the WebButton, I received the following output from QTP:

QTP Output for a Selected RadioButton

QTP Output for a Selected RadioButton

In the next topic in this series, we will learn how to create a ComboBox control using DOTNetFactory.

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
  6. QTP & DOTNetFactory – Adding a RadioButton {Currently Viewed}
  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

{ 7 comments… read them below or add one }

ajay February 21, 2012 at 1:53 pm

Hi Anshoo,
Actually i donot understand where i am using this method or this concept, can u give real time example,which will help full for me.

Reply

Anshoo Arora March 15, 2012 at 7:17 am

Ajay, you can use this concept if you would like to present the user with a form. This will halt execution until the form is closed though.

Reply

Bala August 24, 2010 at 9:22 am

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.

Reply

Rajshree January 27, 2010 at 10:16 am

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

Reply

Anshoo Arora January 28, 2010 at 11:13 am

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?

Reply

rajshree January 26, 2010 at 4:02 pm

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

Reply

Anshoo Arora January 26, 2010 at 11:11 pm

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.vbs under Private Function IsFormConform().

Reply

{ 4 trackbacks }

Previous post:

Next post: