property Items.SelectedItem ([Index as Long]) as HITEM

Retrieves the selected item's handle given its index in selected items collection.

TypeDescription
Index as Long Identifies the index of the selected item into the selected items collection.
HITEM A long expression that indicates the handle of the selected item.

Use the SelectedItem property to get the handle of the selected item(s) in the control. Use the SelectCount property to find out how many items are selected in the control. The control fires the SelectionChanged  event when user changes the selection in the control. Use the SelectItem property to select programmatically an 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 SingleSel property to enable single or multiple selection. Use the SelForeColor and SelBackColor properties to specify colors for selected items.

The following sample shows hot to print the caption for the selected item: Debug.Print G2antt1.Items.CellValue(G2antt1.Items.SelectedItem(0), 0).  

The following sample applies an italic font attribute to the selected item:

Private Sub G2antt1_SelectionChanged()
    If Not (h = 0) Then G2antt1.Items.ItemItalic(h) = False
    h = G2antt1.Items.SelectedItem()
    G2antt1.Items.ItemItalic(h) = True
End Sub

The following VB sample enumerates the selected items:

Dim i As Long
With G2antt1.Items
    For i = 0 To .SelectCount - 1
        Debug.Print .CellValue(.SelectedItem(i), 0)
    Next
End With

The following VB sample unselects all items in the control:

With G2antt1
    .BeginUpdate
    With .Items
        While Not .SelectCount = 0
            .SelectItem(.SelectedItem(0)) = False
        Wend
    End With
    .EndUpdate
End With

The following VC 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;
}

CItems items = m_g2antt.GetItems();
for ( long i = 0; i < items.GetSelectCount(); i++ )
{
	COleVariant vtItem( items.GetSelectedItem( i ) );
	CString strOutput;
	strOutput.Format( "%s\n", V2S( &items.GetCellValue( vtItem, COleVariant( (long)0 ) ) ) );
	OutputDebugString( strOutput );
}

The following C++ sample unselects all items in the control:

m_g2antt.BeginUpdate();
CItems items = m_g2antt.GetItems();
while ( items.GetSelectCount() )
	items.SetSelectItem( items.GetSelectedItem( 0 ), FALSE );
m_g2antt.EndUpdate();

The following VB.NET sample displays the selected items:

With AxG2antt1.Items
    Dim i As Integer
    For i = 0 To .SelectCount - 1
        Debug.WriteLine(.CellValue(.SelectedItem(i), 0))
    Next
End With

The following VB.NET sample unselects all items in the control:

With AxG2antt1
    .BeginUpdate()
    With .Items
        While Not .SelectCount = 0
            .SelectItem(.SelectedItem(0)) = False
        End While
    End With
    .EndUpdate()
End With

The following C# sample displays the selected items:

for ( int i = 0; i < axG2antt1.Items.SelectCount - 1; i++ )
{
	object cell = axG2antt1.Items.get_CellValue( axG2antt1.Items.get_SelectedItem( i), 0 );
	System.Diagnostics.Debug.WriteLine( cell != null ? cell.ToString() : "" );
}

The following C# sample unselects all items in the control:

axG2antt1.BeginUpdate();
EXG2ANTTLib.Items items = axG2antt1.Items;
while (items.SelectCount != 0)
	items.set_SelectItem(items.get_SelectedItem(0), false);
axG2antt1.EndUpdate();

The following VFP sample displays the selected items:

with thisform.G2antt1.Items
	for i = 0 to .SelectCount - 1
		.DefaultItem = .SelectedItem( i )
		wait window nowait .CellValue( 0, 0 )
	next
endwith

The following VFP sample unselects all items in the control:

With thisform.G2antt1
	.BeginUpdate()
	with .Items
		do while ( .SelectCount() # 0 )	
			.DefaultItem = .SelectedItem(0)
			.SelectItem(0) = .f.
		enddo
	endwith
	.EndUpdate()
EndWith