怎么在 AddressOf 中传递参数?

huangapple go评论92阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月9日 22:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定