Hide

SftTree/NET 2.0 - Tree Control for Windows Forms

Display
Print

CategorySample1 (C#)

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Softelvdm.SftTreeNET;
using Softelvdm.Controls;

namespace WindowsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        Image m_PlusImage;
        Image m_MinusImage;

        private void Form1_Load(object sender, EventArgs e) {
            // 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.
            m_PlusImage = Bitmap.FromFile("..\\..\\ExpandableNormal.bmp"); // a small + bitmap
            m_MinusImage = Bitmap.FromFile("..\\..\\CollapsableNormal.bmp"); // a small - bitmap
            Image img = 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.ItemClick += sftTree1_ItemClick;
            sftTree1.ItemDoubleClick += sftTree1_ItemDoubleClick;
            sftTree1.ShowToolTip += new SftTree.ShowToolTipEventHandler(sftTree1_ShowToolTip);
            sftTree1.ToolTip.ShowAlways = true;
            sftTree1.ToolTip.Style = ToolTipAppearanceEnum.Balloon;

            ItemClass cat;
            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;
        }

        private ItemClass AddCategory(string Category) {
            ItemClass item = sftTree1.ItemCollection.Add();
            CellClass cell = 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;
        }
        private void AddItem(ItemClass cat, Image img, string ItemText) {
            ItemClass item = cat.Add();
            CellClass cell = 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;
        }

        private void ClickCategory(ItemClass item)
        {
            if (item.Level == 0) {
                bool fWasExpanded = item.Expanded;
                if (fWasExpanded) {
                    item.Collapse(CollapseStyleEnum.All);
                    item.Cells[0].Image = m_PlusImage;
                } else {
                    item.Expand(ExpandStyleEnum.All);
                    item.Cells[0].Image = m_MinusImage;
                }
                item.ScrollIntoView();
            } else {
                // selected an item
            }
        }

        void sftTree1_ShowToolTip(object sender, ToolTipEventArgs e) {
            ItemClass item = e.Cell.OwningItem;
            e.ToolTipText = item.TagString;
        }

        private void sftTree1_ItemClick(object sender, Softelvdm.SftTreeNET.ItemClickEventArgs e) {
            if (e.MouseEv.Button == MouseButtons.Left &&  e.Area == ItemClickAreaEnum.CellSel) {
                ClickCategory(e.Item);
            }
        }

        private void sftTree1_ItemDoubleClick(object sender, ItemClickEventArgs e) {
            if (e.MouseEv.Button == MouseButtons.Left && e.Area == ItemClickAreaEnum.CellSel) {
                ClickCategory(e.Item);
            }
        }

    }
}