This is Part4 of QTP and DotNetFactory series by Saket. Refer Part1, Part2,Part3

QTP DotNetFactory Part-4

We have already discussed how to add button control to the form, which we used to confirm/cancel the action or close the form. In this part we will learn how to add various input controls on the form. An Input control can be a text box – to input text data, radio button or check box – to select a option control etc. or a combination of these controls. In this part of series we will discuss on how to add these controls on the form.

Let us start with adding text box control. We can create a text box control using CreateInstance. We will use ‘System.Windows.Forms.TextBox’ type name and ‘System.Wndows.Forms’ assembly.

  1. Set MyText = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.TextBox", "System.Windows.Forms")
  3. The textbox control is created. As discussed in the previous part we will need to place this control on the form. So we will need the user form first then we can add this control using Control. Add method.
  4. Set MyForm = DotNetFactory.CreateInstance("
  5. System.Windows.Forms.Form", "System.Windows.Forms")
  6. Set MyText = DotNetFactory.CreateInstance("
  7. System.Windows.Forms.TextBox", "System.Windows.Forms")
  8. MyForm.Control.Add MyText

And we can display the form using ‘ShowDialog’. The code will be like

Example 10

  1. Set MyForm = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.Form", "System.Windows.Forms")
  3. Set MyText = DotNetFactory.CreateInstance("
  4. System.Windows.Forms.TextBox", "System.Windows.Forms")
  5. MyForm.Controls.Add MyText

MyForm.ShowDialog

This gives you output as

Show Dialog QTP

You can see we have added the text box control on the user form, but we have not defined its location on the form. In the same way as we did for the button we position the text box control on the form using System.Drawing.Point

  1. Set Pos = DotNetFactory.CreateInstance("
  2. System.Drawing.Point","System.Drawing",x,y)
  3.  
  4. Pos.X = 10
  5. Pos.Y = 20
  6. MyText.Location = Pos

We can use its different properties like height width, Text, Name etc.

The Text Property can be used to set the text control or to retrieve the text from the text box control. We will use the width property to set the size of text box control and

The code will be like

  1. Set MyForm = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.Form", "System.Windows.Forms")
  3. Set MyText = DotNetFactory.CreateInstance("
  4. System.Windows.Forms.TextBox", "System.Windows.Forms")
  5. Set Pos = DotNetFactory.CreateInstance("
  6. System.Drawing.Point","System.Drawing",x,y)
  7. Pos.X = 100
  8. Pos.Y = 100
  9. MyText.Location = Pos
  10. MyText.Width = 150
  11. MyForm.Control.Add MyText
  12. MyForm.ShowDialog

Now let us add the button control created in the previous part to the user form with text box control to confirm the action or to close the form.

  1. Set MyButton = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.Button", "System.Windows.Forms")

As mentioned earlier we will use ‘Text’ Property to retrieve the text entered into text box control.

MyText.Text

The complete code will be like

Example 11

  1. Set MyForm = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.Form", "System.Windows.Forms")
  3.  
  4. Set MyText = DotNetFactory.CreateInstance("
  5. System.Windows.Forms.TextBox", "System.Windows.Forms")
  6.  
  7. Set MyButton = DotNetFactory.CreateInstance("
  8. System.Windows.Forms.Button", "System.Windows.Forms")
  9.  
  10. Set Pos = DotNetFactory.CreateInstance("
  11. System.Drawing.Point","System.Drawing",x,y)
  12.  
  13. Pos.X = 90
  14. Pos.Y = 100
  15. MyText.Location = Pos
  16. MyText.Width = 100
  17. Pos.X = 100
  18. Pos.Y = 130
  19. MyButton.Location = Pos
  20. MyButton.Text = "Close"
  21. MyForm.Controls.Add MyText
  22. MyForm.Controls.Add MyButton
  23. MyForm.CancelButton = MyButton
  24. MyForm.ShowDialog
  25.  
  26. Msgbox MyText.Text
  27.  
  28. Set MyText = Nothing
  29. Set MyButton = Nothing
  30. Set Pos = Nothing
  31. Set MyForm = Nothing

The output will be

Custom User Form QTP

Enter a text in the box and click on Close. This will give the output a message box with text entered

Custom User Form With TextMessagebox DotNetFactory

So now you have learnt how to create the custom user form and to place controls on it. We will quickly go through how to add other controls like check box and radio button control.

To add Checkbox control we will use type ‘System.Wndows.Forms.CheckBox’ and the assembly ‘System.Wndows.Forms’ as these controls are part of same assembly.

Set MyCheckbox = DotNetFactory.CreateInstance("
System.Windows.Forms.CheckBox", "System.Windows.Forms")

Our CheckBox Control is created and need to be placed on the user form. To set the location we will use Point object again. We will use its ‘Text’ property to set the Text for checkbox and the ‘Name’ property to set a Name to the control.

Example 12

  1. Set MyForm = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.Form", "System.Windows.Forms")
  3.  
  4. Set MyCheckbox = DotNetFactory.CreateInstance("
  5. System.Windows.Forms.CheckBox", "System.Windows.Forms")
  6.  
  7. Set Pos = DotNetFactory.CreateInstance("
  8. System.Drawing.Point","System.Drawing",x,y)
  9.  
  10. With Pos
  11. .X = 90
  12. .Y = 100
  13. End With
  14.  
  15. With MyCheckBox
  16. .Location = Pos
  17. .Text = "My Check Box"
  18. .Name = chkCheckBox
  19. End With
  20.  
  21. With MyForm
  22. .Controls.Add MyCheckBox
  23. .Text = "My Custom User Form"
  24. .ShowDialog
  25. End With
  26.  
  27. Set MyCheckBox = Nothing
  28. Set Pos = Nothing
  29. Set MyForm = Nothing

Which gives the output as

Checkbox Control Form

No you can see our check box control is created, we will add the button control and place it on the user form.

Set MyButton = DotNetFactory.CreateInstance("
System.Windows.Forms.Button", "System.Windows.Forms")

We can use the ‘Checked’ Method to get the value whether it is checked or not. This method returns a Boolean value – True/False

MyCheckBox.Checked

Combining all together, the code will be like

Example 13

  1. Set MyForm = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.Form", "System.Windows.Forms")
  3.  
  4. Set MyCheckbox = DotNetFactory.CreateInstance("
  5. System.Windows.Forms.CheckBox", "System.Windows.Forms")
  6.  
  7. Set Pos = DotNetFactory.CreateInstance("
  8. System.Drawing.Point","System.Drawing",x,y)
  9.  
  10. Set MyButton = DotNetFactory.CreateInstance("
  11. System.Windows.Forms.Button", "System.Windows.Forms")
  12.  
  13. With Pos
  14. .X = 90
  15. .Y = 100
  16. End With
  17.  
  18. With MyCheckBox
  19. .Location = Pos
  20. .Text = "My Check Box"
  21. .Name = "chkCheckBox"
  22. End With
  23.  
  24. With Pos
  25. .X = 100
  26. .Y = 130
  27. End With
  28.  
  29. With MyButton
  30. .Location = Pos
  31. .Text = "Close"
  32. End With
  33.  
  34. With MyForm
  35. .Controls.Add MyCheckBox
  36. .Controls.Add MyButton
  37. .CancelButton = MyButton
  38. .Text = "My Custom User Form"
  39. .ShowDialog
  40. End With
  41.  
  42. msgbox MyCheckBox.Checked
  43.  
  44. Set MyCheckBox = Nothing
  45. Set MyButton = Nothing
  46. Set Pos = Nothing
  47. Set MyForm = Nothing

The Output will be

My Checkbox User Form

Check on the check Box on the user form and click the close button it will pop up a message as ‘True’. It will pop up as ‘False’ if the check box is checked on.

Radio Button Custom user Form

To create Radio button control we will use type name‘System.Windows.Forms.RadioButton’ and the same assembly ‘System.Windows.Forms’.

Set MyRadioButton = DotNetFactory.CreateInstance("
System.Windows.Forms.RadioButton", "System.Windows.Forms")

To define the location of Control on the user form we can use the Point object again. We can use Text Property to identify the object on form

  1. Set Pos = DotNetFactory.CreateInstance("
  2. System.Drawing.Point","System.Drawing",x,y)
  3.  
  4. With Pos
  5. .X = 90
  6. .Y = 100
  7. End With
  8.  
  9. With MyRadioButton
  10. .Location = Pos
  11. .Text = "Option 1"
  12. .Name = "optRadioButton1"
  13. End With

Now we can Create a Radiobutton on the user form. As Radio buttons are arranged in a group of two or more and allows users to choose one from the set of options in the group, the single radiobutton on the form will be of no use. We need to create at least one more control on the form. In the same way we can create another control.

  1. Set MyRadioButton1 = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.RadioButton", "System.Windows.Forms")
  3.  
  4. Set MyRadioButton2 = DotNetFactory.CreateInstance("
  5. System.Windows.Forms.RadioButton", "System.Windows.Forms")
  6.  
  7. Set Pos = DotNetFactory.CreateInstance("
  8. System.Drawing.Point","System.Drawing",x,y)
  9.  
  10. With Pos
  11. .X = 90
  12. .Y = 100
  13. End With
  14.  
  15. With MyRadioButton1
  16. .Location = Pos
  17. .Text = "Option 1"
  18. .Name = "optRadioButton1"
  19. End With
  20.  
  21. With Pos
  22. .X = 90
  23. .Y = 130
  24. End With
  25.  
  26. With MyRadioButton2
  27. .Location = Pos
  28. .Text = "Option 2"
  29. .Name = "optRadioButton1"
  30. End With

The Checked property of RadioButton control can be used to retrieve which control is selected.

This returns the Boolean value as True/False.

  1. MyRadioButton1.Checked
  2. MyRadioButton2.Checked

This property can also be used to set the default option selected. If you need to set the first radio button selected by default then set the property to True

  1. With MyRadioButton1
  2. .Location = Pos
  3. .Text = "Option 1"
  4. .Name = "optRadioButton1"
  5. .Checked = True
  6. End With

Putting it all together

Example 14

  1. Set MyForm = DotNetFactory.CreateInstance("
  2. System.Windows.Forms.Form", "System.Windows.Forms")
  3.  
  4. Set MyRadioButton1 = DotNetFactory.CreateInstance("
  5. System.Windows.Forms.RadioButton", "System.Windows.Forms")
  6.  
  7. Set MyRadioButton2 = DotNetFactory.CreateInstance("
  8. System.Windows.Forms.RadioButton", "System.Windows.Forms")
  9.  
  10. Set MyButton = DotNetFactory.CreateInstance("
  11. System.Windows.Forms.Button", "System.Windows.Forms")
  12.  
  13. Set Pos = DotNetFactory.CreateInstance("
  14. System.Drawing.Point","System.Drawing",x,y)
  15.  
  16. With Pos
  17. .X = 90
  18. .Y = 100
  19. End With
  20.  
  21. With MyRadioButton1
  22. .Location = Pos
  23. .Text = "Option 1"
  24. .Name = "optRadioButton1"
  25. End With
  26.  
  27. With Pos
  28. .X = 90
  29. .Y = 130
  30. End With
  31.  
  32. With MyRadioButton2
  33. .Location = Pos
  34. .Text = "Option 2"
  35. .Name = "optRadioButton1"
  36. End With
  37.  
  38. With Pos
  39. .X = 90
  40. .Y = 160
  41. End With
  42.  
  43. With MyButton
  44. .Location = Pos
  45. .Text = "Close"
  46. End With
  47.  
  48. With MyForm
  49. .Controls.Add MyRadioButton1
  50. .Controls.Add MyRadioButton2
  51. .Controls.Add MyButton
  52. .CancelButton = MyButton
  53. .Text = "My Custom User Form"
  54. .ShowDialog
  55. End With
  56.  
  57. If MyRadioButton1.Checked Then
  58. msgbox "You have selected - Option 1"
  59. elseif MyRadioButton2.Checked then
  60. msgbox "You have selected - Option 2"
  61. else
  62. msgbox "No Options Selected"
  63. End If
  64.  
  65. Set MyRadioButton1 = Nothing
  66. Set MyRadioButton2 = Nothing
  67. Set Button = Nothing
  68. Set Pos = Nothing
  69. Set MyForm = Nothing

This gives the output as

Radio Button Custom user Form

Select an Option and click button, it will pop up the message accordingly

User Form Message Popup

Feel like sharing your ideas with the QTP community? Contact Us