Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to insert text when Texbox.MaxLength is set
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00815672
Message ID:
00815684
Views:
8
>In VB.Net textboxes where I have set the MaxLength property I cannot insert or overwrite characters when the maximum length is reached. In VFP and other programs,I am used to the last letters scrolling off in insert mode, or the text being overwritten by the new keystrokes. The way it is I am forced to delete characters before I can change any others.
>
>Does anyone know how to change this behaviour? It is unacceptable.

Here you go. If you want fox style set the bolFoxStyle var = true I like truncating the leading char but you can have it both ways. The fox style is little more work but, se la vi. Boy, on the UT you always feel like you screwed up any frenchism. ;)

I used Sender inside the functions so you can use one function for any textboxes you want to have this behavior.
HTH,
    Dim bolFoxStyle As Boolean = False

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        Dim strCur As String = sender.text.trim
        Dim ilenCur As Integer = strCur.Length
        Dim strNew As String = ""

        If bolFoxStyle Then

            If e.KeyChar.IsLetterOrDigit(e.KeyChar) Then

                If ilenCur >= sender.maxlength Then
                    strNew = strCur.Substring(0, (ilenCur - 1)) & e.KeyChar
                    sender.text = strNew
                    ' sender.Select((sender.maxlength - 1), 1) ' This doesn't work here add to _textchanged
                End If

            End If

        Else

            If e.KeyChar.IsLetterOrDigit(e.KeyChar) Then

                If ilenCur >= sender.maxlength Then
                    strNew = strCur.Substring(1, (ilenCur - 1)) & e.KeyChar
                    sender.text = strNew
                    TextBox1.SelectionStart = sender.maxlength ' Move to the end of the line for UI clarity
                End If

            End If

        End If

    End Sub

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

        If bolFoxStyle Then
            Dim strCur As String = sender.text.trim
            Dim ilenCur As Integer = strCur.Length

            If ilenCur >= sender.maxlength Then
                sender.Select((sender.maxlength - 1), 1) ' This works here 
            End If
        End If
    End Sub
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform