property Items.CellCaption([Item as Variant], [ColIndex as Variant]) as Variant

Retrieves or sets the text displayed on a specific cell.

 TypeDescription 
   Item as Variant A long expression that indicates the item's handle.  
   ColIndex as Variant A long expression that indicates the column's index, or the handle to the cell, if the Item parameter is 0, a string expression that indicates the column's caption or the column's key.  
   Variant A variant expression that indicates the cell's caption. The cell's caption supports built-in HTML format.  

The CellCaption property specifies the cell's caption. To associate an user data for a cell you can use CellData property. Use the CellCaptionFormat property to use HTML tags in the cell's caption.  Use the ItemData property to associate an extra data to an item. To hide a column you have to use Visible property of the Column object. The AddItem method specifies also the caption for the first cell in the item. Use the SplitCell property to split a cell. Use the <img> HTML tag to insert icons inside the cell's caption, if the CellCaptionFormat property is exHTML. For instance, the "some image <img>1</img> other image <img>2</img> rest of text", displays combined text and icons in the cell's caption. Use the Images method to load icons at runtime. Use the ConditionalFormats method to apply formats to a cell or range of cells, and have that formatting change depending on the value of the cell or the value of a formula. Use the FormatColumn property to format the column.

Let's assume the following template in order to run the samples that follows:

The following samples uses the ItemFromPoint method to determine the hit test code from the point. The exHTCellCaptionIcon indicates that the cursor hovers an icon inside the cell's caption.

The following VB sample displays the index of the icon being clicked when the cell's caption includes <img> tags:

Private Sub Tree1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim i As HITEM, h As HitTestInfoEnum, c As Long
    With Tree1
        i = .ItemFromPoint(X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY, c, h)
    End With
    If (i <> 0) Then
        If exHTCellCaptionIcon = (h And exHTCellCaptionIcon) Then
            Debug.Print "The index of icon being clicked is: " & (h And &HFFFF0000) / 65536 & " Hex: " & Hex(h)
        End If
    End If
End Sub

The following VB sample changes the icon being clicked, by replacing the <img> tag in the CellCaption property ( if your application have access to some regular expression or an easiest way to replace strings, the idea is to replace the n-th <img>..</img> tag element with a new value, and so the icon is changed ):

Private Sub Tree1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim i As HITEM, h As HitTestInfoEnum, c As Long
    With Tree1
        i = .ItemFromPoint(X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY, c, h)
        If (i <> 0) Then
            If exHTCellCaptionIcon = (h And exHTCellCaptionIcon) Then
                Dim iImg As Long
                iImg = (h And &HFFFF0000) / 65536
                With .Items
                    Dim cellStr As String
                    cellStr = .CellCaption(i, c)
                    
                    ' Replaces the iImg-th <img> tag in the CellCaption with a new value
                    Dim n() As String
                    n = Split(cellStr, "<img>")
                    cellStr = ""
                    For j = LBound(n) To UBound(n)
                        Dim sNext As String
                        sNext = n(j)
                        If (j > LBound(n)) Then
                            cellStr = cellStr + "<img>"
                            If (j = iImg + 1) Then
                                Dim lIndex As Long, p As Long
                                p = InStr(1, sNext, "</img>")
                                lIndex = Left(sNext, p - 1)
                                lIndex = lIndex Mod 3 + 1
                                sNext = RTrim(LTrim(Str(lIndex))) + Mid(sNext, p)
                            End If
                        End If
                        cellStr = cellStr + sNext
                    Next
                    
                    .CellCaption(i, c) = cellStr
                End With
            End If
        End If
    End With
End Sub

The following C++ sample displays the index of the icon being clicked when the cell's caption includes <img> tags:

void OnMouseDownTree1(short Button, short Shift, long X, long Y) 
{
	long c = 0, hit = 0, hItem = m_tree.GetItemFromPoint( X, Y, &c, &hit );
	if ( hItem != 0 )
		if ( /*exHTCellCaptionIcon*/0x414 == ( hit & /*exHTCellCaptionIcon*/0x414 ) )
		{
			CString strOutput;
			strOutput.Format( "The index of icon being clicked is: %i, Hit = %08X\n", hit >> 16, hit );
			OutputDebugString( strOutput );
		}
}

Note: A cell is the intersection of an item with a column. All properties that has an Item and a ColIndex parameters are referring to a cell. The Item parameter represents the handle of an item, and the ColIndex parameter indicates an index ( a numerical value, see Column.Index property ) of a column , the column's caption ( a string value, see Column.Caption property ), or a handle to a cell ( see ItemCell property ). Here's few hints how to use properties with Item and ColIndex parameters:

Tree1.Items.CellBold(, Tree1.Items.ItemCell(Tree1.Items(0), 0)) = True
Tree1.Items.CellBold(Tree1.Items(0), 0) = True
Tree1.Items.CellBold(Tree1.Items(0), "ColumnName") = True

Send comments on this topic.
1999-2017 Exontrol.COM, Software. All rights reserved.