Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / Windows Forms

Controls Library: Extended ListView with Column Mapping

Rate me:
Please Sign up or sign in to vote.
4.86/5 (29 votes)
27 Mar 2014CPOL5 min read 83.2K   7.6K   110  
Docking windows container, extended listview, extended property editor.
using System;
using System.Drawing;
using System.Windows.Forms;
using dwf.tool;
using System.Collections;
using System.ComponentModel;

namespace dwf.gui
{
    public class CellEditorText : IPCellEditor, IDisposable
    {
        protected IList _listDataSource;
        protected bool password = false;
        protected bool multiLine = false;
        protected bool visibleHeader = false;
        protected bool dropDownTool = true;
        protected bool dropDownVisible = true;
        protected bool dropDownExVisible = false;
        protected bool handleText = true;
        protected bool dropDownAutoHide = false;
        protected bool filtering = false;
        //protected bool dropDownByKey = true;
        protected string format;
        protected string header;
        protected object item;
        protected EventHandler valueHandler;
        protected PEditor control;
        protected Type type;
        protected Type editType;
        protected ToolWindow tool;
        protected Control content;
        protected Font font = new Font("Arial", 8);
        public string ListProperty = "ToString";
        public bool ListAutoSort = true;
        protected string filter = string.Empty;

        public CellEditorText()
        {
            valueHandler = new EventHandler(OnControlValueChanged);
            editType = typeof(RichTextBox);
        }

        public IList DataSource
        {
            get { return _listDataSource; }
            set
            {
                _listDataSource = value;
                Filtering = true;
            }
        }

        public bool Filtering
        {
            get { return filtering; }
            set { filtering = value; }
        }

        public string Format
        {
            get { return this.format; }
            set { format = value; }
        }

        public Font Font
        {
            get { return this.font; }
            set { font = value; }
        }

        public bool Password
        {
            get { return this.password; }
            set { password = value; }
        }

        public bool DropDownTool
        {
            get { return this.dropDownTool; }
            set
            {
                dropDownTool = value;
                DropDownVisible = value;
            }
        }

        public bool DropDownVisible
        {
            get { return this.dropDownVisible; }
            set { dropDownVisible = value; }
        }

        public bool DropDownExVisible
        {
            get { return this.dropDownExVisible; }
            set { dropDownExVisible = value; }
        }

        public bool Masked { get; set; }

        public bool HandleTextChanged
        {
            get { return this.handleText; }
            set { handleText = value; }
        }

        public ToolWindow ToolWindow
        {
            get { return tool; }
            set { tool = value; }
        }

        public string Header
        {
            get { return header; }
            set { header = value; }
        }

        public bool MultiLine
        {
            get { return multiLine; }
            set { multiLine = value; }
        }

        public bool VisibleHeader
        {
            get { return visibleHeader; }
            set
            {
                if (visibleHeader == value)
                    return;
                visibleHeader = value;
                if (tool != null)
                    tool.HeaderVisible = visibleHeader;
            }
        }
        public string Text
        {
            get
            {
                string val = null;
                if (control == null || control.EditControl == null)
                    return null;
                else if (control.EditControl is MaskedTextBox)
                {
                    if (((MaskedTextBox)control.EditControl).Mask != string.Empty)
                        val = ((MaskedTextBox)control.EditControl).MaskCompleted ? ((MaskedTextBox)control.EditControl).Text : string.Empty;
                    else
                        val = ((MaskedTextBox)control.EditControl).Text;
                }
                else if (control.EditControl is TextBoxBase)
                    val = ((TextBoxBase)control.EditControl).Text;
                return val;
            }
            set
            {
                bool flag = handleText;
                handleText = false;
                if (control != null && control.EditControl is TextBoxBase)
                    ((TextBoxBase)control.EditControl).Text = value;
                if (tool != null && tool.Target is TextBoxBase)
                    ((TextBoxBase)tool.Target).Text = value;
                handleText = flag;
            }
        }

        public Type EditType
        {
            get { return this.editType; }
            set
            {
                editType = value;
            }
        }

        public IPEditor EditControl
        {
            get { return control; }
        }

        public Type ValueType
        {
            get { return type; }
            set { type = value; }
        }

        public object EditItem
        {
            get { return item; }
        }

        public TextBoxBase TextControl
        {
            get { return control.EditControl as TextBoxBase; }
        }

        protected virtual void ListReset()
        {

        }

        protected virtual IList ListFind(string filter)
        {
            var param = new ListQueryParam() { Type = _listDataSource[0].GetType(), Property = ListProperty, Value = filter, Comparer = CompareType.Like };
            return ListTool.Search(_listDataSource, param);
        }

        protected virtual void ListSelect(IList flist)
        {
            if (flist.Count > 0)
            {
                ((PList)tool.Target).Selection._Clear();
                ((PList)tool.Target).Selection.AddRange(flist);
            }
            else
            {
                ((PList)tool.Target).Selection.Clear();
            }
        }

        protected virtual void SetFilter(string filter)
        {
            this.filter = filter;
            if (filter != string.Empty)
            {
                IList flist = ListFind(filter);
                ListSelect(flist);

                if (flist.Count == 1)
                {
                    control.Value = ParseValue(flist[0], item, type);

                    string value = FormatValue(flist[0], item, type) as string;
                    int index = value.IndexOf(filter, StringComparison.OrdinalIgnoreCase);
                    TextControl.Text = value;
                    TextControl.SelectionStart = index + filter.Length;
                    TextControl.SelectionLength = value.Length - TextControl.SelectionStart;

                    tool.Close();
                    handleText = true;
                }
                else if (flist.Count > 1)
                {
                    control.ShowDropDown(ToolShowMode.AutoHide);
                    handleText = true;
                }
            }
            else
                ListReset();
        }

        protected virtual void OnControlValueChanged(object sender, EventArgs e)
        {
            if (handleText)
            {
                handleText = false;
                if (Filtering && !((Control)sender).Text.Equals(filter, StringComparison.OrdinalIgnoreCase))
                {
                    SetFilter(TextControl.Text.Replace("\r\n", "").Replace("\r", ""));
                }

                if (!handleText)
                    try
                    {
                        if (sender != control.EditControl && control.EditControl is TextBoxBase)
                            ((TextBoxBase)control.EditControl).Text = ((Control)sender).Text;
                        string val = Text;
                        control.Value = ParseValue(val);
                    }
                    catch (Exception ex)
                    {
                        if (control != null)
                        {
                            GuiTool.ToolTip.LableText = "Input error";
                            GuiTool.ToolTip.Content = ex.Message + "\n" + ex.StackTrace;
                            GuiTool.ToolTip.Show(control, new Point(0, control.Height));
                        }
                    }
                handleText = true;
            }
        }

        public virtual object ParseValue(object value)
        {
            return ParseValue(value, item, type);
        }

        public virtual object FormatValue(object value)
        {
            return FormatValue(value, item, type);
        }

        public virtual object ParseValue(object Value, object dataSource, Type vType)
        {
            if (Value == null || Value.ToString() == string.Empty)
            {
                return null;
            }
            else if (Value.GetType() == vType)
            {
                return Value;
            }
            else if (Value is string)
            {
                if (vType == typeof(Color))
                {
                    ColorConverter cc = new ColorConverter();
                    return cc.ConvertFromString((string)Value);
                }
                return Tool.TextParse((string)Value, vType, format);
            }
            else
                return Value;
        }

        public virtual object FormatValue(object obj, object dataSource, Type vtype)
        {
            if (password)
                return "*******";
            if (obj is Image)
                return obj;
            return Tool.TextFormat(obj, format);
        }

        protected void DefaultInitialize(IPEditor control, object value, object dataSource, Type valueType)
        {
            filter = string.Empty;
            if (control.CurrentEditor != null)
            {
                control.CurrentEditor.FreeEditControl();
            }

            control.CurrentEditor = this;
            control.DropDownVisible = dropDownVisible;
            control.DropDownExVisible = dropDownExVisible;
            control.DropDownAutoHide = dropDownAutoHide;

            this.control = control as PEditor;
            type = valueType;
            item = dataSource;

            control.Value = value;
        }

        public virtual void InitializeEditControl(IPEditor ccontrol, object value, object dataSource, Type valueType, bool readOnly)
        {
            DefaultInitialize(ccontrol, value, dataSource, valueType);

            if (Password || Masked)
            {
                var box = control.GetCacheControl<MaskedTextBox>("MaskedTextBox");
                control.EditControl = box;
                if (Password)
                    box.PasswordChar = '*';
                else
                    box.PasswordChar = (char)0;

                if (format != null)
                {
                    box.Mask = format;
                    box.ValidatingType = type;
                }
                else
                    box.Mask = string.Empty;
            }
            else
            {
                var box = control.GetCacheControl<TextBox>("TextBox");
                control.EditControl = box;
                if (format != null && type == typeof(string))
                {
                    box.MaxLength = format.Length;
                    box.KeyPress += TextBoxKeyPress;
                }
                else
                    box.MaxLength = 0;
            }
            ((TextBoxBase)control.EditControl).KeyDown += TextBoxKeyDown;
            ((TextBoxBase)control.EditControl).KeyUp += TextBoxKeyUp;
            ((TextBoxBase)control.EditControl).KeyPress += TextBoxKeyPress;
            ((TextBoxBase)control.EditControl).BorderStyle = BorderStyle.None;
            ((TextBoxBase)control.EditControl).Font = Font;
            ((TextBoxBase)control.EditControl).Multiline = multiLine && ((TextBoxBase)control.EditControl).Height > 20;
            ((TextBoxBase)control.EditControl).ReadOnly = readOnly;
            if (!readOnly && handleText)
                ((TextBoxBase)control.EditControl).TextChanged += valueHandler;

            if (DropDownTool)
            {
                tool = control.GetCacheControl<ToolWindow>("ToolWindow");
                tool.HeaderVisible = visibleHeader;
                tool.Label.Text = header;
                control.DropDown = tool;
                if (_listDataSource == null)
                {
                    tool.Target = control.GetCacheControl<RichTextBox>("RichTextBox");
                    ((RichTextBox)tool.Target).WordWrap = true;
                    ((RichTextBox)tool.Target).ReadOnly = readOnly;
                    control.DropDown = tool;
                    if (!readOnly && handleText)
                        ((RichTextBox)tool.Target).TextChanged += valueHandler;
                }
                else
                {
                    tool.Target = control.GetCacheControl<PList>("PList");
                    ((PList)tool.Target).AllowCheck = false;
                    ((PList)tool.Target).GenerateColumns = false;
                    ((PList)tool.Target).GenerateToString = false;
                    ((PList)tool.Target).ListInfo.HeaderVisible = false;
                    ((PList)tool.Target).ListInfo.ColumnsVisible = false;
                    ((PList)tool.Target).ListInfo.TreeMode = true;
                    ((PList)tool.Target).ListInfo.StyleCell.BorderBrush.Color = Color.Transparent;
                    ((PList)tool.Target).ListInfo.StyleCell.Alternate = false;
                    ((PList)tool.Target).Mode = PListMode.List;
                    if (((PList)tool.Target).ListSource != _listDataSource)
                    {
                        ((PList)tool.Target).ListInfo.Columns.Clear();
                        ((PList)tool.Target).ListInfo.Columns.Add(ListProperty, 100).FillWidth = true;
                        ((PList)tool.Target).ListSource = _listDataSource;
                        if (ListAutoSort)
                            ((PList)tool.Target).OnColumnSort(ListProperty, ListSortDirection.Ascending);
                    }
                    if (value != null && value.GetType() == ((PList)tool.Target).ListType)
                        ((PList)tool.Target).SelectedItem = value;
                    if (!readOnly)
                    {
                        ((PList)tool.Target).CellDoubleClick += ListCellDoubleClick;
                        ((PList)tool.Target).KeyDown += ListCellKeyDown;
                        //((PList)tool.Target).SelectionChanged += PListSelectionChanged;
                    }
                }
            }

            SetValue(value, item);
            control.IsValueChanged = false;
        }



        private void PListSelectionChanged(object sender, EventArgs e)
        {
            SetValue(((PList)sender).SelectedItem, item);
        }

        protected void ListCellKeyDown(object sender, KeyEventArgs e)
        {
            SetValue(((PList)sender).SelectedItem, item);
            tool.Close();
        }

        protected void ListCellDoubleClick(object sender, PListHitTestEventArgs e)
        {
            SetValue(((PList)sender).SelectedItem, item);
            tool.Close();
        }

        public virtual void SetValue(object value, object item)
        {
            this.item = item;
            if (control != null)
                control.Value = ParseValue(value, item, type);
            string val = FormatValue(value) as string;
            Text = val == null ? string.Empty : val;
        }

        protected virtual void TextBoxKeyUp(object sender, KeyEventArgs e)
        {
            var box = sender as TextBoxBase;
            if (!box.Multiline && e.KeyCode == Keys.Down && tool != null && tool.Target != null)
            {
                if (tool.Visible)
                    tool.Focus();
                else if (control != null)
                {
                    ListReset();
                    control.ShowDropDown(dwf.tool.ToolShowMode.Default);
                }
            }
        }

        protected virtual void TextBoxKeyDown(object sender, KeyEventArgs e)
        {

        }

        protected virtual void TextBoxKeyPress(object sender, KeyPressEventArgs e)
        {
            if (type == typeof(int) || type == typeof(float) || type == typeof(double) || type == typeof(decimal))
            {
                if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '-')
                {
                    e.Handled = true;
                }

                // only allow one decimal point
                bool dpoint = (sender as TextBox).Text.IndexOf('.') != -1 || (sender as TextBox).Text.IndexOf(',') != -1;
                if (!dpoint && (e.KeyChar == '.' || e.KeyChar == ','))
                {
                    e.Handled = false;
                }
            }
        }

        public virtual void FreeEditControl()
        {
            if (control != null)
            {
                if (control.EditControl is TextBoxBase)
                {
                    ((TextBoxBase)control.EditControl).TextChanged -= valueHandler;
                    ((TextBoxBase)control.EditControl).KeyPress -= TextBoxKeyPress;
                    ((TextBoxBase)control.EditControl).KeyDown -= TextBoxKeyDown;
                }
                control.Image = null;
                control.DropDown = null;
                control.CurrentEditor = null;
            }
            if (tool != null)
            {
                tool.Close();
                if (tool.Target is TextBoxBase)
                    ((TextBoxBase)tool.Target).TextChanged -= valueHandler;
                else if (tool.Target is PList)
                {
                    ((PList)tool.Target).CellDoubleClick -= ListCellDoubleClick;
                    ((PList)tool.Target).KeyDown -= ListCellKeyDown;
                }
            }
            control = null;
            type = null;
            item = null;
        }

        public virtual void Dispose()
        {
            font.Dispose();
            if (_listDataSource is IDisposable)
                ((IDisposable)_listDataSource).Dispose();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Kazakstan Kazakstan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions