Hide

SftTree/NET 2.0 - Tree Control for Windows Forms

Display
Print

MergeSample2 (C#)

This sample demonstrates vertical and horizontal cell merging (MergeStyle = SameText).

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 System.Collections;

using Softelvdm.SftTreeNET;
using Softelvdm.Controls;

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

        private void Form1_Load(object sender, EventArgs e) {
            // This sample demonstrates cell merging. In this sample, vertical merging
            // is based on identical cell text (MergeStyle = MergeStyleEnum.SameText).
            // 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");
            CellClass cell;

            // Most of this initialization code could be eliminated by designing the control.
            // Here, all header area, footer area and item area cells are simply populated 
            // with text. The actual cell merging portion happens later on
            sftTree1.Initializing = true;
            sftTree1.Columns.Count = 5;
            sftTree1.Headers.Rows = 3;
            sftTree1.Footers.Rows = 0;
            for (int r = 0 ; r < sftTree1.Headers.Rows ; ++r) {
                for (int c = 0; c < sftTree1.Columns.Count; ++c) {
                    cell = sftTree1.Headers[r, c]; // access each header cell
                    cell.Text = "Column " + c.ToString(); // set the title
                }
            }
            ItemClass item;
            for (int i = 0; i < 4; ++i) {
                item = sftTree1.ItemCollection.Add(new string[] { "Text 0", "Text 1", "Text 2", "Text 3", "Text 4" });
                item.RowHeader.Text = "Row " + i.ToString();
            }
            // End of initialization

            // In this sample, vertical merging is based on identical cell text.
            sftTree1.MergeStyle = MergeStyleEnum.SameText;

            // set merging default for each column
            foreach (ColumnClass col in sftTree1.Columns.Collection) {
                col.MergeWithLowerDefault = true;
                col.AllowMergeFromUpperDefault = true;
            }
            // set merging default for headers and footers
            sftTree1.Headers.MergeWithLowerDefault = true;
            sftTree1.Headers.AllowMergeFromUpperDefault = true;
            sftTree1.Footers.MergeWithLowerDefault = true;
            sftTree1.Footers.AllowMergeFromUpperDefault = true;

            // Horizontally merge column header cells in row 1, columns 1 through 4
            for (int c = 1 ; c <= 4 ; ++c) {
                sftTree1.Headers[1, c].AllowMergeFromPrevious = BoolOptionalEnum.True;
                sftTree1.Headers[1, c].MergeWithNext = BoolOptionalEnum.True;
                sftTree1.Headers[1, c].Parts.Clear();
            }
            sftTree1.Headers[1, 1].Text = "Horizontally merged cells";
            sftTree1.Headers[1, 1].Image = img;

            // Horizontally merge cells in the second item (starting at column 1 through last)
            item = sftTree1.ItemCollection[1];
            for (cell = item.Cells[1]; cell != null; cell = cell.NextDisplayed) {
                cell.AllowMergeFromPrevious = BoolOptionalEnum.True;
                cell.MergeWithNext = BoolOptionalEnum.True;
                cell.Parts.Clear();
            }
            item.Cells[1].Parts.Add(new ImagePartClass(img));
            item.Cells[1].Text = "Horizontally merged cells";
            // Horizontally merge cells in the third item (starting at column 1 through last)
            item = sftTree1.ItemCollection[2];
            for (cell = item.Cells[1]; cell != null; cell = cell.NextDisplayed) {
                cell.AllowMergeFromPrevious = BoolOptionalEnum.True;
                cell.MergeWithNext = BoolOptionalEnum.True;
                cell.Parts.Clear();
            }
            item = sftTree1.ItemCollection[2];
            item.Cells[1].Parts.Add(new ImagePartClass(img));
            item.Cells[1].Text = "Another horizontally merged cell";

            // Merge 2 row headers, starting at the second item
            int count = 2;
            for (item = sftTree1.ItemCollection[1]; item != null && count > 0; count--, item = item.NextSibling) {
                RowHeaderClass rhd = item.RowHeader;
                rhd.AllowMergeFromUpper = BoolOptionalEnum.True;
                rhd.MergeWithLower = BoolOptionalEnum.True;
                rhd.Text = "Merge";
            }
            Font f = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Point);
            sftTree1.ItemCollection[1].RowHeader.TextPart.Font = f;
            sftTree1.ItemCollection[1].RowHeader.TextPart.Format |= StringFormatFlags.DirectionVertical;
            sftTree1.ItemCollection[1].RowHeader.Parts.Add(new ImagePartClass(img));

            // Make column widths and row header area width optimal
            sftTree1.Columns.MakeOptimal(0, false);
            sftTree1.RowHeaders.MakeOptimal(0, false);
            // Activate the horizontal scrollbar
            sftTree1.RecalcHorizontalExtent();
            sftTree1.Initializing = false;
        }

        // ItemClick event
        private void sftTree1_ItemClick(object sender, ItemClickEventArgs e) {
            Debug.Write("** ItemClick");
            int itemIndex = -1;
            if (e.Item != null) 
                itemIndex = e.Item.VisibleIndex;
            if (itemIndex >= 0)
                Debug.Write(" Row " + itemIndex.ToString());
            int colIndex = -1;
            if (e.Cell != null)
                colIndex = e.Cell.ColumnIndex;
            if (colIndex >= 0)
                Debug.Write(" Column " + colIndex.ToString());
            DumpValues(e);
        }

        private void sftTree1_ItemDoubleClick(object sender, ItemClickEventArgs e) {
            Debug.Write("** ItemDoubleClick");
            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("");
        }

    }
}


Spring Break!

Our offices will be closed this week (March 18 through March 22).

We'll be back March 24 to address any pending sales and support issues.