Creating new items on the ToolStrip - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Creating new items on the ToolStrip

FrancesOFrancesO Posts: 1Member
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?

Comments

  • Malcolm2008Malcolm2008 Posts: 20Member
    This should get you fairly close to what you are needing

    Create a class named "Class1" and insert this code

    [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
    [/code]

    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
    ToolStrip1.Items.Add(MyButtons(Counter).MyButton)
    Next




    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.