none
[VB] Who am i? RRS feed

  • Question

  • Very basic question but i can't find it on the internet.

    How can i pass another function the name of the control that called me (in this case its a textbox named "textbox_memoryused")

        Private Sub textbox_memoryused_TextChanged(sender As Object, e As EventArgs) Handles textbox_memoryused.TextChanged
            TextboxOnlyNumbers(textbox_memoryused)
        End Sub

    so here, i triggered a textchanged event in my textbox_memoryused, and i want to pass the procedure TextboxOnlyNumbers the reference of textbox_memoryused so that I can use reuseable code.

    Im thinking of something like

    TextboxOnlyNumbers(me.the_name_of_the_textbox_that_got_me_here)

    Tuesday, June 21, 2016 11:08 PM

Answers

  • Hi RyanR,

    Try following code,

    TextboxOnlyNumbers(sender)

    Or

    TextboxOnlyNumbers(DirectCast(sender, Textbox))
    Best Regards
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 23, 2016 12:44 AM
    Moderator
  • So your function accepts TextBox itself instead of the control's name. Follow what DotNet Wang say then.
    Thursday, June 23, 2016 1:27 AM
    Answerer
  • This was my final solution:

    The calling button would use this:

       Private Sub textbox_memorytotal_TextChanged(sender As Object, e As EventArgs) Handles textbox_memorytotal.TextChanged
    
            TextboxOnlyNumbers(sender)
    
        End Sub

    The final routine:

        Public Sub TextboxOnlyNumbers(ByRef objTxtBox As TextBox)
    
            If Not IsNumeric(objTxtBox.Text) Then
    
                If objTxtBox.Text.Length > 0 Then
                    MsgBox("Numerical Values only!")
                    Try
                        objTxtBox.Text = objTxtBox.Text.Substring(0, objTxtBox.Text.Length - 1)
                        objTxtBox.Select(objTxtBox.Text.Length + 1, 1)
                    Catch ex As Exception
    
                    End Try
    
                End If
    
            End If
        End Sub



    Ryan


    Sunday, June 26, 2016 8:16 PM

All replies

  • TextboxOnlyNumbers(DirectCast(sender, Textbox).ID)


    Note: I'm assumming this is WebControl.TextBox. If it is WinFrom or WPF control, change ID to Name instead.
    Wednesday, June 22, 2016 1:41 AM
    Answerer
  • Hi RyanR,

    As @cheong00 said, you could use the sender parameter. It represent of the control which trigger this event.

    In VB.NET, we could use the property directly without cast it to TextBox type.

    TextboxOnlyNumbers(sender.Name)
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 22, 2016 2:57 AM
    Moderator
  • When I try this, i get:  {"Unable to cast object of type 'System.String' to type 'System.Windows.Forms.TextBox'."}

    The prototype i am using on the other side is:

    Public Sub TextboxOnlyNumbers(ByRef objTxtBox As TextBox)


    Ryan

    Wednesday, June 22, 2016 2:56 PM
  • Same as above:

    {"Unable to cast object of type 'System.String' to type 'System.Windows.Forms.TextBox'."}

    The prototype i am using on the other side is:

    Public Sub TextboxOnlyNumbers(ByRef objTxtBox As TextBox)


    Ryan

    Wednesday, June 22, 2016 2:57 PM
  • Hi RyanR,

    Try following code,

    TextboxOnlyNumbers(sender)

    Or

    TextboxOnlyNumbers(DirectCast(sender, Textbox))
    Best Regards
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 23, 2016 12:44 AM
    Moderator
  • So your function accepts TextBox itself instead of the control's name. Follow what DotNet Wang say then.
    Thursday, June 23, 2016 1:27 AM
    Answerer
  • No go... same error. ill figure something else out.

    Ryan

    Thursday, June 23, 2016 2:50 PM
  • Hi RyanR,

    Which version of VB and .NET Framework did you use?  Since the name of your textbox is textbox_memoryused, have you tried following code?

    TextboxOnlyNumbers(textbox_memoryused)
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 24, 2016 1:19 AM
    Moderator
  • I have, and this works.

    The whole idea though was that I could just make a macro for the controls i wanted and not need to customize the argument every time.


    Ryan

    Friday, June 24, 2016 4:17 PM
  • Hi RyanR,

    Thank you for your feedback. Have you tried following code?

    TextboxOnlyNumbers(sender)

    Please set a breakpoint on this line of code and you can see what is the value of sender and what is the type of sender.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Sunday, June 26, 2016 1:07 AM
    Moderator
  • This was my final solution:

    The calling button would use this:

       Private Sub textbox_memorytotal_TextChanged(sender As Object, e As EventArgs) Handles textbox_memorytotal.TextChanged
    
            TextboxOnlyNumbers(sender)
    
        End Sub

    The final routine:

        Public Sub TextboxOnlyNumbers(ByRef objTxtBox As TextBox)
    
            If Not IsNumeric(objTxtBox.Text) Then
    
                If objTxtBox.Text.Length > 0 Then
                    MsgBox("Numerical Values only!")
                    Try
                        objTxtBox.Text = objTxtBox.Text.Substring(0, objTxtBox.Text.Length - 1)
                        objTxtBox.Select(objTxtBox.Text.Length + 1, 1)
                    Catch ex As Exception
    
                    End Try
    
                End If
    
            End If
        End Sub



    Ryan


    Sunday, June 26, 2016 8:16 PM
  • Hi RyanR,

    Thank you for sharing your solution. Please mark your reply and helpful replies as answer.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 28, 2016 5:18 AM
    Moderator