Click here to Skip to main content
12,452,056 members (25,992 online)
Click here to Skip to main content
Add your own
alternative version

Stats

124.2K views
3K downloads
65 bookmarked
Posted

ComboBoxTree

, 2 Feb 2004
Rate this:
Please Sign up or sign in to vote.
A treeview control that drops down much like a combobox.

Sample Image

Introduction

The ComboBoxTree control expands on Gevik Babakhani's DropDown Treeview control. Nearly 85% of the code has been rewritten. This updated control provides the following enhancements:

  • Treeview can be sized beyond the width of its parents and the form.
  • The sizing grip functionality is more consistent with standard Windows sizing grips.
  • Node selection populates the box when it is a root node, the SelectionSeparator property assigns the separator (i.e. a SelectionSeperator that is a period '.' would generate something like: Node0.subnode1.subnode2.rootnode).
  • Better design-time support including the standard treeview WYSIWYG editor.
  • The selected node box is editable and accessible as a property - however there is no validation on this value when set manually.
  • Simplified, by using regions and less method calls.

Please feel free to post comments, questions or criticism. This is my first Windows control, so feedback is appreciated.

Background

I needed a control for a project that required multiple treeviews but had very limited form space. When I found Gevik's DropDown TreeView, I was relieved to find what I was looking for but soon ran into problems that required attention and hours of code updating.

Originally, I didn't intend to redistribute the updated control, but I wanted to give back as a way of saying thanks. Thanks goes out to The Code Project and the original author - thanks Gevik Babakhani Wink | ;-)

To use the control, simply add the ComboBoxTree.dll file to your toolbox and drop the control onto your form. The control should provide most of the treeview functionality you require.

Points of Interest

I was able to get around the "sizing beyond parent" problem by adding the treeview to its own borderless form. My next problem involved making the sizing grip work correctly. I was able to get this going by calculating the offset of the mouse within the sizing grip along with the absolute mouse position and the location of the form containing the treeview. My final big problem was the sizing grip when scroll bars showed. Disabling scroll bars wasn't a solution so my only alternative was to add a margin of space on the bottom and right-hand side. A bit different but definitely functional!

History

  • 01/26/04 Version 1.0 submitted.
  • 02/02/04 Version 1.2 submitted, changes include:
    • Text property is now overridden.
    • AbsoluteChildrenSelectableOnly property option now available, users can now select end nodes only or all nodes.
    • Public ValidateText method now available to validate manually entered text.
    • SelectionSeparator now called BranchSeparater.
    • Minor toolbox category changes.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Gabe Anguiano
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
BugSize Bug Pin
Member 1145332622-Dec-15 1:55
memberMember 1145332622-Dec-15 1:55 
QuestionNo NodeSelectEventHandler event? Pin
locketine17-Dec-12 14:07
memberlocketine17-Dec-12 14:07 
AnswerRe: No NodeSelectEventHandler event? Pin
Member 1145332622-Dec-15 2:16
memberMember 1145332622-Dec-15 2:16 
QuestionCan't change size Pin
MVictorL18-Nov-12 22:09
memberMVictorL18-Nov-12 22:09 
QuestionHandling the Enter key Pin
Member 390355925-Oct-12 10:55
memberMember 390355925-Oct-12 10:55 
QuestionHow to show image of the selected tree item in combobox Pin
Rui Frazao9-Jun-10 6:07
memberRui Frazao9-Jun-10 6:07 
GeneralCheckbox With Every Node Pin
Farid_Bilal26-Feb-09 20:58
memberFarid_Bilal26-Feb-09 20:58 
QuestionHow to populate tree with data from datatable ? Pin
loverboy2312-Jul-08 3:49
memberloverboy2312-Jul-08 3:49 
QuestionHow can i get DropDownStyle like combobox? Pin
Member 137660010-Apr-08 3:07
memberMember 137660010-Apr-08 3:07 
GeneralBranchSeparator as String, DropDownHeight, ... Pin
fraveu27-Sep-07 9:53
memberfraveu27-Sep-07 9:53 
GeneralRe: BranchSeparator as String, DropDownHeight, ... Pin
fraveu27-Sep-07 9:55
memberfraveu27-Sep-07 9:55 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Resources;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Verlinea.ComboBoxTree {
public delegate void NodeSelectEventHandler();
///
/// ComboBoxTree control is a treeview that drops down much like a combobox
///

public class ComboBoxTree : UserControl {
#region Private Fields
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 int _dropDownHeight;
private System.Drawing.Point DragOffset;
#endregion
#region Public Properties
[Browsable(true), Description("Gets the TreeView Nodes collection"), Category("TreeView"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(TreeNodeCollection), typeof(TreeNodeCollection))]
public TreeNodeCollection Nodes {
get {
return this.tvTreeView.Nodes;
}
}

[Browsable(true), Description("Gets or sets the TreeView's Selected Node"), Category("TreeView")]
public TreeNode SelectedNode {
set {
this.tvTreeView.SelectedNode = value;
}
}

[Browsable(true), Description("Gets or sets the TreeView's Selected Node"), Category("TreeView")]
public ImageList Imagelist {
get { return this.tvTreeView.ImageList; }
set { this.tvTreeView.ImageList = value; }
}

[Browsable(true), Description("The text in the ComboBoxTree control"), Category("Appearance")]
public override string Text {
get { return this.tbSelectedValue.Text; }
set { this.tbSelectedValue.Text = value; }
}

[Browsable(true), Description("Gets or sets the separator for the selected node's value"), Category("Appearance")]
public string BranchSeparator {
get { return this._branchSeparator; }
set {
if (value.Length > 0)
this._branchSeparator = value/*.Substring(0,1)*/;
}
}

[Browsable(true), Description("Gets or sets the separator for the selected node's value"), Category("Behavior")]
public bool AbsoluteChildrenSelectableOnly {
get { return this._absoluteChildrenSelectableOnly; }
set { this._absoluteChildrenSelectableOnly = value; }
}

[Browsable(true), Description("The heigth, in pixels, of the treeview in a combobox."), Category("Behavior")]
public int DropDownHeight {
get { return this._dropDownHeight; }
set { this._dropDownHeight = value; }
}
#endregion

public ComboBoxTree() {
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 = System.Windows.Forms.BorderStyle.None;

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.StartPosition = FormStartPosition.Manual;
this.frmTreeView.ShowInTaskbar = false;
this.frmTreeView.BackColor = System.Drawing.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);
}

private void RelocateGrip() {
this.lblSizingGrip.Top = this.frmTreeView.Height - lblSizingGrip.Height - 1;
this.lblSizingGrip.Left = this.frmTreeView.Width - lblSizingGrip.Width - 1;
}

private void ToggleTreeView(object sender, EventArgs e) {
if (!this.frmTreeView.Visible) {
Rectangle CBRect = this.RectangleToScreen(this.ClientRectangle);
this.frmTreeView.Location = new System.Drawing.Point(CBRect.X, CBRect.Y + this.pnlBack.Height);

this.frmTreeView.Show();
this.frmTreeView.BringToFront();

this.RelocateGrip();
//this.tbSelectedValue.Text = "";
} else {
this.frmTreeView.Hide();
}
}

public bool ValidateText() {
const char splitChar = '&';
string ValidatorText = this.Text.Replace(this._branchSeparator, splitChar.ToString());
TreeNodeCollection TNC = this.tvTreeView.Nodes;

for (int i = 0; i < ValidatorText.Split(splitChar/*this._branchSeparator.ToCharArray()[0]*/).Length; i++) {
bool NodeFound = false;
string NodeToFind = ValidatorText.Split(splitChar/*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 Events
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 < this.Width)
TvWidth = this.Width;
if (TvHeight < this._dropDownHeight)
TvHeight = this._dropDownHeight;

this.frmTreeView.Size = new System.Drawing.Size(TvWidth, TvHeight);
this.pnlTree.Size = this.frmTreeView.Size;
this.tvTreeView.Size = new System.Drawing.Size(this.frmTreeView.Size.Width - this.lblSizingGrip.Width, this.frmTreeView.Size.Height - this.lblSizingGrip.Width); ;
RelocateGrip();
}
}

private void SizingGripMouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
int OffsetX = System.Math.Abs(Cursor.Position.X - this.frmTreeView.RectangleToScreen(this.frmTreeView.ClientRectangle).Right);
int OffsetY = System.Math.Abs(Cursor.Position.Y - this.frmTreeView.RectangleToScreen(this.frmTreeView.ClientRectangle).Bottom);

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

private void TreeViewLostFocus(object sender, EventArgs e) {
if (!this.btnSelect.RectangleToScreen(this.btnSelect.ClientRectangle).Contains(Cursor.Position))
this.frmTreeView.Hide();
}

private void TreeViewNodeSelect(object sender, EventArgs e) {
if (this._absoluteChildrenSelectableOnly) {
if (this.tvTreeView.SelectedNode.Nodes.Count == 0) {
tbSelectedValue.Text = this.tvTreeView.SelectedNode.FullPath.Replace(@"\", this._branchSeparator);
this.ToggleTreeView(sender, null);
}
} else {
tbSelectedValue.Text = this.tvTreeView.SelectedNode.FullPath.Replace(@"\", this._branchSeparator);
this.ToggleTreeView(sender, null);
}
}

private void InitializeComponent() {
//
// ComboBoxTree
//
this.Name = "ComboBoxTree";
this._absoluteChildrenSelectableOnly = true;
this._dropDownHeight = 106;
this._branchSeparator = "-";
this.Layout += new System.Windows.Forms.LayoutEventHandler(this.ComboBoxTree_Layout);
}

private void ComboBoxTree_Layout(object sender, System.Windows.Forms.LayoutEventArgs e) {
this.Height = this.tbSelectedValue.Height + 10;
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.tvTreeView.Height = this._dropDownHeight;
this.frmTreeView.Size = new Size(this.Width, this.tvTreeView.Height);
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();
}
#endregion

#region LabelEx
private class LabelEx : Label {
///
///
///

public LabelEx() {
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}

///
///
///

///
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
System.Windows.Forms.ControlPaint.DrawSizeGrip(e.Graphics, System.Drawing.Color.Black, 1, 0, this.Size.Width, this.Size.Height);
}
}
#endregion

#region ButtonEx
private class ButtonEx : Button {
ButtonState state;

///
///
///

public ButtonEx() {
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

}
///
///
///

///
protected override void OnMouseDown(MouseEventArgs e) {
state = ButtonState.Pushed;
base.OnMouseDown(e);
}

///
///
///

///
protected override void OnMouseUp(MouseEventArgs e) {
state = ButtonState.Normal;
base.OnMouseUp(e);
}

///
///
///

///
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
System.Windows.Forms.ControlPaint.DrawComboButton(e.Graphics, 0, 0, this.Width, this.Height, state);
}
}
#endregion
}
}

GeneralFlatStyle System Pin
jake07223-Aug-07 10:30
memberjake07223-Aug-07 10:30 
GeneralRe: FlatStyle System Pin
LAcike@sk30-Jun-08 23:40
memberLAcike@sk30-Jun-08 23:40 
GeneralAfterSelect event Pin
mayank_max104-Jun-07 6:50
membermayank_max104-Jun-07 6:50 
GeneralRe: AfterSelect event Pin
mayank_max4-Jun-07 9:07
membermayank_max4-Jun-07 9:07 
GeneralRe: AfterSelect event Pin
mayank_max4-Jun-07 9:13
membermayank_max4-Jun-07 9:13 
GeneralRe: AfterSelect event Pin
JossGP11-Oct-07 23:41
memberJossGP11-Oct-07 23:41 
QuestionHow to sort the ComboBoxTree Pin
satisht23-May-07 20:18
membersatisht23-May-07 20:18 
GeneralNeed this in VB.net [modified] - no longer needed Pin
Vortran9-Oct-06 10:51
memberVortran9-Oct-06 10:51 
GeneralBug Deteced !! Pin
H1berto Ferreira3-Jan-06 1:44
memberH1berto Ferreira3-Jan-06 1:44 
Generalform lostfocus problem Pin
TurboWang10-Mar-04 14:50
memberTurboWang10-Mar-04 14:50 
GeneralRe: form lostfocus problem Pin
Gabe Anguiano12-Mar-04 9:59
memberGabe Anguiano12-Mar-04 9:59 
GeneralExcellent ! Pin
BillWoodruff10-Feb-04 15:52
memberBillWoodruff10-Feb-04 15:52 
GeneralButton on title bar Pin
sylvain22223-Feb-04 3:05
membersylvain22223-Feb-04 3:05 
GeneralRe: Button on title bar Pin
asm433-Feb-04 7:10
memberasm433-Feb-04 7:10 
GeneralRe: Button on title bar Pin
Gabe Anguiano3-Feb-04 11:46
memberGabe Anguiano3-Feb-04 11:46 
GeneralYou can find many useful ideas in my control too :) Pin
Alex Kucherenko2-Feb-04 21:51
memberAlex Kucherenko2-Feb-04 21:51 
GeneralRe: You can find many useful ideas in my control too :) Pin
Gabe Anguiano3-Feb-04 11:40
memberGabe Anguiano3-Feb-04 11:40 
Generalnice functionality except you have to select end node Pin
Ashley van Gerven28-Jan-04 21:47
memberAshley van Gerven28-Jan-04 21:47 
GeneralRe: nice functionality except you have to select end node Pin
Gabe Anguiano (obsidience)29-Jan-04 7:23
sussGabe Anguiano (obsidience)29-Jan-04 7:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 3 Feb 2004
Article Copyright 2004 by Gabe Anguiano
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid