英文:
How can I pass a parameter in an AddressOf?
问题
AddHandler item.Click, Function(sender, e) RTB_MenuItem_Click(RTB)
英文:
I have RichTextBox1 and RichTextBox2 which call a function when user clicks the mouse
Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
RTBCheckText(RichTextBox1, e)
End Sub
Private Sub RichTextBox2_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox2.MouseDown
RTBCheckText(RichTextBox2, e)
End Sub
In the function RTBCheckText I have to create a context menu in the corresponding richtextbox
Private Sub RTBCheckText(ByRef RTB As RichTextBox, e As MouseEventArgs)
Dim text As String = RTB.Text
Do something...
Dim item As New ToolStripMenuItem("item1")
AddHandler item.Click, AddressOf RTB_MenuItem_Click
menu.Items.Add(item)
Do something...
menu.Show(RTB, e.Location)
End Sub
And in the function RTB_MenuItem_Click I need the previously passed parameter RTB in order to replace some text
Private Sub RTB_MenuItem_Click(sender As Object, e As EventArgs)
Do something with RTB
End Sub
To do that, I tried to replace
AddHandler item.Click, AddressOf RTB_MenuItem_Click
with
AddHandler item.Click, Function(sender, e) RTB_MenuItem_Click(RTB)
Private Sub RTB_MenuItem_Click(ByVal RTB As RichTextBox)
Dim menuItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim Text As String = RTB.SelectedText
Do something with RTB...
End Sub
but it does not work. It tells me Expression does not produce a value
. How can I solve this ?
答案1
得分: 0
根据jmcilhinney的建议,必须将Function替换为Sub。
这段代码很管用:
Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
RTBCheckText(RichTextBox1, e)
End Sub
Private Sub RichTextBox2_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox2.MouseDown
RTBCheckText(RichTextBox2, e)
End Sub
Private Sub RTBCheckText(RTB As RichTextBox, e As MouseEventArgs)
Dim item As New ToolStripMenuItem("item1")
AddHandler item.Click, Sub() RTB_MenuItem_Click(item, RTB)
menu.Items.Add(item)
'做一些事情...
menu.Show(RTB, e.Location)
End Sub
Private Sub RTB_MenuItem_Click(sender As Object, ByVal RTB As RichTextBox)
Dim menuItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim Text As String = RTB.SelectedText
'对RTB做一些事情...
End Sub
英文:
As jmcilhinney suggested, Function had to be replaced with Sub.
This code did the trick :
Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
RTBCheckText(RichTextBox1, e)
End Sub
Private Sub RichTextBox2_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox2.MouseDown
RTBCheckText(RichTextBox2, e)
End Sub
Private Sub RTBCheckText(RTB As RichTextBox, e As MouseEventArgs)
Dim item As New ToolStripMenuItem("item1")
AddHandler item.Click, Sub() RTB_MenuItem_Click(item, RTB)
menu.Items.Add(item)
Do something...
menu.Show(RTB, e.Location)
End Sub
Private Sub RTB_MenuItem_Click(sender As Object, ByVal RTB As RichTextBox)
Dim menuItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim Text As String = RTB.SelectedText
Do something with RTB...
End Sub
It works fine ! In each RichTextBox, I can display a context menu using the same code for all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论