Skybound AutoComplete

AutoCompleteManager.DrawItem Event

Occurs when DrawMode is OwnerDraw, and an item needs to be painted in the drop-down suggestion window.

[Visual Basic]
Public Event DrawItem As DrawItemEventHandler
[C#]
public event DrawItemEventHandler DrawItem;

Event Data

The event handler receives an argument of type DrawItemEventArgs containing data related to this event. The following DrawItemEventArgs properties provide information specific to this event.

Property Description
AutoComplete Gets the instance of AutoComplete that raised the event.
BackColor Gets the background color of the item.
Bounds Gets the bounding rectangle of the item.
Font Gets the font used to draw the item.
ForeColor Gets the foreground (text) color used to draw the item.
Graphics Gets the graphics surface on which the item is drawn.
ItemHeight Gets or sets the height of the item.
ItemIndex Gets the index of the item for which the event was raised.
ItemValue Gets the item object for which the event was raised.
State Gets a value that specifies one or more values that determine the state of the item.
Text Gets the string representation of the item, as it is displayed in the drop-down suggestion window.
VisibleIndex Gets the index of the visible item for which the event was raised.

Example

This is an example handler for the DrawItem event. It draws the background of the highlight bar using a gradient fill, and displays the file size or "Folder" beside each item in the list (depending on what it is). We are assuming in this example that files are being suggested in the drop-down suggestion window.

[C#]

private void autoComplete1_DrawItem(object sender, Skybound.AutoComplete.DrawItemEventArgs e)
{
    bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
    
    if (selected)
    {
        // make a gradient highlight brush
        using (Brush brush = new LinearGradientBrush(
            e.Bounds, SystemColors.Highlight, ControlPaint.Light(SystemColors.Highlight),
            LinearGradientMode.Vertical))
        {
            e.Graphics.FillRectangle(brush, e.Bounds);
        }
    }
    else
    {
        e.DrawBackground();
    }
    
    // draw image and text
    e.DrawImage(e.Bounds.Location + new Size(4, 0));
    e.DrawText(e.Bounds.Location + new Size(22, 1));
    
    // get the full path to the file
    string fileName = e.GetFullPath();
    if (fileName != null)
    {
        string extra;
        
        // retrieve information about the file
        FileInfo info = new FileInfo(fileName);
        
        if ((info.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
        {
            extra = "Folder";
        }
        else
        {
            extra = (info.Length / 1024).ToString("#,##0") + " KB";
        }
        
        // draw the additional information directly in front of the other text
        Point location = new Point(e.Bounds.X + e.MeasureText().Width + 28, e.Bounds.Y + 1);
        
        e.DrawText(location, extra, selected ? Color.LightGray : Color.Gray);
    }
}

[Visual Basic]

Private Sub autoComplete1_DrawItem(ByVal sender As Object, _
    ByVal e As Skybound.AutoComplete.DrawItemEventArgs)

    Dim selected As Boolean = ((e.State And DrawItemState.Selected) = DrawItemState.Selected)
    
    If selected Then
        ' make a gradient highlight brush
        Dim brush As Brush = New LinearGradientBrush(e.Bounds, SystemColors.Highlight, _
            ControlPaint.Light(SystemColors.Highlight), LinearGradientMode.Vertical)
        e.Graphics.FillRectangle(brush, e.Bounds)
    Else
        e.DrawBackground
    End If
    
    ' draw image and text
    e.DrawImage((e.Bounds.Location + New Size(4, 0)))
    e.DrawText((e.Bounds.Location + New Size(22, 1)))
    
    ' get the full path to the file
    Dim fileName As String = e.GetFullPath
    
    If (Not (fileName) Is Nothing) Then
        Dim extra As String
        
        ' retrieve information about the file
        Dim info As FileInfo = New FileInfo(fileName)
        
        If ((info.Attributes And FileAttributes.Directory) = FileAttributes.Directory) Then
            extra = "Folder"
        Else
            extra = ((info.Length / 1024).ToString("#,##0") + " KB")
        End If
        
        ' draw the additional information directly in front of the other text
        Dim location As Point = New Point((e.Bounds.X  _
                        + (e.MeasureText.Width + 28)), (e.Bounds.Y + 1))
        
        e.DrawText(location, extra, IIf(selected, Color.LightGray, Color.Gray))
    End If
End Sub

See Also

AutoCompleteManager Class | Skybound.AutoComplete Namespace