csharp:DropDownComboxTreeView

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

namespace CustomControls
{

    /// <summary>
    /// 涂聚文 2014-06-30
    /// DropDownTreeView class offers TreeView that is desined to act under a drop down control
    /// You can use this control when you need to select a value from a dynamic TreeView.
    /// The sub controls of this control are set to protected. You can override the control
    /// to assign values to the subcontrol of change their behaviour.
    /// http://www.codeproject.com/Articles/3994/Custom-ComboBoxes-with-Advanced-Drop-down-Features
    /// </summary>
    public class DropDownComboxTreeView : UserControl
    {
        /// <summary>
        /// protected Button control, this is the control‘s toggle button
        /// </summary>
        protected ButtonEx btnSelect;
        /// <summary>
        /// protected TextBox control, this is control‘s value TextBox.
        /// The TextBox‘s ReadyOnly false is by default set ti true
        /// </summary>
        protected ComboBox useComboxBox;
        /// <summary>
        /// protected TreeView control, this is the control‘s TreeView.
        /// </summary>
        protected TreeView treeView;

        /// <summary>
        /// protected value that is set to true of false within the
        ///  SetTextValueToSelectedNode method.
        /// </summary>
        protected bool TextValueSet=false;

        /// <summary>
        /// the resize label on the Control‘s TreeView control
        /// </summary>
        private LabelEx treeLabel;

        /// <summary>
        /// This is the margin in pixels for resizing the TreeView
        /// </summary>
        private int Margin = 10;

        /// <summary>
        /// TreeView‘s minimum width
        /// </summary>
        private int tvMinWidth = 0;

        /// <summary>
        /// TreeView‘s minimum height
        /// </summary>
        private int tvMinHeight = 150;

        //private ComboBox.ObjectCollection itm;

        /// <summary>
        /// General point to access MouseMove event for resizing the TreeView
        /// </summary>
        private MouseEventArgs parentMouse;

       // ComboboxItem item = null;

        //private string _Text;

        //        public string ComboxText
        //        {
        //            get { return _Text; }
        //            set { _Text = value; }
        //        }
        //private string _Value;

        //        public string ComboxValue
        //        {
        //            get { return _Value; }
        //            set { _Value = value; }
        //        }

        /// <summary>
        /// Get TreeNodeCollection collection, Herewith you can add and remove items to the treeview
        /// </summary>
        [
            Description("Gets the TreeView Nodes collection"),
            Category("TreeView")
        ]
        public TreeNodeCollection Nodes
        {
            get
            {
                return treeView.Nodes;
            }
        }

        /// <summary>
        /// Get,set property that provides the TreeView‘s Selected Node
        /// </summary>
        [
            Description("Gets or sets the TreeView‘s Selected Node"),
            Category("TreeView")
        ]
        public TreeNode SelectedNode
        {
            get
            {
                return treeView.SelectedNode;
            }
            set
            {
                treeView.SelectedNode = value;
            }
        }

        /// <summary>
        /// Get, set property of boolean type, this property is mapped to the ReadOnly property of the control‘s TextBox
        /// </summary>
        [
            Description("Gets or sets the ComboBox‘s Enabled state"),
            Category("ComboBox")
        ]
        public bool ComboBoxReadOnly
        {
            set
            {
                useComboxBox.Enabled = value;
            }
            get
            {
                return useComboxBox.Enabled;
            }
        }

        //[
        //    Description("Gets or sets the ComboBox‘s text state"),
        //    Category("ComboBox")
        //]
        //public ComboboxItem Items
        //{
        //    get
        //    {
        //        return object; //txtValue.Items
        //    }
        //    set
        //    {
        //        txtValue.SelectedItem = value;
        //    }
        //}
        /// <summary>
        ///
        /// </summary>
        //public List<UserCombox>
        //{
        //   List<UserCombox> collection=new List<UserCombox>;

        //}
        //public UserCombox Items
        //{
        //    get { }
        //    set { }
        //}

        //[
        //    Description("Gets or sets the ComboBox‘s text state"),
        //    Category("ComboBox")
        //]
        public ComboBox.ObjectCollection Items
        {
           //ComboBox.ObjectCollection  items= new ComboBox.ObjectCollection(txtValue); 

            set
            {
                for (int i = 0; i < value.Count; i++)
                {
                    useComboxBox.Items.Add(value[i]);
                }
            }

            get
            {
                //if (Items.Count > 0)
                //{
                //    txtValue.Items.Add(Items);
                //}
                return this.useComboxBox.Items;
            }
        }
        [
            Description("Gets the ComboxBox DataSource"),
            Category("ComboBox")
        ]
        public Object DataSource
        {
            set
            {
                useComboxBox.DataSource = value;  //要先的关系
            }
            get
            {
                return useComboxBox.DataSource;
            }

        }
        [
            Description("Gets the ComboxBox DataSource DisplayMember"),
            Category("ComboBox")
        ]
        public string DisplayMember
        {
            set
            {
                useComboxBox.DisplayMember = value;
            }
            get
            {
                return useComboxBox.DisplayMember;
            }
        }
        [
            Description("Gets the ComboxBox DataSource ValueMember"),
            Category("ComboBox")
        ]
        public string ValueMember
        {
            set
            {
                useComboxBox.ValueMember = value;
            }
            get
            {
                return useComboxBox.ValueMember;
            }
        }

  //      public static void SetLookupBinding(ListControl toBind,
  //object dataSource, string displayMember,
  //string valueMember)
  //      {
  //          toBind.DisplayMember = displayMember;
  //          toBind.ValueMember = valueMember;

  //          // Only after the following line will the listbox
  //          // receive events due to binding.
  //          toBind.DataSource = dataSource;
  //      }

        /// <summary>
        /// Get, set property thai is mapped to the Control‘s treeView control‘s ImageList property.
        /// </summary>
        public ImageList Imagelist
        {
            get
            {
                return treeView.ImageList;
            }
            set
            {
                treeView.ImageList = value;
            }
        }
        /// <summary>
        /// Control‘s constuctor
        /// </summary>
        public DropDownComboxTreeView()
        {
            //item=new ComboboxItem();
            //item.Text = "";// CustomControls.CustComboboxItem.ComboxText;
            //item.Value = "";// CustomControls.CustComboboxItem.ComboxValue;
            // General
            TextValueSet = false;

            // Initializing Controls
            InitControls();

            // Setting The locations
            SetSizeAndLocation();

            // Adding Controls to UserControl
            this.Controls.AddRange(new Control[] { btnSelect, useComboxBox, treeView });

            // attaching events
            this.Resize += new EventHandler(ControlResize);
            this.LocationChanged += new EventHandler(ControlResize);

        }

        /// <summary>
        /// Control‘s resize handler, this handler is attached to the controls Resize and LocationChanged events
        /// </summary>
        /// <param name="sender">sender obcject</param>
        /// <param name="e">default EventArgs</param>
        private void ControlResize(object sender, EventArgs e)
        {
            // Setting The locations
            SetSizeAndLocation();
        }

        /// <summary>
        /// Controls, sub control initializer, private void that handles the control initialization.
        /// </summary>
        public void InitControls()
        {
            try
            {
                //value box settings;
                useComboxBox = new ComboBox();
                this.ComboBoxReadOnly = true;
                useComboxBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
                //初始化的值
                //if (TextValueSet == false)
                //{
                //    ComboboxItem item = new ComboboxItem();
                //    ////txtValue.Text = this.SelectedNode.Text; //设定的值
                //    item.Text = CustComboboxItem.ComboxText;
                //    item.Value = CustComboboxItem.ComboxValue;
                //if (!object.Equals(item, null))
                //{
                //    txtValue.Items.Add(item);
                //    txtValue.SelectedIndex = 0;
                //}

                //}
                //txtValue.Text = "gee";
                //txtValue.Text = "";
                //txtValue.Items.Add(SelecItem);
                if (TextValueSet == false)
                {
                    useComboxBox.DataSource = DataSource;
                    useComboxBox.DisplayMember = DisplayMember;
                    useComboxBox.ValueMember = ValueMember;
                }
                else
                {
                    useComboxBox.DataSource = null;
                }

                useComboxBox.Size = new System.Drawing.Size(121, 18);
                //select button settings
                btnSelect = new ButtonEx();
                //btnSelect.Font = new Font("System",7);
                //btnSelect.Text = "+";
                //btnSelect.TextAlign = ContentAlignment.MiddleCenter;
                //btnSelect.ClientSize = btnSelect.Size;
                btnSelect.Click += new EventHandler(ToggleTreeView);
                //btnSelect.Size = new Size(120,txtValue.Height);

                treeView = new TreeView();
                treeView.BorderStyle = BorderStyle.FixedSingle;
                treeView.Visible = false;
                treeView.AfterSelect += new TreeViewEventHandler(TreeViewNodeSelect);
                treeView.DoubleClick += new EventHandler(TreeViewNodeDoubleClickSelect); //双击为选择
                treeView.MouseMove += new MouseEventHandler(TreeViewMouseMoveEventHandler);
                treeView.Size = new Size(tvMinWidth, tvMinHeight);
                treeLabel = new LabelEx();
                treeLabel.Size = new Size(16, 16);
                treeLabel.BackColor = Color.Transparent;
                treeView.Controls.Add(treeLabel);
                SetHotSpot();

                this.SetStyle(ControlStyles.DoubleBuffer, true);
                this.SetStyle(ControlStyles.ResizeRedraw, true);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        /// <summary>
        /// private void for setting the HotSpot helper label.
        /// </summary>
        private void SetHotSpot()
        {
            treeLabel.Top = treeView.Height - treeLabel.Height;
            treeLabel.Left = treeView.Width - treeLabel.Width;
        }

        /// <summary>
        /// TreeView node select handler, this is a protected event method
        /// that is attached to the TreeView‘s AfterSelect event. It sets the
        /// TextBox‘s Text property. By default It checks the selected treenode,
        /// If the treenode dosn‘t have any child node then, this node‘s value
        /// is assigned to the TextBox‘s Text Property.
        /// You can implement your own AfterSelect handler by overriding this method.
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">default TreeViewEventArgs</param>
        protected void TreeViewNodeSelect(object sender, TreeViewEventArgs e)
        {
            // chiching for none root node
            if (this.SelectedNode.Nodes.Count == 0) //选择确定
            {
                SetTextValueToSelectedNode();
                TextValueSet = true;
            }
        }

        /// <summary>
        /// This event method is the control‘s TreeView‘s DoubleClick event handler.
        /// It is meant to close the TreeView when an item is DoubleClicked.
        /// This function calls the TreeViewNodeSelect event handler method, then if
        /// the TextValueSet is set to true it toggles (closes) the TreeView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeViewNodeDoubleClickSelect(object sender, EventArgs e)
        {
            this.TreeViewNodeSelect(sender, null);
            if (TextValueSet)
            {
                this.ToggleTreeView(sender, null);
            }
        }
        /// <summary>
        /// private void setting the control‘s TextBox‘s Text property.
        /// This method also sets the TextValueSet to true if a value
        /// is assigned to the TextBox‘s Text property
        /// </summary>
        private void SetTextValueToSelectedNode()
        {
            try
            {
                //useComboxBox.Items.Clear();
                useComboxBox.DataSource = null;
                ComboboxItem item = new ComboboxItem();
                //txtValue.Text = this.SelectedNode.Text; //设定的值
                item.Text = this.SelectedNode.Text;
                item.Value = this.SelectedNode.Name;
                useComboxBox.Items.Clear();
                useComboxBox.Items.Add(item);
                useComboxBox.SelectedIndex = 0;
                TextValueSet = true;
            }
            catch(Exception ex)
            {
                ex.Message.ToString();
                TextValueSet = false;
            }
        }

        /// <summary>
        /// private event method, this method is attached to the control‘s Button‘s Clcick event.
        /// It handles the show/hide of the control‘s treeview
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">default EventArgs</param>
        private void ToggleTreeView(object sender, EventArgs e)
        {
            if (!treeView.Visible)
            {

                SetTreeViewSizeAndLocation();
                try
                {
                    this.btnSelect.Text = "-";
                    this.Parent.Controls.Add(treeView);
                    this.treeView.BringToFront();
                    treeView.Visible = true;
                    treeView.Select();
                    Parent.MouseMove += new MouseEventHandler(ParentMouseMoveHandler);
                    if (treeView.Width < this.Width || treeView.Height < this.Height)
                    {
                        treeView.Width = tvMinWidth;
                        treeView.Height = 150;
                        SetHotSpot();
                    }

                }
                catch
                {
                }
            }
            else
            {
                try
                {
                    btnSelect.Text = "+";
                    treeView.Visible = false;
                    this.Parent.Controls.Remove(treeView);
                    Parent.MouseMove -= new MouseEventHandler(ParentMouseMoveHandler);
                }
                catch
                {
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeViewMouseMoveEventHandler(object sender, MouseEventArgs e)
        {
            this.parentMouse = e;
            SetCursor(Cursors.Default);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ParentMouseMoveHandler(object sender, MouseEventArgs e)
        {
            int tx, ty;
            this.parentMouse = e;

            tx = treeView.Left + treeView.Width;
            ty = treeView.Top + treeView.Height;

            SetHotSpot();

            if (e.Button == MouseButtons.Left)
            {
                Margin = 50;
                treeLabel.BringToFront();
            }
            else
            {
                Margin = 4;
            }

            if ((is_in_range(e.Y, ty - Margin, ty + Margin)) && is_in_range(e.X, tx - Margin, tx + Margin))
            {
                SetCursor(Cursors.SizeNWSE);
                if (e.Button == MouseButtons.Left)
                {
                    treeView.Height = e.Y - treeView.Top;
                    treeView.Width = e.X - treeView.Left;
                }
            }
            else
            {
                SetCursor(Cursors.Default);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="crs"></param>
        private void SetCursor(Cursor crs)
        {
            treeView.Cursor = crs;
            Parent.Cursor = crs;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="rvalue"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        private bool is_in_range(int rvalue, int start, int end)
        {
            if ((rvalue >= start) && (rvalue <= end))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///
        /// </summary>
        public void SetTreeViewMinimumSize()
        {
            if ((treeView.Width < this.Width) || (treeView.Height < 150))
            {
                treeView.Width = this.Width;
                treeView.Height = 150;
            }
        }

        /// <summary>
        /// private method sets the control‘s TreeView‘s size and location.
        /// It is called when the control is resized to moved from it‘s place
        /// </summary>
        private void SetTreeViewSizeAndLocation()
        {
            if (treeView != null)
            {
                treeView.Location = new Point(this.Left, this.Top + this.Height + 1);
            }
        }

        /// <summary>
        /// private method, sets the controls size and location.
        /// It sets the control‘s and sub control‘s sizes and locations
        /// </summary>
        private void SetSizeAndLocation()
        {
            tvMinWidth = this.Width;
            if (useComboxBox != null && btnSelect != null)
            {
                btnSelect.Size = new Size(useComboxBox.Height, 18);
                useComboxBox.Width = this.Width - btnSelect.Width - 4;
                useComboxBox.Location = new Point(0, 0);
                btnSelect.Left = useComboxBox.Width + 4;
                SetTreeViewSizeAndLocation();
                this.Height = useComboxBox.Height;
            }
        }
    }

    /// <summary>
    /// Extended Label control with user paint.
    /// </summary>
    public class LabelEx : Label
    {
        /// <summary>
        ///
        /// </summary>
        public LabelEx()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            System.Windows.Forms.ControlPaint.DrawSizeGrip(e.Graphics, Color.Black, 0, 0, 16, 16);
        }
    }

    /// <summary>
    ///
    /// </summary>
    public class ComboboxItem
    {

        string _Text;
        public string Text {
            get { return _Text; }
            set { _Text = value; }
		}
        object _Value;
        public object Value {
            get { return _Value; }
            set { _Value = value; }
		 }

        public override string ToString()
        {
            return Text;
        }
    }
    /// <summary>
    ///
    /// </summary>
    public  class CustComboboxItem
    {
        public static string ComboxText;
        public static string ComboxValue;
    }
    //private string _Text;

    //        public string ComboxText
    //        {
    //            get { return _Text; }
    //            set { _Text = value; }
    //        }
    //private string _Value;

    //        public string ComboxValue
    //        {
    //            get { return _Value; }
    //            set { _Value = value; }
    //        }
    /// <summary>
    /// Extended button control with userpaint
    /// </summary>
    public class ButtonEx : Button
    {
        ButtonState state;

        /// <summary>
        ///
        /// </summary>
        public ButtonEx()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            state = ButtonState.Pushed;
            base.OnMouseDown(e);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            state = ButtonState.Normal;
            base.OnMouseUp(e);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            System.Windows.Forms.ControlPaint.DrawComboButton(e.Graphics, 0, 0, this.Width, this.Height, state);
        }
    }
}

csharp:DropDownComboxTreeView

时间: 2024-11-06 03:52:18

csharp:DropDownComboxTreeView的相关文章

csharp:Google TTS API text to speech

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

Csharp: speech to text, text to speech in win

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

【转】Unity3D研究院之通过C#使用Advanced CSharp Messenger(五十)

http://www.xuanyusong.com/archives/2165 Advanced CSharp Messenger 属于C#事件的一种. 维基百科中由详细的说明http://wiki.unity3d.com/index.php?title=Advanced_CSharp_Messenger 上周的一天刚巧有朋友问到我这一块的知识,那么我研究出来将它贴在博客中,帮助了他也帮助我自己!哇咔咔. Advanced CSharp Messenger的特点可以将游戏对象做为参数发送.到底A

CSharp文件读取与写入入门图解

C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言.并定于在微软职业开发者论坛(PDC)上登台亮相.C#是微软公司研究员Anders Hejlsberg的最新成果.C#看起来与Java有着惊人的相似:它包括了诸如单一继承.接口.与Java几乎同样的语法和编译成中间代码再运行的过程.但是C#与Java有着明显的不同,它借鉴了Delphi的一个特点,与COM(组件对象模型)是直接集成的,而且它是微软公司 .NET windows网络框架的主角. 用c#来读取

项目笔记---CSharp图片处理

原文:项目笔记---CSharp图片处理 最近由于项目上需要对图片进行二值化处理,就学习了相关的图片处理上的知识,从开始的二值化的意义到动态阀值检测二值化等等,并用C#得以应用,学到了很多的知识和大家分享下我个人的经验,希望对大家有帮助. 二值化 二值化简而言之是对一副彩色图片进行0/1运算,最终显示一副黑白相间的图片,其意义多数在于对二值化处理后的图片进行分割识别,一些自动识别的验证码工具大多是先进行二值化,然后在模式识别,最终推断出验证码:我的项目中是由于硬件只支持黑色和白色,所以要对用户的

csharp:using Newtonsoft.Json.Net2.0 in .net 2.0 webform

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

What&#39;s Assembly - CSharp - Editor - first pass.dll? Best How to Fix Assembly - CSharp - Editor - first pass.dll Error Guide

If you've found yourself here, I'm guessing that you're getting Assembly - CSharp - Editor - first pass.dll error message and want to find an effective solution to fix this error. You should know for sure that you have to fix this error ASAP, otherwi

editplus配置csharp

只要是写代码的,我们肯定常有用到EditPlus..Net开发也是如此.有时我们需要调试一小段C#(或VB.Net)代码,这时去大动干戈在臃肿的VS.Net中新建“控制台应用程序”项目,写满“Console.ReadLine()”,总会有点不爽吧?这时你肯定想到要在EditPlus中配置C#运行环境了.直接在EditPlus中运行C#多舒服? 打开GOOGLE,输入“善用EditPlus构建精悍的C#编译环境”,或者“在EditPlus里配置C#的编写环境全过程”.一搜索,符合查询结果的有多少?

易语言调用csharp写的COM组件的程序在Win2008上奔溃的解决办法

易语言调用csharp写的COM组件,除了要注册csharp写的dll之外(由于是.net代码,需要用.net自带的注册工具RegAsm.exe注册,具体注册方法为: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe dotnet_lib.dll /tlb 这种调用的方法,在大部分的机器下捣鼓之后能成功,但是也有不成功的案例,具体表现如下: 一开始我以为是少了哪些dll,后来上百度,一找关键字“StackHash_0a9e”,还真能