Creating new items on the ToolStrip

In the project I'm working on, there's a series of modules that can be selected and each has a myriad of tools. As there's so many over all, and I'd like to leave the option open of adding modules as expansions later, I'd like to be able to create buttons for the ToolStrip within my code. How can I do this?


  • This should get you fairly close to what you are needing

    Create a class named "Class1" and insert this code

    Public WithEvents MyButton As System.Windows.Forms.ToolStripButton

    Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
    Select Case MyButton.Text
    Case "0"
    MsgBox("This is Button 1")
    Case "1"
    MsgBox("This is Button 2")
    Case "2"
    MsgBox("This is Button 3")
    End Select
    End Sub

    Public Sub New()
    MyButton = New System.Windows.Forms.ToolStripButton
    End Sub

    Then create a form (if one does not already exist), put a Tool Strip Control on it (I was using the System.Windows.FOrms.ToolStrip Control to create this code) and put this code in the form class

    [code] Public MyButtons(0 To 2) As Class1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Counter As Integer
    For Counter = 0 To 2
    MyButtons(Counter) = New Class1
    MyButtons(Counter).MyButton.Text = Counter
    MyButtons(Counter).MyButton.Visible = True

    End Sub[/code]

    Obviously it needs some modifying. I've used a 3 element array, but you would normally use the ReDim Preserve Statement, and not declare it like I've done. I've also used Button.Text in the Select STatement to work out which button is being clicked, but there should be multiple ways of doing that depending on your needs.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion