分享
 
 
 

C#中怎么在ComboBox的下拉列表中加入树形控件?

王朝知道·作者佚名  2009-07-11
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 軟件 >> 其他軟件
 
參考答案:

using System;

using System.ComponentModel;

using System.Drawing;

using System.Windows.Forms;

namespace UserControls

{

/// <summary>

/// TreeViewComboBox 的摘要说明。

/// </summary>

public class TreeViewComboBox : UserControl

{

/// <summary>

/// 必需的设计器变量。

/// </summary>

private Container components = null;

public delegate void EventHandle(TreeNode n);

public event EventHandle AfterSelectedNode;

private bool _allowSelectParentNode = false;

public TreeViewComboBox()

{

this.InitializeComponent();

// Initializing Controls

this.pnlBack = new Panel();

this.pnlBack.BorderStyle = BorderStyle.Fixed3D;

this.pnlBack.BackColor = Color.White;

this.pnlBack.AutoScroll = false;

this.tbSelectedValue = new TextBox();

this.tbSelectedValue.BorderStyle = BorderStyle.None;

this.tbSelectedValue.ReadOnlyChanged += new EventHandler(tbSelectedValue_ReadOnlyChanged);

this.btnSelect = new ButtonEx();

this.btnSelect.Click += new EventHandler(ToggleTreeView);

this.btnSelect.FlatStyle = FlatStyle.Flat;

this.lblSizingGrip = new LabelEx();

this.lblSizingGrip.Size = new Size(9,9);

this.lblSizingGrip.BackColor = Color.Transparent;

this.lblSizingGrip.Cursor = Cursors.SizeNWSE;

this.lblSizingGrip.MouseMove += new MouseEventHandler(SizingGripMouseMove);

this.lblSizingGrip.MouseDown += new MouseEventHandler(SizingGripMouseDown);

this.tvTreeView = new TreeView();

this.tvTreeView.BorderStyle = BorderStyle.None;

this.tvTreeView.DoubleClick += new EventHandler(TreeViewNodeSelect);

this.tvTreeView.Location = new Point(0,0);

this.tvTreeView.LostFocus += new EventHandler(TreeViewLostFocus);

//this.tvTreeView.Scrollable = false;

this.frmTreeView = new Form();

this.frmTreeView.FormBorderStyle = FormBorderStyle.None;

this.frmTreeView.BringToFront();

this.frmTreeView.StartPosition = FormStartPosition.Manual;

this.frmTreeView.ShowInTaskbar = false;

this.frmTreeView.BackColor = SystemColors.Control;

this.pnlTree = new Panel();

this.pnlTree.BorderStyle = BorderStyle.FixedSingle;

this.pnlTree.BackColor = Color.White;

SetStyle(ControlStyles.DoubleBuffer,true);

SetStyle(ControlStyles.ResizeRedraw,true);

// Adding Controls to UserControl

this.pnlTree.Controls.Add(this.lblSizingGrip);

this.pnlTree.Controls.Add(this.tvTreeView);

this.frmTreeView.Controls.Add(this.pnlTree);

this.pnlBack.Controls.AddRange(new Control[]{btnSelect, tbSelectedValue});

this.Controls.Add(this.pnlBack);

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region 组件设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器

/// 修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

//

// ComboBoxTree

//

this.Name = "ComboBoxTree";

// this._absoluteChildrenSelectableOnly = true;

this.Layout += new LayoutEventHandler(this.ComboBoxTree_Layout);

}

#endregion

#region Private 属性

private Panel pnlBack;

private Panel pnlTree;

private TextBox tbSelectedValue;

private ButtonEx btnSelect;

private TreeView tvTreeView;

private LabelEx lblSizingGrip;

private Form frmTreeView;

// private string _branchSeparator;

// private bool _absoluteChildrenSelectableOnly;

private Point DragOffset;

#endregion

#region Public 属性

/// <summary>

/// 是否允许选择非叶子节点

/// </summary>

public bool AllowSelectParentNode

{

set

{

_allowSelectParentNode = value;

}

get

{

return _allowSelectParentNode;

}

}

public Color TextForeColor

{

set { this.tbSelectedValue.ForeColor = value; }

}

public Color TextBackColor

{

set { this.tbSelectedValue.BackColor = value; }

}

/// <summary>

/// 文本只读属性

/// </summary>

public bool TextReadOnly

{

set { this.tbSelectedValue.ReadOnly = value; }

}

/// <summary>

/// 树节点集合

/// </summary>

public TreeNodeCollection Nodes

{

get

{

return this.tvTreeView.Nodes;

}

}

/// <summary>

/// 选中的节点

/// </summary>

public TreeNode SelectedNode

{

set

{

this.tvTreeView.SelectedNode = value;

}

get

{

return this.tvTreeView.SelectedNode;

}

}

/// <summary>

/// ImageList

/// </summary>

public ImageList Imagelist

{

get {return this.tvTreeView.ImageList;}

set {this.tvTreeView.ImageList = value;}

}

/// <summary>

/// 显示选中的值

/// </summary>

public override string Text

{

get {return this.tbSelectedValue.Text;}

set {this.tbSelectedValue.Text = value;}

}

// /// <summary>

// /// 显示完整树节点路径时,路经的分隔符(1位字符)

// /// </summary>

// public string BranchSeparator

// {

// get {return this._branchSeparator;}

// set

// {

// if(value.Length > 0)

// this._branchSeparator = value.Substring(0,1);

// }

// }

//

// /// <summary>

// /// 是否是叶子节点

// /// </summary>

// public bool AbsoluteChildrenSelectableOnly

// {

// get {return this._absoluteChildrenSelectableOnly;}

// set {this._absoluteChildrenSelectableOnly = value;}

// }

#endregion

/// <summary>

/// 拖动树背景,改变背景大小

/// </summary>

private void RelocateGrip()

{

this.lblSizingGrip.Top = this.frmTreeView.Height - lblSizingGrip.Height - 1;

this.lblSizingGrip.Left = this.frmTreeView.Width - lblSizingGrip.Width - 1;

}

/// <summary>

/// 点击三角按钮,显示树Form

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void ToggleTreeView(object sender, EventArgs e)

{

if(!this.frmTreeView.Visible)

{

Rectangle CBRect = this.RectangleToScreen(this.ClientRectangle);

this.frmTreeView.Location = new Point(CBRect.X, CBRect.Y + this.pnlBack.Height);

this.tvTreeView.Nodes[0].Expand();

this.frmTreeView.Show();

this.frmTreeView.BringToFront();

this.RelocateGrip();

//this.tbSelectedValue.Text = "";

}

else

{

this.frmTreeView.Hide();

}

}

/// <summary>

/// 验证选中的是否是叶子节点

/// </summary>

/// <returns></returns>

// public bool ValidateText()

// {

// string ValidatorText = this.Text;

// TreeNodeCollection TNC = this.tvTreeView.Nodes;

//

// for(int i = 0; i < ValidatorText.Split(this._branchSeparator.ToCharArray()[0]).Length; i++)

// {

// bool NodeFound = false;

// string NodeToFind = ValidatorText.Split(this._branchSeparator.ToCharArray()[0])[i];

// for(int j = 0; j < TNC.Count; j++)

// {

// if(TNC[j].Text == NodeToFind)

// {

// NodeFound = true;

// TNC = TNC[j].Nodes;

// break;

// }

// }

//

// if(!NodeFound)

// return false;

// }

//

// return true;

// }

#region 事件

/// <summary>

/// 改变背景大小,在鼠标移动时发生

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void SizingGripMouseMove(object sender, MouseEventArgs e)

{

if(e.Button == MouseButtons.Left)

{

int TvWidth, TvHeight;

TvWidth = Cursor.Position.X - this.frmTreeView.Location.X;

TvWidth = TvWidth + this.DragOffset.X;

TvHeight = Cursor.Position.Y - this.frmTreeView.Location.Y;

TvHeight = TvHeight + this.DragOffset.Y;

if(TvWidth < 50)

TvWidth = 50;

if(TvHeight < 50)

TvHeight = 50;

this.frmTreeView.Size = new Size(TvWidth, TvHeight);

this.pnlTree.Size = this.frmTreeView.Size;

this.tvTreeView.Size = new Size(this.frmTreeView.Size.Width - this.lblSizingGrip.Width, this.frmTreeView.Size.Height - this.lblSizingGrip.Width);;

RelocateGrip();

}

}

/// <summary>

/// 改变背景大小,在按下鼠标时发生

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void SizingGripMouseDown(object sender, MouseEventArgs e)

{

if(e.Button == MouseButtons.Left)

{

int OffsetX = Math.Abs(Cursor.Position.X - this.frmTreeView.RectangleToScreen(this.frmTreeView.ClientRectangle).Right);

int OffsetY = Math.Abs(Cursor.Position.Y - this.frmTreeView.RectangleToScreen(this.frmTreeView.ClientRectangle).Bottom);

this.DragOffset = new Point(OffsetX, OffsetY);

}

}

/// <summary>

/// 树型控件失去焦点

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void TreeViewLostFocus(object sender, EventArgs e)

{

if(!this.btnSelect.RectangleToScreen(this.btnSelect.ClientRectangle).Contains(Cursor.Position))

this.frmTreeView.Hide();

}

/// <summary>

/// 选中树型空间节点

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void TreeViewNodeSelect(object sender, EventArgs e)

{

if(this._allowSelectParentNode && this.tvTreeView.SelectedNode != this.tvTreeView.Nodes[0])

{

this.tbSelectedValue.Text = this.tvTreeView.SelectedNode.Text;

this.ToggleTreeView(sender,null);

AfterSelectedNode(this.tvTreeView.SelectedNode);

}

else

{

if(this.tvTreeView.SelectedNode.Nodes.Count == 0)

{

this.tbSelectedValue.Text = this.tvTreeView.SelectedNode.Text;

this.ToggleTreeView(sender,null);

AfterSelectedNode(this.tvTreeView.SelectedNode);

}

}

}

private void ComboBoxTree_Layout(object sender, LayoutEventArgs e)

{

this.Height = this.tbSelectedValue.Height + 8;

this.pnlBack.Size = new Size(this.Width, this.Height - 2);

this.btnSelect.Size = new Size(16, this.Height - 6);

this.btnSelect.Location = new Point(this.Width - this.btnSelect.Width - 4, 0);

this.tbSelectedValue.Location = new Point(2, 2);

this.tbSelectedValue.Width = this.Width - this.btnSelect.Width - 4;

this.frmTreeView.Size = new Size(this.Width + 60, 250);

this.pnlTree.Size = this.frmTreeView.Size;

this.tvTreeView.Width = this.frmTreeView.Width - this.lblSizingGrip.Width;

this.tvTreeView.Height = this.frmTreeView.Height - this.lblSizingGrip.Width;

this.RelocateGrip();

}

/// <summary>

/// 文本只读事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void tbSelectedValue_ReadOnlyChanged(object sender, EventArgs e)

{

if(this.tbSelectedValue.ReadOnly)

{

this.tbSelectedValue.BackColor = Color.White;

}

}

#endregion

#region 树型背景Label

private 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);

ControlPaint.DrawSizeGrip(e.Graphics,Color.Black, 1, 0, this.Size.Width, this.Size.Height);

}

}

#endregion

#region 三角形按钮

private 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);

ControlPaint.DrawComboButton(e.Graphics, 0, 0, this.Width, this.Height, state);

}

}

#endregion

}

}

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有