0

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 ?

Mika
  • 5
  • 3
  • First things first, why is that parameter declared `ByRef`? Are you assigning a different object to that parameter inside the method? That is the only reason for it to be declared `ByRef` so, if you're not doing that, don't declare it `ByRef`. – jmcilhinney Mar 09 '23 at 14:24
  • 2
    As for the issue, the error message tells you what the problem is. A `Function` returns a value. A `Sub` does not. Are you returning a value? No you're not, so which should you be using? Which are you using? The clue is that event handlers are ALWAYS a `Sub`. I'm not convinced that there aren't still other issues but that's the resolution to this one. – jmcilhinney Mar 09 '23 at 14:28
  • 1
    I think you're misunderstanding the difference between hooking-up an event and calling an event. The AddHandler only hooks-up the event so doesn't use parameters. Use RaiseEvent to call it. You can add anything to the object parameter and cast it back, or create a custom event class with custom properties, and declare using an EventHandler. – Julian Mar 09 '23 at 14:34
  • 1
    However, In your scenario you don't need to raise the event manually as it is automatically fires after the click and the RichTextBox will be the passed in as the sender. – Julian Mar 09 '23 at 14:42
  • @Julian No, the control passed as the sender is the context menu item – Mika Mar 09 '23 at 15:43
  • @jmcilhinney You're right, I removed ByRef. But I still can't get what I want. I will try now with RaiseEvent... – Mika Mar 09 '23 at 15:45
  • @jmcilhinney thanks ! And as you told me last time, I answered the question but it tells me that I will be abble to mark it as accepted only in 2 days. I'll be back... – Mika Mar 09 '23 at 17:53

1 Answers1

0

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.

Mika
  • 5
  • 3