Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

BookTable Sample (VB.NET)

This sample illustrates sorting, column reordering, responding to column header clicks, context menus, controlling expand/collapse buttons and plus/minus graphics.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - VB.NET\BookTable\Form1.vb or C:\Program Files\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - VB.NET\BookTable\Form1.vb (on 32-bit Windows versions).


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AxSftTree1.BulkUpdate = True

        ' set default item graphic.  This can also be done at design time
        AxSftTree1.Items.ItemImageExpandable.SetImageListH(ItemImageList.Handle.ToInt64(), 0)
        AxSftTree1.Items.ItemImageExpanded.SetImageListH(ItemImageList.Handle.ToInt64(), 1)
        AxSftTree1.Items.ItemImageLeaf.SetImageListH(ItemImageList.Handle.ToInt64(), 2)

        ' set the column header sort indicators
        AxSftTree1.Headers.SortIndicators = SftTreeHeaderSortIndicatorsConstants.headerSortIndicatorsSftTreeAuto
        AxSftTree1.get_Header(0).SortIndicator = SftTreeSortIndicatorConstants.sortIndicatorSftTreeAscending

        Dim R As Random
        R = New Random()

        ' set the cell font for books
        Dim fnt As Font
        fnt = New Font(AxSftTree1.Font, FontStyle.Bold)
        Dim pFont As stdole.IFontDisp
        pFont = OLECvt.ToIFontDisp(fnt)

        Dim bk As Integer
        For bk = 1 To 4
            ' add a book
            Dim bookIndex As Integer
            bookIndex = AxSftTree1.Items.Add("Book " & bk)

            AxSftTree1.get_Cell(bookIndex, 1).Text = "Description for book " + bk.ToString()
            Dim size As Integer
            If bk = 1 Then
                size = R.Next(1, 99)
            Else
                size = R.Next(1, 999)
            End If
            AxSftTree1.get_Cell(bookIndex, 2).Text = size.ToString()
            AxSftTree1.get_Item(bookIndex).Data = size
            ' add chapters
            Dim ch As Integer
            For ch = 1 To 2
                Dim index As Integer
                index = AxSftTree1.Items.Add("Chapter " & ch)
                AxSftTree1.get_Item(index).Level = 1
                ' add sections
                Dim sect As Integer
                For sect = 1 To 2
                    index = AxSftTree1.Items.Add("Section " & sect)
                    AxSftTree1.get_Item(index).Level = 2
                Next
            Next
            ' after adding the book and all dependent items, we
            ' collapse the item, so it's up to the user to expand it
            AxSftTree1.get_Item(bookIndex).Collapse(False)
            ' set font
            AxSftTree1.get_Cell(bookIndex, 0).Font = pFont
        Next
        ' End of Mass-Update
        AxSftTree1.BulkUpdate = False
        ' Make columns optimal
        AxSftTree1.ColumnsObj.MakeOptimal()
        ' allow horizontal scrolling
        AxSftTree1.Items.RecalcHorizontalExtent()
    End Sub

    Private Sub PlusMinusCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlusMinusCheckBox.CheckedChanged
        If PlusMinusCheckBox.Checked Then
            AxSftTree1.Items.PlusMinusImageExpandable.NETImageObject = PlusMinusImageList.Images(0)
            AxSftTree1.Items.PlusMinusImageExpanded.NETImageObject = PlusMinusImageList.Images(1)
        Else
            AxSftTree1.Items.PlusMinusImageExpandable.Clear()
            AxSftTree1.Items.PlusMinusImageExpanded.Clear()
        End If
    End Sub

    Private Sub UglyCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UglyCheckBox.CheckedChanged
        If UglyCheckBox.Checked Then
            AxSftTree1.ButtonPicture = ButtonImageList.Images(0)
        Else
            AxSftTree1.ButtonPicture = Nothing
        End If
    End Sub

    Private Sub SortHeader(ByVal ColNum As Integer)
        Dim SortedColumn As Integer
        ' get the new, sorted column
        SortedColumn = AxSftTree1.Headers.SortedColumn
        If SortedColumn >= 0 Then
            ' Sort the data. Note that column 2 is sorted by Item.Data, which is
            ' an integer value (book size in pages)
            If AxSftTree1.get_Header(SortedColumn).SortIndicator = SftTreeSortIndicatorConstants.sortIndicatorSftTreeAscending Then
                If SortedColumn = 2 Then
                    AxSftTree1.Items.SortDependents(-1, SortedColumn, SftTreeSortTypeConstants.sortSftTreeAscItemData)
                Else
                    AxSftTree1.Items.SortDependents(-1, SortedColumn, SftTreeSortTypeConstants.sortSftTreeAscending)
                End If
            Else
                If SortedColumn = 2 Then
                    AxSftTree1.Items.SortDependents(-1, SortedColumn, SftTreeSortTypeConstants.sortSftTreeDscItemData)
                Else
                    AxSftTree1.Items.SortDependents(-1, SortedColumn, SftTreeSortTypeConstants.sortSftTreeDescending)
                End If
            End If
        End If
    End Sub

    Private Sub AxSftTree1_ItemClick(ByVal sender As Object, ByVal e As AxSftTreeLib75._DSftTreeEvents_ItemClickEvent) Handles AxSftTree1.ItemClick
        Dim AreaType As SftTreeAreaTypeConstants
        AreaType = e.areaType
        Dim Button As SftTreeButtonConstants
        Button = e.button
        ' respond to click in column header with left mouse button
        If AreaType = SftTreeAreaTypeConstants.constSftTreeColumn And Button = SftTreeButtonConstants.constSftTreeLeftButton Then
            SortHeader(e.colIndex)
        ElseIf AreaType = SftTreeAreaTypeConstants.constSftTreeExpandAll Then
            AxSftTree1.get_Item(e.itemIndex).Expand(False, True)
        End If
    End Sub

    Private Sub AxSftTree1_ItemDblClick(ByVal sender As Object, ByVal e As AxSftTreeLib75._DSftTreeEvents_ItemDblClickEvent) Handles AxSftTree1.ItemDblClick
        Dim AreaType As SftTreeAreaTypeConstants
        AreaType = e.areaType
        Dim Button As SftTreeButtonConstants
        Button = e.button
        ' Respond to click in column header with left mouse button
        If AreaType = SftTreeAreaTypeConstants.constSftTreeColumnRes And Button = SftTreeButtonConstants.constSftTreeLeftButton Then
            AxSftTree1.get_Column(e.colIndex).MakeOptimal()
            AxSftTree1.Items.RecalcHorizontalExtent()
        ElseIf AreaType = SftTreeAreaTypeConstants.constSftTreeColumn And Button = SftTreeButtonConstants.constSftTreeLeftButton Then
            SortHeader(e.colIndex)
        End If
    End Sub

    Private Sub menuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem1.Click
        MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.")
    End Sub

    Private Sub menuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem2.Click
        MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.")
    End Sub

    Private Sub menuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem3.Click
        MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.")
    End Sub

    Private Sub menuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem4.Click
        MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.")
    End Sub

    Private Sub AxSftTree1_ContextMenuEvent(ByVal sender As Object, ByVal e As AxSftTreeLib75._DSftTreeEvents_ContextMenuEvent) Handles AxSftTree1.ContextMenuEvent
        Dim P As Point
        P = AxSftTree1.PointToScreen(New Point(e.x, e.y))

        ' Determine click context menu for header or item
        Dim L As Integer, T As Integer, H As Integer, W As Integer
        AxSftTree1.Headers.GetPositionPix(L, T, W, H)
        If e.x >= L And e.x < L + W And e.y >= T And e.y <= T + H Then
            HeaderMenu(PointToClient(P))
            Return
        End If

        ' determine item right-clicked
        Dim ItemIndex As Integer
        ItemIndex = AxSftTree1.Items.HitTestPix(e.x, e.y)
        If ItemIndex >= 0 And ItemIndex < AxSftTree1.Items.Count Then
            AxSftTree1.Items.Current = ItemIndex
            AxSftTree1.get_Item(ItemIndex).Selected = True
            AxSftTree1.CancelMode()
            ItemContextMenu.Show(Me, PointToClient(P))
        End If
    End Sub

    Private Sub menuHeader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim mItem As MenuItem
        mItem = sender
        Dim colIndex As Integer
        colIndex = mItem.Index
        If mItem.Checked Then
            AxSftTree1.get_Column(colIndex).WidthPix = 0
        Else
            AxSftTree1.get_Column(colIndex).MakeOptimal()
        End If
        AxSftTree1.Items.RecalcHorizontalExtent()
        mItem.Checked = Not mItem.Checked
    End Sub

    Private Sub menuHeaderShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        AxSftTree1.ColumnsObj.MakeOptimal()
    End Sub

    Private Sub HeaderMenu(ByVal cursor As Point)

        Dim Menu As System.Windows.Forms.ContextMenu
        Menu = New ContextMenu()
        Dim mItem As System.Windows.Forms.MenuItem

        Dim Count As Integer
        Count = 0
        Dim C As Integer
        For C = 0 To AxSftTree1.ColumnsObj.Count - 1
            mItem = New System.Windows.Forms.MenuItem()
            mItem.Text = AxSftTree1.get_Header(C).Text
            If AxSftTree1.get_Column(C).WidthPix > 0 Then
                mItem.Checked = True
                Count = Count + 1
            Else
                mItem.Checked = False
            End If
            AddHandler mItem.Click, AddressOf Me.menuHeader_Click
            Menu.MenuItems.Add(mItem)
        Next

        If Count <= 1 Then
            For C = 0 To AxSftTree1.ColumnsObj.Count - 1
                If Menu.MenuItems(C).Checked Then
                    Menu.MenuItems(C).Enabled = False
                Else
                    Menu.MenuItems(C).Enabled = True
                End If
            Next
        End If

        mItem = New System.Windows.Forms.MenuItem("-")
        Menu.MenuItems.Add(mItem)

        mItem = New System.Windows.Forms.MenuItem("&Show All")
        mItem.Enabled = (Count < AxSftTree1.ColumnsObj.Count)
        AddHandler mItem.Click, AddressOf Me.menuHeaderShowAll_Click
        Menu.MenuItems.Add(mItem)

        AxSftTree1.CancelMode()
        Menu.Show(Me, cursor)
    End Sub

End Class

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.