How do I prevent MaxLength beeping or prevent application beeping altogether?

橙三吉。 提交于 2019-12-11 19:30:07

问题


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


来源:https://stackoverflow.com/questions/26581697/how-do-i-prevent-maxlength-beeping-or-prevent-application-beeping-altogether

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!