ComboBox和Treeview组合.docx

上传人:b****5 文档编号:5723249 上传时间:2022-12-31 格式:DOCX 页数:17 大小:25.62KB
下载 相关 举报
ComboBox和Treeview组合.docx_第1页
第1页 / 共17页
ComboBox和Treeview组合.docx_第2页
第2页 / 共17页
ComboBox和Treeview组合.docx_第3页
第3页 / 共17页
ComboBox和Treeview组合.docx_第4页
第4页 / 共17页
ComboBox和Treeview组合.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

ComboBox和Treeview组合.docx

《ComboBox和Treeview组合.docx》由会员分享,可在线阅读,更多相关《ComboBox和Treeview组合.docx(17页珍藏版)》请在冰豆网上搜索。

ComboBox和Treeview组合.docx

ComboBox和Treeview组合

自定义控件---在combobox下拉列表中添加TreeView

usingSystem;

usingSystem.ComponentModel;

usingSystem.Drawing;

usingSystem.Windows.Forms;

namespaceUserControls

{

///

///TreeViewComboBox的摘要说明。

///

publicclassTreeViewComboBox:

UserControl

{

  ///

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

  ///

  privateContainercomponents=null;

  publicdelegatevoidEventHandle(TreeNoden);

  publiceventEventHandleAfterSelectedNode;

  privatebool_allowSelectParentNode=false;

  publicTreeViewComboBox()

  {

  this.InitializeComponent();

  //InitializingControls

  this.pnlBack=newPanel();

  this.pnlBack.BorderStyle=BorderStyle.Fixed3D;

  this.pnlBack.BackColor=Color.White;

  this.pnlBack.AutoScroll=false;

  this.tbSelectedValue=newTextBox();

  this.tbSelectedValue.BorderStyle=BorderStyle.None;

  this.tbSelectedValue.ReadOnlyChanged+=newEventHandler(tbSelectedValue_ReadOnlyChanged);

  this.btnSelect=newButtonEx();

  this.btnSelect.Click+=newEventHandler(ToggleTreeView);

  this.btnSelect.FlatStyle=FlatStyle.Flat;

  this.lblSizingGrip=newLabelEx();

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

  this.lblSizingGrip.BackColor=Color.Transparent;

  this.lblSizingGrip.Cursor=Cursors.SizeNWSE;

  this.lblSizingGrip.MouseMove+=newMouseEventHandler(SizingGripMouseMove);

  this.lblSizingGrip.MouseDown+=newMouseEventHandler(SizingGripMouseDown);

  this.tvTreeView=newTreeView();

  this.tvTreeView.BorderStyle=BorderStyle.None;

  this.tvTreeView.DoubleClick+=newEventHandler(TreeViewNodeSelect);

  this.tvTreeView.Location=newPoint(0,0);

  this.tvTreeView.LostFocus+=newEventHandler(TreeViewLostFocus);

  //this.tvTreeView.Scrollable=false;

  this.frmTreeView=newForm();

  this.frmTreeView.FormBorderStyle=FormBorderStyle.None;

  this.frmTreeView.BringToFront();

  this.frmTreeView.StartPosition=FormStartPosition.Manual;

  this.frmTreeView.ShowInTaskbar=false;

  this.frmTreeView.BackColor=SystemColors.Control;

  this.pnlTree=newPanel();

  this.pnlTree.BorderStyle=BorderStyle.FixedSingle;

  this.pnlTree.BackColor=Color.White;

  SetStyle(ControlStyles.DoubleBuffer,true);

  SetStyle(ControlStyles.ResizeRedraw,true);

  //AddingControlstoUserControl

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

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

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

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

  this.Controls.Add(this.pnlBack);

  }

  ///

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

  ///

  protectedoverridevoidDispose(booldisposing)

  {

  if(disposing)

  {

    if(components!

=null)

    {

    components.Dispose();

    }

  }

  base.Dispose(disposing);

  }

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

  ///

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

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

  ///

  privatevoidInitializeComponent()

  {

  //ComboBoxTree

  this.Name="ComboBoxTree";

//  this._absoluteChildrenSelectableOnly=true;

  this.Layout+=newLayoutEventHandler(this.ComboBoxTree_Layout);

  }

  #endregion

  #regionPrivate属性

  privatePanelpnlBack;

  privatePanelpnlTree;

  privateTextBoxtbSelectedValue;

  privateButtonExbtnSelect;

  privateTreeViewtvTreeView;

  privateLabelExlblSizingGrip;

  privateFormfrmTreeView;

//  privatestring_branchSeparator;

//  privatebool_absoluteChildrenSelectableOnly;

  privatePointDragOffset;

  #endregion

  #regionPublic属性

  ///

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

  ///

  publicboolAllowSelectParentNode

  {

  set

  {

    _allowSelectParentNode=value;

  }

  get

  {

    return_allowSelectParentNode;

  }

  }

  publicColorTextForeColor

  {

  set{this.tbSelectedValue.ForeColor=value;}

  }

  publicColorTextBackColor

  {

  set{this.tbSelectedValue.BackColor=value;}

  }

  ///

  ///文本只读属性

  ///

  publicboolTextReadOnly

  {

  set{this.tbSelectedValue.ReadOnly=value;}

  }

  ///

  ///树节点集合

  ///

  publicTreeNodeCollectionNodes

  {

  get

  {

    returnthis.tvTreeView.Nodes;

  }

  }

  ///

  ///选中的节点

  ///

  publicTreeNodeSelectedNode

  {

  set

  {

    this.tvTreeView.SelectedNode=value;

  }

  get

  {

    returnthis.tvTreeView.SelectedNode;

  }

  }

  ///

  ///ImageList

  ///

  publicImageListImagelist

  {

  get{returnthis.tvTreeView.ImageList;}

  set{this.tvTreeView.ImageList=value;}

  }

  ///

  ///显示选中的值

  ///

  publicoverridestringText

  {

  get{returnthis.tbSelectedValue.Text;}

  set{this.tbSelectedValue.Text=value;}

  }

//  ///

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

//  ///

//  publicstringBranchSeparator

//  {

//  get{returnthis._branchSeparator;}

//  set

//  {

//    if(value.Length>0)

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

//  }

//  }

  ///

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

//  ///

//  publicboolAbsoluteChildrenSelectableOnly

//  {

//  get{returnthis._absoluteChildrenSelectableOnly;}

//  set{this._absoluteChildrenSelectableOnly=value;}

//  }

  #endregion

  ///

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

  ///

  privatevoidRelocateGrip()

  {

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

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

  }

  ///

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

  ///

  ///

  ///

  privatevoidToggleTreeView(objectsender,EventArgse)

  {  

  if(!

this.frmTreeView.Visible)

  {

    RectangleCBRect=this.RectangleToScreen(this.ClientRectangle);

    this.frmTreeView.Location=newPoint(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();

  }

  }

  ///

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

  ///

  ///

//  publicboolValidateText()

//  {

//  stringValidatorText=this.Text;

//  TreeNodeCollectionTNC=this.tvTreeView.Nodes;

//

//  for(inti=0;i

//  {

//    boolNodeFound=false;

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

//    for(intj=0;j

//    {

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

//    {

//      NodeFound=true;

//      TNC=TNC[j].Nodes;

//      break;

//    }

//    }

//

//    if(!

NodeFound)

//    returnfalse;

//  }

//  returntrue;

//  }

  #region事件

  ///

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

  ///

  ///

  ///

  privatevoidSizingGripMouseMove(objectsender,MouseEventArgse)

  {

  if(e.Button==MouseButtons.Left)

  {

    intTvWidth,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=newSize(TvWidth,TvHeight);

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

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

    RelocateGrip();

  }

  }

  ///

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

  ///

  ///

  ///

  privatevoidSizingGripMouseDown(objectsender,MouseEventArgse)

  {

  if(e.Button==MouseButtons.Left)

  {

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

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

    this.DragOffset=newPoint(OffsetX,OffsetY);

  }

  }

  ///

  ///树型控件失去焦点

  ///

  ///

  ///

  privatevoidTreeViewLostFocus(objectsender,EventArgse)

  {

  if(!

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

    this.frmTreeView.Hide();

  }

  ///

  ///选中树型空间节点

  ///

  ///

  ///

  privatevoidTreeViewNodeSelect(objectsender,EventArgse)

  {

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

    }

  }

  }

  privatevoidComboBoxTree_Layout(objectsender,LayoutEventArgse)

  {

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

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

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

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

  

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1