Selected Property

Applies to TestComplete 15.70, last modified on December 17, 2024

Specifies whether a particular list box item is selected.

Declaration

componentObj.Selected(Index)
Read-Write Property Boolean
Parameters
componentObj     One of the user forms components listed in the Applies To section  
Index [in] Required Integer

Applies To

The property applies to the following components:

TcxListBox

Description

The Selected property lets you determine whether a particular list box item is selected. Using this property, you can also select and unselect list box items.

Note: The Selected property is not displayed among other TcxListBox properties in the User Forms editor. However, it is available in scripts and displayed in the Code Completion window.

Parameters

The property has one parameter:

Index

Specifies the zero-based index of the list box item.

Property Value

True, if the specified item is selected; False otherwise.

Remarks

If you use Python or DelphiScript, you should enclose the parameter of the Selected property in square brackets: Selected[Index].

For those list box components that work in single-selection mode, you can quickly determine the selected item’s index using the ItemIndex property. If the list box supports multiple selection (that is, if its MultiSelect property is True), you can get the number of selected items using the SelCount property. To get the selected items’ indexes, you can iterate through the list box items and check the Selected property value for each item.

Example

The following example demonstrates how you can obtain an array of the selected items' indexes using the Selected property:

JavaScript, JScript

function GetSelIndexes (listBox)
{
  var indexes = new Array ();

  // Iterate through the list box items
  for (var i = 0; i < listBox.Items.Count; i++)
    // If the item is selected, ...
    if (listBox.Selected(i))
      // ... save its index to the array
      indexes.push(i);

  return indexes;
}

Python

def GetSelIndexes (listBox):
  indexes = []

  # Iterate through the list box items
  for i in range (0, listBox.Items.Count):
    # If the item is selected, ...
    if (listBox.Selected[i]):
      # ... save its index to the array
      indexes.append(i)

  return indexes

VBScript

Function GetSelIndexes (listBox)
  Dim indexes, i, j

  indexes = Array (listBox.SelCount)
  j = 0 ' Counter of indexes elements

  ' Iterate through the list box items
  For i = 0 To listBox.Items.Count-1
    ' If the item is selected, ...
    If listBox.Selected(i) Then
      ' ... save its index to the array
      Indexes(j) = i
      j = j + 1
    End If
  Next

  GetSelIndexes = indexes
End Function

DelphiScript

function GetSelIndexes (listBox);
var indexes, i, j : OleVariant;
begin
  indexes := CreateVariantArray (0, listBox.SelCount-1);
  j := 0; // Counter of indexes elements

  // Iterate through the list box items
  for i := 0 to listBox.Items.Count-1 do
    // If the item is selected, ...
    if listBox.Selected[i] then
    begin
      // ... save its index to the array
      indexes[j] := i;
      j := j + 1
    end;

  Result := indexes;
end;

C++Script, C#Script

function GetSelIndexes (listBox)
{
  var indexes = new Array ();

  // Iterate through the list box items
  for (var i = 0; i < listBox["Items"]["Count"]; i++)
    // If the item is selected, ...
    if (listBox["Selected"](i))
      // ... save its index to the array
      indexes["push"](i);

  return indexes;
}

See Also

ItemIndex Property
MultiSelect Property
SelCount Property

Highlight search results