Hide

SftTree/NET 2.0 - Tree Control for Windows Forms

Display
Print

PartsSample2 (C#)

This sample demonstrates how to add items and child items, with checkboxes and radiobuttons.

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

using Softelvdm.SftTreeNET;
using Softelvdm.Controls;

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

        private void Form1_Load(object sender, EventArgs e) {
            sftTree1.Initializing = true;
            sftTree1.Columns.Count = 1;
            // This sample demonstrates how radiobuttons are used in cells.
            // 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 method to use a (small) bitmap
            // that is located on your system.
            Image img = Bitmap.FromFile("..\\..\\test.gif");

            // Add an item
            ItemClass item = sftTree1.ItemCollection.Add();
            CellClass cell = item.Cells[0];
            item.Image = img;
            cell.Text = "Category 1";
            // Add some sub items
            ItemClass subItem = item.Add();
            RadioButtonPartClass rb = new RadioButtonPartClass(RadioButtonStateEnum.Checked);
            subItem.Cells[0].Parts.Add(rb);
            subItem.Cells[0].Parts.Add(new CheckBoxPartClass());
            subItem.Cells[0].Parts.Add(new TextPartClass("Item 1"));
            subItem = item.Add();
            rb = new RadioButtonPartClass(RadioButtonStateEnum.Unchecked);
            subItem.Cells[0].Parts.Add(rb);
            subItem.Cells[0].Parts.Add(new TextPartClass("Item 2"));
            subItem = item.Add();
            rb = new RadioButtonPartClass(RadioButtonStateEnum.Unchecked);
            subItem.Cells[0].Parts.Add(rb);
            subItem.Cells[0].Parts.Add(new TextPartClass("Item 3"));

            sftTree1.Columns.MakeOptimal(0, false);
            sftTree1.RecalcHorizontalExtent();
            sftTree1.Initializing = false;
        }

        // ItemClick event
        private void sftTree1_ItemClick(object sender, ItemClickEventArgs e) {
            Debug.Write("** ItemClick");
            DumpValues(e);
        }

        // This is a small helper routine to show all properties and fields of an object
        private void DumpValues(object o) {
            PropertyInfo[] api = o.GetType().GetProperties();
            foreach (PropertyInfo pi in api)
                Debug.Write(" " + pi.Name + " " + pi.GetValue(o, new object[] ));
            FieldInfo[] afi = o.GetType().GetFields();
            foreach (FieldInfo fi in afi)
                Debug.Write(" " + fi.Name + " " + fi.GetValue(o));
            Debug.WriteLine("");
        }
    }
}