How do I prevent MaxLength beeping or prevent appl

2019-09-01 18:30发布

问题:

My problem:

When my text box's MaxLength is reached, a beep sound is produced. I want to prevent this beeping and would even go so far as preventing all instances of beeping for my application if achievable.

I'm already familiar with how to mimic MaxLength using Substring and resetting the caret but in this particular instance, substituting MaxLength is not an option.

To reproduce:

  1. In Visual Studio, in design mode, drag a text box onto a fresh form.
  2. Use the following as is:

Code:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.MaxLength = 5
    End Sub
    Private Sub TextBox1_keypress(keyascii As Integer)
        If len(TextBox1.text) = 5 Then
            keyascii = 0
        End If
    End Sub
End Class

The above is an adaptation of examples I've come across online but has no effect.

回答1:

Test this:

Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Not e.KeyChar.Equals(ControlChars.Back) Then
        If Me.TextBox1.TextLength = Me.TextBox1.MaxLength Then
            e.Handled = True
        End If
    End If
End Sub