![]() | Type | Description |
Use the SelectionChanged event to notify your application that the user selects an item (that's selectable). The control supports single or multiple selection as well. When an item is selected or unselected the control fires the SelectionChanged event. Use the SingleSel property to specify if your control supports single or multiple selection. Use the SelectCount property to get the number of selected items. Use the SelectedItem property to get the selected item. Use the SelectItem to select or unselect a specified item. Use the FocusItem property to get the focused item. If the control supports only single selection, you can use the FocusItem property to get the selected/focused item because they are always the same. Use the SelForeColor and SelBackColor properties to specify colors for selected items.
The following VB sample displays the selected items:
Private Sub List1_SelectionChanged()
On Error Resume Next
Dim i As Long, j As Long, nCols As Long, nSels As Long, h As Long
nCols = List1.Columns.Count
With List1.Items
nSels = .SelectCount
For i = 0 To nSels - 1
Dim s As String
For j = 0 To nCols - 1
s = s + .Caption(.SelectedItem(i), j) + Chr(9)
Next
Debug.Print s
Next
End With
End Sub
The following VB sample displays the selected items:
Private Sub List1_SelectionChanged()
Dim i As Long
With List1.Items
For i = 0 To .SelectCount - 1
Debug.Print .Caption(.SelectedItem(i), 0)
Next
End With
End Sub
The following C++ sample displays the selected items:
#include "Items.h"
static CString V2S( VARIANT* pv, LPCTSTR szDefault = _T("") )
{
if ( pv )
{
if ( pv->vt == VT_ERROR )
return szDefault;
COleVariant vt;
vt.ChangeType( VT_BSTR, pv );
return V_BSTR( &vt );
}
return szDefault;
}
void OnSelectionChangedList1()
{
CItems items = m_list.GetItems();
for ( long i = 0; i < items.GetSelectCount(); i++ )
{
long nItem = items.GetSelectedItem( i );
CString strOutput;
strOutput.Format( "%s\n", V2S( &items.GetCaption( nItem, COleVariant( (long)0 ) ) ) );
OutputDebugString( strOutput );
}
}
The following VB.NET sample displays the selected items:
Private Sub AxList1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxList1.SelectionChanged
With AxList1.Items
Dim i As Integer
For i = 0 To .SelectCount - 1
Debug.WriteLine(.Caption(.SelectedItem(i), 0))
Next
End With
End Sub
The following C# sample displays the selected items:
private void axList1_SelectionChanged(object sender, System.EventArgs e)
{
for ( int i = 0; i < axList1.Items.SelectCount - 1; i++ )
{
object cell = axList1.Items.get_Caption( axList1.Items.get_SelectedItem( i), 0 );
System.Diagnostics.Debug.WriteLine( cell != null ? cell.ToString() : "" );
}
}
The following VFP sample displays the selected items:
*** ActiveX Control Event *** with thisform.List1.Items for i = 0 to .SelectCount - 1 wait window nowait .Caption( .SelectedItem( i ), 0 ) next endwith