Brandon Amoroso
Brandon Amoroso

Reputation: 121

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" sound when I actually type ctrl+A into my text box when I'm using the application. I can't figure out why.

Upvotes: 2

Views: 3237

Answers (2)

Jaroslav Jandek
Jaroslav Jandek

Reputation: 9563

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;
    }
}

Upvotes: 3

JasCav
JasCav

Reputation: 34632

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.

Upvotes: 0

Related Questions