Reference the textbox code is running from?

Ryan Lashway 61 Reputation points
2022-04-17T16:34:16.223+00:00

I have code that runs from multiple text boxes (Filter Command) off the main binding source.

How can I run the code based off the textbox it is running from, when trying sender.text or e.text neither works.

In multiple textboxes I am running code based on the keyup event but having to reference the object name in each text box.

Instead of typing:
Dim searchVal as string = textbox1.text and textbox2.text depending on where it is, i want to just use like sender.text so I can copy and paste the code or even pass it as a function.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-04-17T23:00:43.89+00:00

    Hi
    Maybe this is what you are asking:

    You set up an event handler for a Textbox. That handler will have a Handles clause indiating the control event the handler will handle. This can be extended to have several control(s) events to handle, and, to differentiate them, cast the 'sender' to the control type and then test (say) the control name.
    A simple example: Form1 has TextBox1, TextBox2, TextBox3, Button1 (text="Butt1") and Button2 (text="Butt2")

    Option Strict On
    Option Explicit On
    Public Class Form1
    
      Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
        Dim tb As TextBox = DirectCast(sender, TextBox)
    
        ' use the Name property to differentiate
        Select Case tb.Name
          Case "TextBox1"
            Stop
          Case "TextBox2"
            Stop
          Case "TextBox3"
            Stop
        End Select
      End Sub
    
      Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.MouseClick, Button2.MouseClick
        Dim butt As Button = DirectCast(sender, Button)
    
        ' this time, use the Text property
        Select Case butt.Text
          Case "Butt1"
            Stop
          Case "Butt2"
            Stop
        End Select
      End Sub
    End Class
    
    0 comments No comments

  2. Ryan Lashway 61 Reputation points
    2022-04-19T01:10:42.34+00:00

    I thought there was a way to reference the object, like Me. references, so one could do something like:

    Private sub textbox_leave(sender as Object, e as KeyEventArgs) Handles, textbox1.leave, textbox2.leave, textbox3.leave
    This.Text = UCase(This.Text)
    End Sub