none
Need Help can someone told me why i am no success show my error message when my textbox word out of limit RRS feed

  • Question

  • Good Day, i am wring one program. The program purpose is when the textbox detect textbox word is out of limit it can't to continue writing and it will pop out a error message. Below is my code can someone help me take a look.

    Dim i As Integer
    txtCN.MaxLength = 15
    i = txtCN.MaxLength

    If txtCN.Text.Length > i Then
    MessageBox.Show("Maximum is 15 Character", "Error Message")
    End If

      Dim i As Integer
            txtCN.MaxLength = 15
            i = txtCN.MaxLength

            If txtCN.Text.Length > i Then
                MsgBox("Maximum is 15 Character")
            End If
    Wednesday, November 13, 2019 12:59 AM

All replies

  • Hi christing,

    I suppose we can use KeyPress event in a textbox.
    Private Sub txtCN_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCN.KeyPress
        Me.txtCN.MaxLength = 15  ' <<== this can be defined in Designer
        If (Me.txtCN.Text.Length >= 14) Then
            MessageBox.Show("Maximum is 15 Character", "Error Message")
        End If
    End Sub

    Regards,

    Ashidacchi -- http://hokusosha.com

    Wednesday, November 13, 2019 3:52 AM
  • Hi christing,

    I suppose we can use KeyPress event in a textbox.
    Private Sub txtCN_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCN.KeyPress
        Me.txtCN.MaxLength = 15  ' <<== this can be defined in Designer
        If (Me.txtCN.Text.Length >= 14) Then
            MessageBox.Show("Maximum is 15 Character", "Error Message")
        End If
    End Sub

    Regards,

    Ashidacchi -- http://hokusosha.com

    Hi christing,

    As Ashidacchi said, using the KeyPress event is a good choice.

    To achieve your requirement, you should change the statement "Me.txtCN.Text.Length >= 14" to "Me.txtCN.Text.Length >14".

    Best Regards,

    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 13, 2019 9:19 AM
  • @ Daniel_Zhang-MSFT,

    Did you execute your recommendation?
    The if condition should be "Me.txtCN.Text.Length >= 14" as my code.
    Because the input key character is not yet put in the textbox, when the KeyPress event is handled.  

    Please make a real project and check it.

    Regards,


    Ashidacchi -- http://hokusosha.com


    • Edited by Ashidacchi Wednesday, November 13, 2019 9:49 AM
    Wednesday, November 13, 2019 9:48 AM