[ create a new paste ] login | about

Link: http://codepad.org/cdpiHCNj    [ raw code | fork ]

C++, pasted on Oct 10:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data; // for DataTable

namespace WindowsFormsApplication1 {
    class ControlListView : ListView {
        private Color altColor;
        private bool bColumnResize;
        private DataTable dt = null;

        public Color AlternateColor {
            get { return this.altColor; }
            set { this.altColor = value; }
        }

        public bool ResizableColumns {
            get { return this.bColumnResize; }
            set { this.bColumnResize = value; }
        }

        public ControlListView() {
            this.dt = new DataTable();
            this.Font = new Font("Microsoft Sans Serif", 12);
            this.GridLines = true;
            this.OwnerDraw = true;
            this.LabelEdit = true;
            this.View = View.Details;
            this.LabelWrap = false;
            this.MultiSelect = false;
            this.ShowGroups = false;
            this.AllowColumnReorder = false;
            this.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            this.FullRowSelect = true;
            this.Scrollable = false;
            this.altColor = this.BackColor;
            this.ResizableColumns = false;
        }

        public void DetermineInitialSize() {
            this.Height = 488;
        }

        public void AddControl(Control ctrl, int col, int row) {
            if (col < this.Columns.Count && row < this.Items.Count) {
                // determine location of new control
                int locY = this.Items[row].Bounds.Top;
                if (col == 0)
                    ctrl.Location = new Point(0, locY);
                else {
                    int locX = 0;
                    for (int i = 0; i < col; i++)
                        locX += this.Columns[i].Width;
                    ctrl.Location = new Point(locX, locY);
                }

                // assign width of column to new control
                ctrl.Width = this.Columns[col].Width;

                // configure size properties of added control
                if (ctrl.GetType() == typeof(ComboBox)) {
                    ctrl.Font = new Font("Arial", 10);
                    ((ComboBox)ctrl).DropDownStyle = ComboBoxStyle.DropDownList;
                }
                else if (ctrl.GetType() == typeof(CheckBox)) {
                    ((CheckBox)ctrl).CheckAlign = ContentAlignment.MiddleCenter;

                    if (row % 2 == 1)
                        ctrl.BackColor = this.altColor;
                }

                // configure unique name for added item
                ctrl.Name = "field[" + col + "][" + row + "]";

                // add this control to the listview
                this.Controls.Add(ctrl);
            }
        }

        public void AddColumn(string sColName, int iWidth, HorizontalAlignment haAlign = HorizontalAlignment.Left) {
            this.Columns.Add(sColName, iWidth, haAlign);
            dt.Columns.Add(sColName);

            int wdth = 9;
            for (int i = 0; i < this.Columns.Count; i++)
                wdth += this.Columns[i].Width;

            this.Width = wdth;
        }

        public ListViewItem AddItem(string sItem) {
            ListViewItem item = this.Items.Add(sItem);

            if (this.Items.Count > 19)
                this.Height = this.Items[0].Bounds.Top + this.Items[0].Bounds.Height * (this.Items.Count + 1);

            return item;
        }

        public void RemoveItem(int col, int row) {

        }

        protected override void OnMouseWheel(MouseEventArgs e) {
            this.Parent.Focus();
        }

        protected override void OnColumnWidthChanging(ColumnWidthChangingEventArgs e) {
            if (!bColumnResize) {
                //Keep the width not changed.
                e.NewWidth = this.Columns[e.ColumnIndex].Width;
                //Cancel the event.
                e.Cancel = true;
            }
        }

        protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e) {
            // since OwnerDraw is set to true, must draw default
            e.DrawDefault = true;
        }

        protected override void OnDrawItem(DrawListViewItemEventArgs e) {
            e.DrawDefault = true;
            // draw alternating grid background color
            e.Item.BackColor = e.ItemIndex % 2 == 0 ? this.BackColor : this.altColor;
        }
    }
}


Create a new paste based on this one


Comments: