开发者

Windows "ding" sound plays when using ctrl+A in a textbox (C#)

It was easy enough to create a text box that supports "ctrl+A" (select all), by listening in on the text box's KeyDown event for an "A" keypress when Control is set to True. When this condition is met, the text box does a call like this:

textBox1.Select(0, textBox1.Text.Length);
textBox1.ScrollToCaret();

The "select all" functionality works well enough, except that I hear the windows "ding"开发者_JAVA百科 sound when I actually type ctrl+A into my text box when I'm using the application. I can't figure out why.


At least on Windows XP SP3 with Windows Forms, the same happens to me (it is really annoying).

The "ding" sound is played even without any event handlers. Multiline and other settings (preview, input keys, etc.) also have no effect.

I use this event handler to get rid of it:

public static void TextBoxSelectAll(object sender, KeyEventArgs e)
{
    if (e.KeyData == (Keys.Control | Keys.A))
    {
        ((TextBox)sender).SelectAll();

        e.SuppressKeyPress = true;
        e.Handled = true;
    }
}


The ding sound indicates an error has occurred, so my guess is that this line of code is what's causing the problem:

textBox1.Select(0, textBox1.Text.Length);

Because it's a 0 count, you really want to scroll to textBox1.Text.Length - 1. (I am guessing a bit, however. And, as Daniel said, this functionality is already built in...no need to implement it!)

Edit - The problem (as described here) occurs when textboxes are in multiline mode. Follow the link to a fix for the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜