Hide

SftTree/NET 2.0 - Tree Control for Windows Forms

Display
Print

CategorySample1 (VB)

This sample shows how to implement a simple toolbox-like category list.

Imports System.Reflection
Imports Softelvdm.Controls
Imports Softelvdm.SftTreeNET

Public Class Form1

Private m_PlusImage As Image = Bitmap.FromFile("..\\..\\ExpandableNormal.bmp") ' a small + bitmap
Private m_MinusImage As Image = Bitmap.FromFile("..\\..\\CollapsableNormal.bmp") ' a small - bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' This sample demonstrates how use the control as a category list (similar to 
    ' Visual Studio's Toolbox.
    ' To prepare for this sample, create a new project with a blank form and add
    ' a SftTree/NET control named sftTree1.
    ' In addition, adjust the following FromFile methods to use a (small) bitmaps
    ' that are located on your system.
    Dim img As Image = Bitmap.FromFile("..\\..\\test.gif") ' a small sample bitmap

    ' Most of this initialization code could be eliminated by designing the control.
    sftTree1.Initializing = True
    sftTree1.Headers.Rows = 0
    sftTree1.Footers.Rows = 0
    sftTree1.Dimensions.LevelIndent = 5 ' indent just a few pixels for level 1 items
    sftTree1.TreeLineStyle = TreeLineStyleEnum.None
    sftTree1.ShowExpandCollapseButtons = ShowExpandCollapseButtonsEnum.None
    sftTree1.RowHeaders.Width = 0
    sftTree1.ShowFocusRectangle = False
    sftTree1.ToolTip.ShowAlways = True
    sftTree1.ToolTip.Style = ToolTipAppearanceEnum.Balloon

    Dim cat As ItemClass
    cat = AddCategory("Category 1")
     AddItem(cat, img, "Item 1")
     AddItem(cat, img, "Item 2")
     AddItem(cat, img, "Item 3")
    cat = AddCategory("Category 2")
     AddItem(cat, img, "Item A")
     AddItem(cat, img, "Item B")
     AddItem(cat, img, "Item C")
     AddItem(cat, img, "Item D")
     AddItem(cat, img, "Item E")
    cat = AddCategory("Category 3")
     AddItem(cat, img, "Item A1")
     AddItem(cat, img, "Item A2")
    cat = AddCategory("Category 4")
     AddItem(cat, img, "Item D1")
    cat = AddCategory("Category 5")
     AddItem(cat, img, "Item G1")
     AddItem(cat, img, "Item G2")
     AddItem(cat, img, "Item G3")
     AddItem(cat, img, "Item G4")
     AddItem(cat, img, "Item G5")
     AddItem(cat, img, "Item G6")
     AddItem(cat, img, "Item G7")
     AddItem(cat, img, "Item G8")
    cat = AddCategory("Category 6")
     AddItem(cat, img, "Item aa")
     AddItem(cat, img, "Item bb")
     AddItem(cat, img, "Item cc")
     AddItem(cat, img, "Item dd")
     AddItem(cat, img, "Item ee")
    cat = AddCategory("Category 7")
     AddItem(cat, img, "Item 123")
     AddItem(cat, img, "Item abc")

    sftTree1.Columns.MakeOptimal(0, False)
    sftTree1.RecalcHorizontalExtent()
    sftTree1.Initializing = False
End Sub

Private Function AddCategory(ByVal Category As String) As ItemClass
    Dim item As ItemClass = sftTree1.ItemCollection.Add()
    Dim cell As CellClass = item.Cells(0)
    cell.Appearance = BackgroundAppearanceEnum.ThemedSystemHeaderNeverPressed
    cell.Parts.Add(New ImagePartClass(m_PlusImage))
    cell.Parts.Add(New TextPartClass(Category))
    item.TagString = "This is a tooltip explaining " & Category
    Return item
End Function

Private Sub AddItem(ByVal cat As ItemClass, ByVal img As Image, ByVal ItemText As String)
    Dim item As ItemClass = cat.Add()
    Dim cell As CellClass = item.Cells(0)
    cell.Parts.Add(New ImagePartClass(img))
    cell.Parts.Add(New TextPartClass(ItemText))
    item.TagString = "This is a tooltip for an item labeled " & ItemText
End Sub

Private Sub ClickCategory(ByVal item As ItemClass)
    If item.Level = 0 Then
        Dim fWasExpanded As Boolean = item.Expanded
        If fWasExpanded Then
            item.Collapse(CollapseStyleEnum.All)
            item.Cells(0).Image = m_PlusImage
        Else
            item.Expand(ExpandStyleEnum.All)
            item.Cells(0).Image = m_MinusImage
        End If
        item.ScrollIntoView()
    Else
        ' selected an item
    End If
End Sub

Private Sub sftTree1_ItemClick(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.ItemClickEventArgs) Handles sftTree1.ItemClick
    If e.MouseEv.Button = Windows.Forms.MouseButtons.Left And e.Area = ItemClickAreaEnum.CellSel Then
        ClickCategory(e.Item)
    End If
End Sub

Private Sub sftTree1_ItemDoubleClick(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.ItemClickEventArgs) Handles sftTree1.ItemDoubleClick
    If e.MouseEv.Button = Windows.Forms.MouseButtons.Left And e.Area = ItemClickAreaEnum.CellSel Then
        ClickCategory(e.Item)
    End If
End Sub

Private Sub sftTree1_ShowToolTip(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.ToolTipEventArgs) Handles sftTree1.ShowToolTip
    Dim item As ItemClass = e.Cell.OwningItem
    e.ToolTipText = item.TagString
End Sub

' This is a small helper routine to show all properties and fields of an object
Private Sub DumpValues(ByVal o As Object)
    Dim api() As PropertyInfo = o.GetType().GetProperties()
    For Each pi As PropertyInfo In api
        Debug.Write(" " & pi.Name & " " & pi.GetValue(o, New Object() ))
    Next
    Dim afi() As FieldInfo = o.GetType().GetFields()
    For Each fi As FieldInfo In afi
        Dim t As Object = fi.GetValue(o)
        Dim s As String = "(null)"
        If Not t Is Nothing Then s = t.ToString()
        Debug.Write(" " & fi.Name & " " & s)
    Next
    Debug.WriteLine("")
End Sub

End Class