Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

BookTable Sample (C#)

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 - CSharp\BookTable\Form1.cs or C:\Program Files\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - CSharp\BookTable\Form1.cs (on 32-bit Windows versions).


        private void button1_Click(object sender, System.EventArgs e) {
            Application.Exit();
        }

        private void Form1_Load(object sender, System.EventArgs e) {

            axSftTree1.BulkUpdate = true;

            // set default item graphic.  This can also be done at design time
            axSftTree1.Items.ItemImageExpandable.SetImageListH((long)itemImageList.Handle, 0);
            axSftTree1.Items.ItemImageExpanded.SetImageListH((long)itemImageList.Handle, 1);
            axSftTree1.Items.ItemImageLeaf.SetImageListH((long)itemImageList.Handle, 2);

            // set the column header sort indicators
            axSftTree1.Headers.SortIndicators = SftTreeHeaderSortIndicatorsConstants.headerSortIndicatorsSftTreeAuto;
            axSftTree1.get_Header(0).SortIndicator = SftTreeSortIndicatorConstants.sortIndicatorSftTreeAscending;

            Random random = new Random();

            // set the cell font for books
            Font fnt = new Font(axSftTree1.Font, FontStyle.Bold);
            stdole.IFontDisp pFont = OLECvt.ToIFontDisp(fnt);

            for (int bk = 1 ; bk <= 4 ; ++bk) {
                // add a book
                int bookIndex = axSftTree1.Items.Add("Book " + bk.ToString());

                axSftTree1.get_Cell(bookIndex, 1).Text = "Description for book " + bk.ToString();
                int size = random.Next(1, bk == 1 ? 99 : 999);
                axSftTree1.get_Cell(bookIndex, 2).Text = size.ToString();
                axSftTree1.get_Item(bookIndex).Data = size;
                // add chapters
                for (int ch = 1 ; ch <= 2 ; ++ch) {
                    int index = axSftTree1.Items.Add("Chapter " + ch.ToString());
                    axSftTree1.get_Item(index).Level = 1;
                    // add sections
                    for (int sect = 1 ; sect <= 2 ; ++sect) {
                        index = axSftTree1.Items.Add("Section " + sect.ToString());
                        axSftTree1.get_Item(index).Level = 2;
                    }
                }
                // 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;
            }
            // End of Mass-Update
            axSftTree1.BulkUpdate = false;
            // Make columns optimal
            axSftTree1.ColumnsObj.MakeOptimal();
            // allow horizontal scrolling
            axSftTree1.Items.RecalcHorizontalExtent();
        }

        private void plusMinusCheckBox_CheckedChanged(object sender, System.EventArgs e) {
            if (plusMinusCheckBox.Checked) {
                axSftTree1.Items.PlusMinusImageExpandable.NETImageObject = plusMinusImageList.Images[0];
                axSftTree1.Items.PlusMinusImageExpanded.NETImageObject = plusMinusImageList.Images[1];
            } else {
                axSftTree1.Items.PlusMinusImageExpandable.Clear();
                axSftTree1.Items.PlusMinusImageExpanded.Clear();
            }
        }

        private void uglyCheckBox_CheckedChanged(object sender, System.EventArgs e) {
            if (uglyCheckBox.Checked)
                axSftTree1.ButtonPicture = buttonImageList.Images[0];
            else
                axSftTree1.ButtonPicture = null;
        }

        private void SortHeader(short ColNum) {
            // get the new, sorted column
            short sortedColumn = axSftTree1.Headers.SortedColumn;
            // Sort the data based on the sort indicator
            // 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) {
                if (sortedColumn == 2)
                    axSftTree1.Items.SortDependents(-1, sortedColumn, SftTreeSortTypeConstants.sortSftTreeAscItemData);
                else
                    axSftTree1.Items.SortDependents(-1, sortedColumn, SftTreeSortTypeConstants.sortSftTreeAscending);
            } else {
                // Sort the data. Note that column 2 is sorted by Item.Data, which is
                // an integer value (book size in pages)
                if (sortedColumn == 2)
                    axSftTree1.Items.SortDependents(-1, sortedColumn, SftTreeSortTypeConstants.sortSftTreeDscItemData);
                else
                    axSftTree1.Items.SortDependents(-1, sortedColumn, SftTreeSortTypeConstants.sortSftTreeDescending);
            }
        }

        private void axSftTree1_ItemClick(object sender, AxSftTreeLib75._DSftTreeEvents_ItemClickEvent e) {
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            SftTreeButtonConstants button = (SftTreeButtonConstants) e.button;
            // respond to click in column header with left mouse button
            if (area == SftTreeAreaTypeConstants.constSftTreeColumn && button == SftTreeButtonConstants.constSftTreeLeftButton)
                SortHeader(e.colIndex);
            else if (area == SftTreeAreaTypeConstants.constSftTreeExpandAll)
                axSftTree1.get_Item(e.itemIndex).Expand(false, true);
        }

        private void axSftTree1_ItemDblClick(object sender, AxSftTreeLib75._DSftTreeEvents_ItemDblClickEvent e) {
            // Respond to click in column header with left mouse button
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            SftTreeButtonConstants button = (SftTreeButtonConstants) e.button;
            if (area == SftTreeAreaTypeConstants.constSftTreeColumnRes && button == SftTreeButtonConstants.constSftTreeLeftButton) {
                axSftTree1.get_Column(e.colIndex).MakeOptimal();
                axSftTree1.Items.RecalcHorizontalExtent();
            } else if (area == SftTreeAreaTypeConstants.constSftTreeColumn && button == SftTreeButtonConstants.constSftTreeLeftButton)
                SortHeader(e.colIndex);
        }

        private void menuItem1_Click(object sender, System.EventArgs e) {
            MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.");
        }

        private void menuItem2_Click(object sender, System.EventArgs e) {
            MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.");
        }

        private void menuItem3_Click(object sender, System.EventArgs e) {
            MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.");
        }

        private void menuItem4_Click(object sender, System.EventArgs e) {
            MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.");
        }

        private void axSftTree1_ContextMenuEvent(object sender, AxSftTreeLib75._DSftTreeEvents_ContextMenuEvent e)
        {
            Point point = axSftTree1.PointToScreen(new Point(e.x, e.y));

            // Determine click context menu for header or item
            int l, t, h, w;
            axSftTree1.Headers.GetPositionPix(out l, out t, out w, out h);
            if (e.x >= l && e.x < l + w && e.y >= t && e.y <= t + h) {
                HeaderMenu(PointToClient(point));
                return;
            }

            // determine item right-clicked
            int ItemIndex = axSftTree1.Items.HitTestPix(e.x, e.y);
            if (ItemIndex >= 0 && ItemIndex < axSftTree1.Items.Count) {
                axSftTree1.Items.Current = ItemIndex;
                axSftTree1.get_Item(ItemIndex).Selected = true;
                axSftTree1.CancelMode();
                ItemContextMenu.Show(this, PointToClient(point));
            }
        }

        private void menuHeader_Click(object sender, System.EventArgs e) {
            MenuItem mItem = (MenuItem) sender;
            short colIndex = (short) mItem.Index;
            if (mItem.Checked)
                axSftTree1.get_Column(colIndex).WidthPix = 0;
            else
                axSftTree1.get_Column(colIndex).MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();
            mItem.Checked = !mItem.Checked;
        }

        private void menuHeaderShowAll_Click(object sender, System.EventArgs e) {
            axSftTree1.ColumnsObj.MakeOptimal();
        }

        private void HeaderMenu(Point cursor) {

            System.Windows.Forms.ContextMenu menu = new ContextMenu();
            System.Windows.Forms.MenuItem mItem;

            int count = 0;
            for (short c = 0 ; c < axSftTree1.ColumnsObj.Count ; ++c) {
                mItem = new System.Windows.Forms.MenuItem();
                mItem.Text = axSftTree1.get_Header(c).Text;
                if (axSftTree1.get_Column(c).WidthPix > 0) {
                    mItem.Checked = true;
                    ++count;
                } else
                    mItem.Checked = false;
                mItem.Click += new System.EventHandler(menuHeader_Click);
                menu.MenuItems.Add(mItem);
            }

            if (count <= 1) {
                for (short c = 0 ; c < axSftTree1.ColumnsObj.Count ; ++c) {
                    if (menu.MenuItems[c].Checked)
                        menu.MenuItems[c].Enabled = false;
                    else
                        menu.MenuItems[c].Enabled = true;
                }
            }

            mItem = new System.Windows.Forms.MenuItem("-");
            menu.MenuItems.Add(mItem);

            mItem = new System.Windows.Forms.MenuItem("&Show All");
            mItem.Enabled = (count < axSftTree1.ColumnsObj.Count);
            mItem.Click += new System.EventHandler(menuHeaderShowAll_Click);
            menu.MenuItems.Add(mItem);

            axSftTree1.CancelMode();
            menu.Show(this, cursor);
        }
    }
}

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