Click here to Skip to main content
15,896,606 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.6K   2K   71  
With this .NET library, you can implement a customize toolbar function in your application.
Public Class Form1
    Dim rrt(3) As ToolStripItem
    Dim rrs(4) As ToolStripItem
    Dim rru(2) As ToolStripItem
    Dim langs(100) As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i = 0 To 12 'Use the strings in the DataGridView for the dialog
            If DataGridView1.Item(1, i).Value = Nothing Then
                DataGridView1.Item(1, i).Value = String.Empty
            End If
            langs(i) = DataGridView1.Item(1, i).Value.ToString
        Next

        Dim result = UserCustomizableToolStrip.ShowCustomizeToolbarWindow(ToolStrip1, rrt, "|nieuw|openen", langs)

        TextBox1.Text = result
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        MsgBox("openen") 'Just an event so that you can see it remains when a button is moved, removed or added
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        UserCustomizableToolStrip.UpdateToolStripWithString(ToolStrip1, rrt, TextBox1.Text) 'Update the ToolStrip with the contents of the TextBox
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.ToolStripButtons = TextBox1.Text 'Save the current setting of the ToolStrip
        My.Settings.StatusStripButtons = TextBox2.Text
        My.Settings.MenuStripButtons = TextBox3.Text
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.Rows.Add(New String() {"Label1", "Available Buttons:"})
        DataGridView1.Rows.Add(New String() {"Label2", "Current Buttons:"})
        DataGridView1.Rows.Add(New String() {"Button1", "OK"})
        DataGridView1.Rows.Add(New String() {"Button2", "Cancel"})
        DataGridView1.Rows.Add(New String() {"Button3", "Reset"})
        DataGridView1.Rows.Add(New String() {"Button4", "Move Up"})
        DataGridView1.Rows.Add(New String() {"Button5", "Move Down"})
        DataGridView1.Rows.Add(New String() {"Button6", "Add ->"})
        DataGridView1.Rows.Add(New String() {"Button7", "<- Remove"})
        DataGridView1.Rows.Add(New String() {"Window Title", "Customize Toolbar"})
        DataGridView1.Rows.Add(New String() {"Translation for 'separator'", "Separator"})
        DataGridView1.Rows.Add(New String() {"Reset warning text", "Are you sure you want to reset the toolbar?"})
        DataGridView1.Rows.Add(New String() {"Reset warning title", "Warning"})

        rrt(0) = ToolStripButton1 'Update the variables containing avaiable buttons for the 3 Strips
        rrt(1) = ToolStripButton2
        rrt(2) = ToolStripButton3
        'rrt(3) = ToolStripSeparator1   'Isn't needed since separators with the default values are made by the function
        rrt(3) = ToolStripDropDownButton1

        rrs(0) = TestToolStripMenuItem2
        rrs(1) = FooToolStripMenuItem1
        rrs(2) = BarToolStripMenuItem1
        rrs(3) = ToolStripTextBox1
        rrs(4) = ToolStripComboBox2

        rru(0) = ToolStripStatusLabel1
        rru(1) = ToolStripProgressBar1
        rru(2) = ToolStripDropDownButton2


        My.Settings.Reload()
        UserCustomizableToolStrip.UpdateToolStripWithString(ToolStrip1, rrt, My.Settings.ToolStripButtons) 'Reload ToolStrip with saved setting
        UserCustomizableToolStrip.UpdateToolStripWithString(StatusStrip1, rru, My.Settings.StatusStripButtons)
        UserCustomizableToolStrip.UpdateToolStripWithString(MenuStrip1, rrs, My.Settings.MenuStripButtons)
        TextBox1.Text = My.Settings.ToolStripButtons 'Reload setting
        TextBox2.Text = My.Settings.StatusStripButtons
        TextBox3.Text = My.Settings.MenuStripButtons
    End Sub

    Private Sub CustomizeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomizeToolStripMenuItem.Click
        Button1_Click(sender, e)
    End Sub

    Private Sub CustomizeToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomizeToolStripMenuItem1.Click
        TextBox3.Text = UserCustomizableToolStrip.ShowCustomizeToolbarWindow(MenuStrip1, rrs, "|nieuw")
    End Sub

    Private Sub CustomizeToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomizeToolStripMenuItem2.Click
        TextBox2.Text = UserCustomizableToolStrip.ShowCustomizeToolbarWindow(StatusStrip1, rru, "|nieuw")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        CustomizeToolStripMenuItem1_Click(sender, e)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        CustomizeToolStripMenuItem2_Click(sender, e)
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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