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的更多相关文章

  1. c#操作MangoDB 之MangoDB CSharp Driver驱动详解

    序言 MangoDB CSharp Driver是c#操作mongodb的官方驱动. 官方Api文档:http://api.mongodb.org/csharp/2.2/html/R_Project_ ...

  2. c#进阶之神奇的CSharp

    CSharp 简写为c#,是一门非常年轻而又有活力的语言. CSharp的诞生      在2000年6月微软发布了c#这门新的语言.作为微软公司.NET 平台的主角,c#吸收了在他之前诞生的语言(c ...

  3. WindowsCE project missing Microsoft.CompactFramework.CSharp.targets in Visual Studio 2008

    00x0 前言 之前在Windows 7系统中开发的WindowsCE项目,最近换成Windows 10系统,需要将项目进行修改,打开项目后提示如下错误: 无法读取项目文件"App.cspr ...

  4. csharp: Oracle Stored Procedure DAL using ODP.NET

    paging : http://www.codeproject.com/Articles/44858/Custom-Paging-GridView-in-ASP-NET-Oracle https:// ...

  5. Excel转Json,Json转CSharp

    一份给策划最好的礼物!就是:Excel2Json2CSharp 策划配置Excel,动不动就要改数值啊,增加字段啊. 程序这边对应的解析类就得改动啊.整一个麻烦了得! 所以我就整理了这个Excel2J ...

  6. Microsoft.CompactFramework.CSharp.targets not found

    今天打开VS2008的智能设备项目,报以下错误,应该是文件找不到了. The imported project "C:\WINDOWS\Microsoft.NET\Framework\v3. ...

  7. CSharp 相关知识点小结

    1.JS获取iframe下面的内容document.getElementById('IFRAME1').contentDocument; 2.dialog 弹出层,定位:postion:'bottom ...

  8. 自己动手制作CSharp编译器

    在你喜欢的位置(如F盘根目录)新建一个文件夹,并命名为“CSharp开发环境”.找到或下载C#编译器组件(csc.exe和cscui.exe),并放在先前建立的文件夹中.该组件的一般位置在C盘的.NE ...

  9. Storm系列(二):使用Csharp创建你的第一个Storm拓扑(wordcount)

    WordCount在大数据领域就像学习一门语言时的hello world,得益于Storm的开源以及Storm.Net.Adapter,现在我们也可以像Java或Python一样,使用Csharp创建 ...

随机推荐

  1. P4168 [Violet]蒲公英 区间众数

    $ \color{#0066ff}{ 题目描述 }$ 在乡下的小路旁种着许多蒲公英,而我们的问题正是与这些蒲公英有关. 为了简化起见,我们把所有的蒲公英看成一个长度为n的序列 \((a_1,a_2.. ...

  2. [开源JVM] yvm - 自制Java虚拟机

    中文 | English | | | YVM是用C++写的一个Java虚拟机,现在支持Java大部分功能,以及一个基于标记清除算法的并发垃圾回收器. 不过还有很多bug等待修复. 感兴趣的朋友pull ...

  3. leetcode-665-Non-decreasing Array

    题目描述: Given an array with n integers, your task is to check if it could become non-decreasing by mod ...

  4. 2016级算法第一次练习赛-E.AlvinZH的儿时回忆——蛙声一片

    864 AlvinZH的儿时回忆----蛙声一片 题目链接:https://buaacoding.cn/problem/865/index 思路 中等题.难点在于理解题意!仔细读题才能弄懂题目规则.整 ...

  5. Schema Workbench 启动慢

    原始是JDBC连接设定的时候需要在参数中增加下列选项 FILTER_SCHEMA_LIST 官方的解释是 就是去搜寻连接数据库的所有的表结构,表越大越慢. 也要把这选项去除,保存数据库链接,并重新登录 ...

  6. 863公司 linux软测题

    1.浏览目录命令 2.浏览文件命令 3.目录操作命令 4.文件操作命令 5.进程管理命令

  7. python全栈开发_day10_函数的实参和形参

    一:函数的实参和形参 实参是在调用函数时()出现的外界的实际的值 形参不能再函数外部直接使用 1)实参的两种形式 实参是调用函数时()中传入的参数 1.位置实参 def a(a): print(a) ...

  8. Jenkins windows部署

    1.安装jenkins 进入https://jenkins.io/download/,下载windows安装包,解压后运行jenkins.msi进行安装. 配置jenkins (1)打开http:// ...

  9. adb的常用命令及如何查看被占用的端口

    adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...

  10. docker 容器 centos + tomcat + jdk

    环境: 阿里云ecs服务器 步骤: 1.安装docker 2.获取centos镜像 3.下载tomcat 和 jdk 安装包 4.配置 1.安装docker https://www.cnblogs.c ...