none
Refer to selected textbox in code

    Question

  • Wondering how to use a selected textbox that the user has clicked on in the code. eventually want to validate once i work it out.

    for example:

    If IsNumeric(textbox1.text) then

    *things*

    EndIf

    But instead of it being 'textbox1' i want it to be the textbox that is clicked on

     
    Friday, March 17, 2017 3:20 AM

Answers

  • Wondering how to use a selected textbox that the user has clicked on in the code. eventually want to validate once i work it out. 

    The textbox has a click event - if you put your code in the click event handler for the text box then it will execute whenever the user clicks on the text box.   You could create the one event handler for all the text boxes that use the same code:

        Private Sub TextBox_Click(sender As Object, e As EventArgs) Handles TextBox1.Click, TextBox2.Click
            Dim TB As TextBox = CType(sender, TextBox)
            If IsNumeric(TB.Text) Then
                *things*
            End If
        End Sub
    However that sort of code is typically used in a Validating event.  See:
    https://msdn.microsoft.com/en-us/library/ms229603%28v=vs.110%29.aspx
    Friday, March 17, 2017 4:32 AM

All replies

  • Hi RemyRooPigsGoMoo,

    I suppose you should use "TextChanged" event for the textbox, instead of "Click".

    Here's a sample code and its results.
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    	If IsNumeric(TextBox1.Text) Then
    		Me.Label1.Text = "IsNumeric"
    	Else
    		Me.Label1.Text = "Not IsNumeric"
    	End If
    End Sub
    
    Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
    	Me.Label1.Text = "TextBox1 clicked"
    End Sub




    Regards,
    Ashidacchi
    • Edited by Ashidacchi Friday, March 17, 2017 4:01 AM
    Friday, March 17, 2017 4:00 AM
  • Wondering how to use a selected textbox that the user has clicked on in the code. eventually want to validate once i work it out. 

    The textbox has a click event - if you put your code in the click event handler for the text box then it will execute whenever the user clicks on the text box.   You could create the one event handler for all the text boxes that use the same code:

        Private Sub TextBox_Click(sender As Object, e As EventArgs) Handles TextBox1.Click, TextBox2.Click
            Dim TB As TextBox = CType(sender, TextBox)
            If IsNumeric(TB.Text) Then
                *things*
            End If
        End Sub
    However that sort of code is typically used in a Validating event.  See:
    https://msdn.microsoft.com/en-us/library/ms229603%28v=vs.110%29.aspx
    Friday, March 17, 2017 4:32 AM
  •   Private Sub txt_weight_TextChanged(sender As Object, e As EventArgs) Handles txt_weight.TextChanged
            'Each part will run again when new text is entered
            txt_weight.MaxLength = 4 'Sets the max length of the textbox to 4 characters
            If (IsNumeric(txt_weight.Text) = False) Then
                txt_weight.Text = "" 'vailidates the imput so that only numeric values can be entered
                ep_weight.SetError(txt_weight, "Not Numeric values") ' Creates a little error bubble next to the textbox displaying an error message
            Else
                ep_weight.SetError(txt_weight, String.Empty) ' Removes error message
            End If
            If Val(txt_weight.Text) < 0 Then
                txt_weight.Text = "" 'vailidates the imput so that only positive numbers can be entered
                ep_weight2.SetError(txt_weight, "Input cannot be less than 0") ' Creates a little error bubble next to the textbox displaying an error message
            Else
                ep_weight2.SetError(txt_weight, String.Empty) ' Removes error message
            End If
        End Sub
    This is what i am doing now but i want to replace all the 'txt_weight' with any textbox that has just been clicked on. so when the text is changed in the selected textbox it runs validation of the selected textbox. if that makes sense 
    Friday, March 17, 2017 5:39 AM
  •     Sub vailidate()
    
    
            Dim isValid As Boolean = False
            For Each c As Control In Me.pan_metric.Controls
                If TypeOf c Is TextBox Then
    
    
                    If Not IsNumeric(CType(c, TextBox).Text.Trim()) Then
                        ep_height.SetError(c, "Required field.")
                        c = Nothing
                        isValid = False
                        Exit For
                    Else 'If (CType(c, TextBox).Text.Trim()  String.Empty) Then
                        ep_height2.SetError(c, "")
                        isValid = True
                        Continue For
                    End If
                End If
            Next
            Return
    
        End Sub
    Have managed to get it to work like this, although i want it to only effect the textbox being edited.
    Friday, March 17, 2017 5:54 AM
  • This is what i am doing now but i want to replace all the 'txt_weight' with any textbox that has just been clicked on. so when the text is changed in the selected textbox it runs validation of the selected textbox. if that makes sense 

    Follow the pattern in the code example I posted - add each textbox that you want the code to execute with to the Handles clause of the method signature, then get the textbox that raised the event into a local variable using the sender argument.

    Friday, March 17, 2017 5:55 AM
  • Thank you so much 
    Friday, March 17, 2017 6:00 AM