Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3615652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:24:01+00:00 2026-05-18T22:24:01+00:00

How can I refer to the control while I am inside a control’s method

  • 0

How can I refer to the control while I am inside a control’s method in VB.NET?

For example, I want in a textbox to show a message box with that textbox’s text every time the text changes. The code would be something like:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

msgbox("The text is:"+ Me.text)    

' ok the line above wont work i already know that, because "Me" refer to the form,
' not the control textbox1
' how i will refer to the textbox1's text???
' i dont want to use "textbox1.text" is there a way similar like the "Me" is for forms?
' because i want to copy-paste a code like this in a lot of controls and do not want to
' have to change in every copy the name to each control name

End Sub

I hope I made myself clear; my English needs some improvement 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:24:02+00:00Added an answer on May 18, 2026 at 10:24 pm

    No, there’s no keyword that allows you to do that. However, every event raised by a control passes in a sender parameter that you can use to determine which particular control raised that event.

    Note that this parameter is always typed as a basic Object (because it can represent any possible control), so you’ll need to downcast to a more specific control class if you need to access any of the unique members that it exposes. Since you’re handling an event raised by a TextBox control, you know that the sender must be of type TextBox, so you can simply use DirectCast to handle the upcasting. You don’t have to worry that an InvalidCastException will be thrown.

    For instance, your above example would become:

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
        Dim textBox As TextBox = DirectCast(sender, TextBox)
        MessageBox.Show("The text is: " & textBox.Text)
    End Sub
    

    That being said, there are a couple of concerning things that jump out at me in your question:

    1. Any time that your approach to solving a problem is “copy-pasting” code, you should stop, take a step back, and try to figure out if there’s any better way to achieve your ultimate goal.

      For example, if you need every textbox on your form to react in the same way whenever a particular event is raised, you should consider subclassing the existing TextBox control and consolidating all of your code in one place. Remember that you can inherit off of most of the standard controls to add custom functionality. This is often a far better solution than copying and pasting code to multiple places in your project. If you ever need to track down a bug or modify that functionality, you’ll only have to change it one place in your code, rather than several. As a somewhat cheekier benefit, you’ll be able to use Me to refer to that control when you’re editing its subclass.

    2. You should always prefer to concatenate (combine) strings using the & operator in VB.NET, rather than the + sign. Or perhaps even better, the String.Concat or String.Format methods.

    3. There is no reason to use MsgBox in VB.NET, as opposed to MessageBox.Show. No, this won’t improve performance of your application, but it’s a good practice to get into for .NET languages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

does anybody know any resources that I can refer to?
Can a LINQ enabled app run on a machine that only has the .NET
I would like to know how I can refer to a list item object
In JavaScript, the this operator can refer to different things under different scenarios. Typically
In C# language when you refer to an array element you can write: myclass.my_array['element_name']
An article has many articles that it refers to and many other articles can
Can somebody point me to a resource that explains how to go about having
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.