Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / Visual Basic

User Customizable ToolStrip with Drag and Drop

Rate me:
Please Sign up or sign in to vote.
4.74/5 (16 votes)
21 May 2010CPOL3 min read 78.3K   2K   71   24
With this .NET library, you can implement a customize toolbar function in your application.
Source

Introduction

With this library, written in VB.NET, you can add a 'Customize Toolbar' function to your application.

Background

I needed a customize toolbar function in my application, but I really couldn't find any library for .NET to implement this. So, I decided to write my own and share it to help others.

Using the Code

Add a reference to the DLL file. In the DLL, there is a class named CustomizeToolStrip. To use the library, you should make an instance of this class. Then you can set three things:

  • ToolStrip - The ToolStrip you want a customize function for
  • LanguageStrings - A Dictionary with strings used in the dialog
  • DefaultSetting - A String containing the code which is used when the user clicks the Reset button

To show the customize dialog, use ShowDialog(). This is a function which returns a String which you can save in your settings.
The second function is to reload the ToolStrip if you have saved the result of the ShowDialog() function. The ToolStrip is reloaded with the buttons/separators as the code in the String says.

Implementing the Customize Toolbar Function

If you just want to add the functionality with the basic functions, this code will do the complete job. Add it to the handler of a button, for example.

VB.NET
Dim t As UserCustomizableToolStrip.CustomizeToolStrip
t.ToolStrip = ToolStrip1
Dim chosenSetting = t.ShowDialog()

Then, you can save chosenSetting in My.Settings, for example. When loading your application, you can reload the ToolStrip again (assuming your Setting is called ToolStripSetting):

VB.NET
Dim t As UserCustomizableToolStrip.CustomizeToolStrip
t.ToolStrip = ToolStrip1
t.UpdateToolStripWithString(My.Settings.ToolStripSetting)

Adding a DefaultSetting to Enable the Reset Button

If you wish to add a Reset button, you obviously need to supply a default value in case the Reset button is clicked. This is a code as described above.

Adding a LanguageStrings to Use Another Language

In case you need to change the wording in the dialog, you can set other Strings in the LanguageStrings, which is a Dictionary. For example, a Dutch version would be:

VB.NET
Dim lang As New Dictionary_
	(Of UserCustomizableToolStrip.CustomizableToolStripLanguageStrings, String)

lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.Add, _
	"Toevoegen ->")

lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings._
	AvailableToolBarButtons, "Beschikbare items:")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.Cancel, _
	"Annuleren")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings._
	CurrentToolBatButtons, "Huidige items:")

lang.Add_
    (UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.MoveDown, "Omlaag")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.MoveUp, "Omhoog")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.OK, "OK")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.Remove, _
	"<- Verwijderen")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.Reset, _
	"Beginwaarden")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.Separator, _
	"Scheidingsteken")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.TitleBar, _
	"Werkbalk aanpassen")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.WarningText, _
	"Weet u zeker dat u de werkbalk wilt herstellen naar de beginwaarden?")
lang.Add(UserCustomizableToolStrip.CustomizableToolStripLanguageStrings.WarningTitle, _
	"Werkbalk aanpassen")

t.LanguageStrings = lang

More About the Code of the Chosen Setting

The code that the ShowDialog function returns (and which you have to supply for the Reset button) has a very simple lay-out:

|Item1|Item2|Item3|... 

Each item (Item1, ...) corresponds to either a ToolStripItem or equals the word Separator. The order of the items is the order of which the buttons and separators appear in the ToolStrip.

Points of Interest

I'm only 14 years old, and I'm happy I managed to write this code. If you use this component in your application, please let me know about it. I'd love to see my component in action.

Bugs/Notes/etc.

  • Please do not use the word 'Separator' as the name of any of your ToolStripItems
  • Not implemented - Nothing

References

History

  • 21-05-2010
    • Rewritten most of the code, enhancing the simplicity of the use
  • 06-02-2009
    • Reset button
    • .Name used instead of .Tag (you don't have to specify the .Tag properties anymore)
    • Fixed some issues with the demo
  • 02-02-2009
    • First release

License

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


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

Comments and Discussions

 
QuestionDownload but reset button is missing Pin
AACINC5-Jan-12 9:46
AACINC5-Jan-12 9:46 
AnswerRe: Download but reset button is missing Pin
pimb25-Jan-12 11:05
pimb25-Jan-12 11:05 
GeneralRe: Download but reset button is missing Pin
AACINC6-Jan-12 2:37
AACINC6-Jan-12 2:37 
GeneralMy vote of 5 Pin
Member 840416715-Nov-11 21:33
Member 840416715-Nov-11 21:33 
GeneralNice work - suggestions for improvements Pin
TobiasP15-Feb-09 9:27
TobiasP15-Feb-09 9:27 
GeneralRe: Nice work - suggestions for improvements Pin
pimb215-Feb-09 9:37
pimb215-Feb-09 9:37 
GeneralRe: Nice work - suggestions for improvements Pin
TobiasP15-Feb-09 10:39
TobiasP15-Feb-09 10:39 
"The things in VB.NET that start with the capital letter I" are interfaces. Basically, they specify what you can do with a thing rather than exactly what sort of thing it is. An array, a list and many other types of collections allow you to, e.g., enumerate the contents of them, and even though they are different types they can all still be used as arguments to a method that takes an IEnumerable as parameter. That makes using interfaces much more flexible and they should be used whenever possible.

Regarding my second point: The programmer can add multiple ToolStripSeparators but also multiple ToolStripButtons or multiples of any other class that inherits from ToolStripItem (there are seven classes inheriting from ToolStripItem supported by Visual Studio, if I'm not mistaken). Different buttons usually look different and different separators usually look the same, but never the less they are all different objects. If the contents of the ToolStrip are set using Visual Studio, all items of the ToolStrip are given unique names by default, and you should be able to replace e.g. this part of code (from UpdateToolStripWithString)
For Each spButton In splittedstringtoload
    If spButton = "Separator" Then
        Dim sep = New ToolStripSeparator
        sep.Name = "Separator"
        ToolStrip.Items.Add(sep)
    Else
        For Each AButton As ToolStripItem In Buttons
            If AButton.Name = spButton Then
                ToolStrip.Items.Add(AButton)
            End If
        Next
    End If
Next
with the shorter, faster and less error-prone code
For Each spItem In splittedstringtoload
    For Each AItem As ToolStripItem In Buttons
        If AItem.Name = spItem Then
            ToolStrip.Items.Add(AItem)
            Exit For
        End If
    Next
Next
(although it has to be changed slightly more if you do as I suggest in my third point, and using LINQ the code can probably be shorten even more).

Buttons and separators are perhaps the most common things to place in a ToolStrip, but not the only things that can be placed there, so some of the variable names you are using (e.g. Buttons) are a bit misleading. Labels, textboxes and drop-down boxes are examples of other things that can be placed in a ToolStrip, but just like buttons and separators they all inherit from ToolStripItem, so if you write code that works for ToolStripItem it should work for all the child classes as well.

Nevertheless, you have done a good job resulting in a useful class!
GeneralRe: Nice work - suggestions for improvements Pin
pimb216-Feb-09 4:29
pimb216-Feb-09 4:29 
GeneralGreat job! Pin
Shane Story6-Feb-09 4:03
Shane Story6-Feb-09 4:03 
GeneralRe: Great job! Pin
pimb27-Feb-09 0:32
pimb27-Feb-09 0:32 
GeneralNew version Pin
pimb25-Feb-09 8:59
pimb25-Feb-09 8:59 
GeneralVery good! One issue though. Pin
Anthony Daly4-Feb-09 0:49
Anthony Daly4-Feb-09 0:49 
GeneralRe: Very good! One issue though. Pin
pimb24-Feb-09 4:34
pimb24-Feb-09 4:34 
AnswerRe: Very good! One issue though. Pin
Anthony Daly4-Feb-09 6:57
Anthony Daly4-Feb-09 6:57 
GeneralNames Pin
Jouke van der Maas3-Feb-09 10:04
Jouke van der Maas3-Feb-09 10:04 
GeneralRe: Names Pin
pimb24-Feb-09 8:50
pimb24-Feb-09 8:50 
GeneralRe: Names Pin
Jouke van der Maas4-Feb-09 8:53
Jouke van der Maas4-Feb-09 8:53 
GeneralRe: Names Pin
pimb24-Feb-09 8:56
pimb24-Feb-09 8:56 
GeneralRe: Names Pin
Jouke van der Maas4-Feb-09 8:57
Jouke van der Maas4-Feb-09 8:57 
GeneralBug Pin
pimb23-Feb-09 7:46
pimb23-Feb-09 7:46 
GeneralVery Nice Work! Pin
rspercy652-Feb-09 15:12
rspercy652-Feb-09 15:12 
GeneralNice work... Pin
Paul Selormey2-Feb-09 8:43
Paul Selormey2-Feb-09 8:43 
GeneralGood! Pin
JoseMenendez2-Feb-09 8:15
JoseMenendez2-Feb-09 8:15 
GeneralRe: Good! Pin
pimb22-Feb-09 8:20
pimb22-Feb-09 8:20 

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.