在代码中创建时,将单击事件添加到 VB.NET 中的 ToolStripTextBox
Add click events to ToolStripTextBox in VB.NET when created in code
我正在创建标签来跟踪打开的文件。当我创建它们时,我可以设置它们的属性并将它们添加到 MenuStrip 的列表中。
在我这样做之前,我想添加一个点击事件函数。由于这是在一个函数中,并且将 运行 多次,因此我需要一种动态的方式来添加这些事件处理程序。我不能这样写:
Private Sub ToolStripTextBox1_Click(sender As Object, e As EventArgs) Handles ToolStripTextBox1.Click
' ...
End Sub
如果我这样做,我只能给他们一个名字。我希望能够添加一个点击事件,作为单独的项目分别应用于它们。
更新:
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
这是创建和个性化代码。虽然当你们建议 AddButton_Click() Handles AddButton.Click
它不起作用因为 AddButton 不是一个实际的按钮
您可以跟踪项目,使用以下代码添加添加和删除点击处理程序
Private FileList As New List(Of ToolStripItem)()
' put this code where you add an item
Dim filename = "whatever"
Dim textFile As ToolStripTextBox = New ToolStripTextBox(filename)
textFile.Text = filename
textFile.ReadOnly = True
textFile.BackColor = Color.FromArgb(61, 61, 61)
textFile.ForeColor = Color.White
add(textFile)
Private Sub clickHandler(sender As Object, e As EventArgs)
Dim item = DirectCast(sender, ToolStripItem)
MessageBox.Show(item.Text)
End Sub
Private Sub add(textFile As ToolStripItem)
FileList.Add(textFile) ' Since I don't know what FileList is, I ued List
AddHandler textFile.Click, AddressOf clickHandler
OpenProjectToolStripMenuItem.DropDownItems.Add(textFile) ' add to whatever menu item you normally do
End Sub
Private Sub remove(textFile As ToolStripItem)
FileList.Remove(textFile)
RemoveHandler textFile.Click, AddressOf clickHandler
OpenProjectToolStripMenuItem.DropDownItems.Remove(textFile)
End Sub
代码中的示例点击事件包括:
sender As Object
您可以 re-use 这一个事件处理程序,因为无论哪个菜单项被单击都会成为 sender
。所以你做这样的事情:
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
并且您可以使用 AddHandler
:
将动态工具条项目连接到此方法
AddHandler myMenuStrip.Click, AddressOf ToolSTripTextBox1_Click
我正在创建标签来跟踪打开的文件。当我创建它们时,我可以设置它们的属性并将它们添加到 MenuStrip 的列表中。
在我这样做之前,我想添加一个点击事件函数。由于这是在一个函数中,并且将 运行 多次,因此我需要一种动态的方式来添加这些事件处理程序。我不能这样写:
Private Sub ToolStripTextBox1_Click(sender As Object, e As EventArgs) Handles ToolStripTextBox1.Click
' ...
End Sub
如果我这样做,我只能给他们一个名字。我希望能够添加一个点击事件,作为单独的项目分别应用于它们。
更新:
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
这是创建和个性化代码。虽然当你们建议 AddButton_Click() Handles AddButton.Click
它不起作用因为 AddButton 不是一个实际的按钮
您可以跟踪项目,使用以下代码添加添加和删除点击处理程序
Private FileList As New List(Of ToolStripItem)()
' put this code where you add an item
Dim filename = "whatever"
Dim textFile As ToolStripTextBox = New ToolStripTextBox(filename)
textFile.Text = filename
textFile.ReadOnly = True
textFile.BackColor = Color.FromArgb(61, 61, 61)
textFile.ForeColor = Color.White
add(textFile)
Private Sub clickHandler(sender As Object, e As EventArgs)
Dim item = DirectCast(sender, ToolStripItem)
MessageBox.Show(item.Text)
End Sub
Private Sub add(textFile As ToolStripItem)
FileList.Add(textFile) ' Since I don't know what FileList is, I ued List
AddHandler textFile.Click, AddressOf clickHandler
OpenProjectToolStripMenuItem.DropDownItems.Add(textFile) ' add to whatever menu item you normally do
End Sub
Private Sub remove(textFile As ToolStripItem)
FileList.Remove(textFile)
RemoveHandler textFile.Click, AddressOf clickHandler
OpenProjectToolStripMenuItem.DropDownItems.Remove(textFile)
End Sub
代码中的示例点击事件包括:
sender As Object
您可以 re-use 这一个事件处理程序,因为无论哪个菜单项被单击都会成为 sender
。所以你做这样的事情:
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
并且您可以使用 AddHandler
:
AddHandler myMenuStrip.Click, AddressOf ToolSTripTextBox1_Click