Click here to Skip to main content
15,894,180 members
Articles / Desktop Programming / Win32
Tip/Trick

A Visual Studio 2010 style ToolBox control

Rate me:
Please Sign up or sign in to vote.
4.78/5 (8 votes)
26 Nov 2012CPOL1 min read 47.4K   5.5K   25   8
VS 2010 style toolbox written in C#

Introduction  


This article aims to show the use of ToolBox control which can be useful for projects like IDE development and other design surface projects.  

Background 

The design cue has been borrowed from Visual Studio 2010 and have made all possible efforts to keep it simple and usable.  

Using the code  

Download the attached assembly and add a reference in your project to pplStuff.Controls.ToolBox.dll.

Create an instance of the ToolBox 

C#
private void Form1_Load(object sender, EventArgs e)
{
    ToolBox toolBox = new ToolBox();
} 

Add Groups

You may  create individual groups and add it to the toolbox 

C#
ToolGroup grpCommon = new ToolGroup();
grpCommon.Caption = "Common Control";
grpCommon.Collapsed = false;
toolBox.ToolGroups.Add(grpCommon);  

Or create them using constructor

C#
toolBox.ToolGroups.Add(new ToolGroup("Common Control", false)); 

Add Items to Groups

C#
toolBox.ToolItems.Add(new ToolItem(toolBox.ToolGroups[0], "Pointer", Image.FromFile(@"C:\Images\Pointer.jpg"), new object()));

The first parameter of the ToolItem constructor takes a ToolGroup object which must be already added to the control. Second parameter defines the text displayed on the item. Next is the image to be used when creating the toolitem.

The last parameter is an user defined object which can be accessed during runtime. This has been added to cater to requirements when associating an object to an item becomes necessary.  

Events 

The toolbox provides one event for Selection Change of items and provides access to the currently selected ToolItem.

C#
toolBox.SelectionChanged += new EventHandler(toolBox1_SelectionChanged);
C#
void toolBox_SelectionChanged(object sender, EventArgs e)
{
    ToolItem item = sender as ToolItem;
    MessageBox.Show(item.Text);
} 

Clearing item selection

ClearItemSections() method can be used to clear current selection of item in the toolbox.

C#
toolBox.ClearItemSections(); 

Removing Groups/Items from the ToolBox    

Groups or items can be removed from the toolbox by simply removing them from the collections

To remove the first group  

C#
toolBox.ToolGroups.Remove(toolBox.ToolGroups[0]); 

 To remove the 5th item from the toolbox (Item indexes are independent of the group they are in)

C#
toolBox.ToolItems.Remove(toolBox.ToolItems[4]); 

Drag & Drop 

The toolbox supports DragDrop operation and can be implemented as follows

  • I have created a panel and placed a label inside the panel to show the text of item being dropped. 
C#
private void panel1_DragEnter(object sender, DragEventArgs e)
{
    if (e.AllowedEffect == DragDropEffects.Copy)
       e.Effect = DragDropEffects.Copy;
}  
C#
private void panel1_DragDrop(object sender, DragEventArgs e)
{
    ToolItem item = e.Data.GetData(typeof(ToolItem)) as ToolItem;
    lblDemo.Text = item.Text;
}   

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionControl After Drop Pin
Member 1105648317-May-16 2:11
Member 1105648317-May-16 2:11 
QuestionpplStuff.Controls.ToolBox.dll Pin
abdulaziz M.9-Apr-14 22:13
abdulaziz M.9-Apr-14 22:13 
QuestionMy vote of 2 Pin
ElektroStudios31-Dec-13 22:05
ElektroStudios31-Dec-13 22:05 
SuggestionMissing core functionality - doubleclick on ToolItem Pin
devslo19-Nov-13 23:07
devslo19-Nov-13 23:07 
AnswerRe: Missing core functionality - doubleclick on ToolItem Pin
devslo20-Nov-13 21:02
devslo20-Nov-13 21:02 
QuestionSource for control missing Pin
Waseem Anis8-May-13 22:27
Waseem Anis8-May-13 22:27 
GeneralMy vote of 5 Pin
Flash2009-MX21-Mar-13 10:36
Flash2009-MX21-Mar-13 10:36 
QuestionFull Size image Pin
LordD4rkHelmet22-Jan-13 18:42
LordD4rkHelmet22-Jan-13 18:42 

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.