65.9K
CodeProject is changing. Read more.
Home

TreeView with Combo

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.92/5 (20 votes)

Feb 24, 2005

viewsIcon

82222

downloadIcon

1556

A control that makes users able to select predefined values from a ComboBox instead of editing them inside a TreeView.

Sample Image

Introduction

This control makes users able to select predefined values from a ComboBox instead of editing them inside a TreeView.

Background

The reader is expected to have basic Object Oriented concepts and awareness in C# coding.

Using the code

Just add the control to your form in design mode or in run time. There is a property in the control called "combobox". It exposes the ComboBox that will appear when editing in the tree. Just fill the data source of the ComboBox. Do not forget to enable editing in the TreeView!!

The sample demo has all of this in brief.

All I did in the control was to inherit from the class "TreeView" of the .NET Framework and override the OnBeforeLabelEdit method of the class to show the ComboBox.

//
// Show combobox before editing in treeview //
protected override void OnBeforeLabelEdit (NodeLabelEditEventArgs e)
{ 
  current_tree_node = e.Node;
  tcombo.Bounds = e.Node.Bounds; tcombo.Show();
}

Then I handled the event SelectedIndexChanged of the ComboBox to set the TreeView label text with the selected text from the ComboBox.

//
// Show combobox text in the treeview label text
//
private void tcombo_SelectedIndexChanged(object sender, EventArgs e)
{
  current_tree_node.Text = tcombo.Text;
  tcombo.Hide();
}

History

Version 1.0: 21/2/2005 (Posted).