Home > Software engineering >  Add click events to ToolStripTextBox in VB.NET when created in code
Add click events to ToolStripTextBox in VB.NET when created in code

Time:05-05

I am creating tabs to keep track of open files. When I create them I can set their properties and add them to the list in a MenuStrip.

Before I do so I want to add a click event function. Since this is in a function and will be run multiple times, I need to have a dynamic way to do add these event handlers. I cannot write this:

Private Sub ToolStripTextBox1_Click(sender As Object, e As EventArgs) Handles ToolStripTextBox1.Click
    ' ...
End Sub

If I do this I can only name them all one name. I want to be able to add a click event that applies to them separately as individual items.

UPDATE:

Dim textFile As ToolStripTextBox = New ToolStripTextBox("textFile")
FileList.Items.Add(textFile)
textFile.Text = filename
textFile.ReadOnly = True
textFile.BackColor = Color.FromArgb(61, 61, 61)
textFile.ForeColor = Color.White

This is the creation and personalization code. Though when you guys suggested AddButton_Click() Handles AddButton.Click It doesn't work because AddButton isn't an actual button

CodePudding user response:

You can keep track of the items, add add and remove click handlers with the following code

Private items As New List(Of ToolStripItem)()

Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
    Dim name = Guid.NewGuid().ToString()
    add(name)
End Sub

Private Sub clickHandler(sender As Object, e As EventArgs)
    Dim item = DirectCast(sender, ToolStripItem)
    MessageBox.Show(item.Text)
End Sub

Private Sub add(name As String)
    Dim item As New ToolStripLabel(name)
    items.Add(item)
    AddHandler item.Click, AddressOf clickHandler
    OpenProjectToolStripMenuItem.DropDownItems.Add(item)
End Sub

Private Sub remove(name As String)
    Dim item = items.Single(Function(i) i.Name = name)
    items.Remove(item)
    RemoveHandler item.Click, AddressOf clickHandler
    OpenProjectToolStripMenuItem.DropDownItems.Remove(item)
End Sub

You didn't paste the code you use to add them, so I came up with my own. You will need to change some things to make it fit. The GUID is used to generate a random string, but I assume you can replace that with the file name, or whatever.

CodePudding user response:

The sample click event in your code includes this:

sender As Object

You can re-use this one event handler, because whichever menu item is clicked will be the sender. So you do something like this:

Private Sub ToolStripTextBox1_Click(sender As Object, e As EventArgs) Handles ToolStripTextBox1.Click
    Dim menuItem = DirectCast(sender,  ToolStripMenuItem)
    If menuItem Is Nothing Then Return

    'If you need to choose different code for each item:
    Select Case menuItem.name
         Case "some item"

         Case "some other item"

    End Select
End Sub

And you can connect your dynamic toolstrip items to this method using AddHandler:

AddHandler myMenuStrip.Click, AddressOf ToolSTripTextBox1_Click
  • Related