DotNetFactory interface functions (5) - Create ComboBox

Categories: DotNetClassesSource code

Parent page: Service Functions – DotNetFactory (QTP, VBScript)

Creating .NET objects from VBScript/QTP

You can create a ComboBox (Dropdown Listbox/InputBox Object) and assign required properties within one line by calling the function below. The function has one mandatory parameter (list of available selection items - packed in the Dictionary object) and a set of optional parameters provided in the Dictionary object.

Mandatory parameters through Dictionary object: “Key - Value” pairs. Keys are used to retrieve the Values only. Values will form the contents of the dropdown.

Currently supported optional parameters:

ComboBox Width and Height.

ComboBox - Left, Top.

  • p.text - Prompt text displayed by default in the input area.
  • p.index - Zero-based index of the item to be selected by default.
  • p.item - Exact string of the existing item to be selected by default.

Note. You may code as many optional parameters as required. Just follow the established syntax and keep in mind that parameter index for Dictionary must be unique - otherwise it will overwrite the previous parameter.

References

For the complete .NET ComboBox Class reference please visit:

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox(VS.80).aspx

If you want to have ComboBox editable as TextBox you need to implement a more comprehensive code covering settings for .DropDownStyle property.

Used resources

Service Functions – String (QTP, VBScript)

Implementing optional and default parameters

Create ComboBox

 Public Function CreateComboBox(ByVal objItems, ByVal objParameter)
 Dim sText, intWidth, intHeight, intMaxLength
 Dim intLeft, intTop
 Dim objComboBox, objComboBoxStyle
 Dim Iter, dvKeys, sItem, intIndex
 'Verify parameters
 If TypeName (objParameter) <> "Dictionary" Then
 Set objParameter = CreateObject("Scripting.Dictionary")
 End If
 sText = objParameter.Item("p.text")
 intWidth = InitLong(objParameter.Item("p.width"), 100)
 intHeight = InitLong(objParameter.Item("p.height"), 20)
 intLeft = IntVal(objParameter.Item("p.left"))
 intTop = IntVal(objParameter.Item("p.top"))
 intMaxLength = InitLong(objParameter.Item("p.length"), 255)
 Set objComboBoxStyle = DotNetFactory.CreateInstance("System.Windows.Forms.ComboBoxStyle", "System.Windows.Forms")
 Set objComboBox = DotNetFactory.CreateInstance("System.Windows.Forms.ComboBox", "System.Windows.Forms")
 objComboBox.Text = sText
 objComboBox.Width = intWidth
 objComboBox.Height = intHeight
 objComboBox.Left = intLeft
 objComboBox.Top = intTop
 objComboBox.MaxLength = intMaxLength
 objComboBox.DropDownStyle = objComboBoxStyle.DropDownList
 Set objComboBoxStyle = Nothing
 If TypeName (objItems) <> "Dictionary" Then
 Set CreateComboBox = objComboBox
 Exit Function
 End If
 dvKeys = objItems.Keys()
 For Iter = 0 To UBound(dvKeys)
 sItem = objItems.Item(dvKeys(Iter))
 If sItem <> "" Then
 objComboBox.Items.Add(sItem)
 End If
 Next
 If objParameter.Exists("p.index") Then
 intIndex = IntVal(objParameter.Item("p.index"))
 If intIndex <= UBound(dvKeys) Then
 objComboBox.SelectedIndex = intIndex
 End If
 End If
 If objParameter.Exists("p.item") Then
 sItem = objParameter.Item("p.item")
 intIndex = objComboBox.FindStringExact(sItem)
 If intIndex <> - 1 Then
 objComboBox.SelectedIndex = CInt(intIndex)
 End If
 End If
 Set CreateComboBox = objComboBox
 End Function

Example

Set objLst = CreateComboBox(AssociateParameters("1 = Option 1, 2 = Option 2"), AssociateParameters("p.index = 0"))

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by the author is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.