当MultiLine属性为True时,全选快捷方式将失败 [英] Select All shortcut fails when MultiLine property is True

查看:127
本文介绍了当MultiLine属性为True时,全选快捷方式将失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序,上面有一个标准的TextBox.表单上没有事件被处理,也没有菜单.当我按下 Ctrl + A 快捷方式以选择所有文本时,我得到一个叮当声,但未选择任何内容.

I have a Windows forms application that has a standard TextBox on it. There are no events being handled and no menus on the form. When I press the Ctrl+A shortcut to select all the text, I get a ding, and nothing is selected.

为确认我没有无意编写某些代码,我创建了一个新的Windows窗体应用程序,该窗体上只有一个文本框.我用C#和VB.Net都尝试过,两者都一样.我已经在Windows 7上运行的Visual Studio 2012 Update 1和Windows XP上运行的Visual Studio 2008中进行了尝试,它在每个实例中的行为都相同.

To confirm I didn't inadvertently code something I created a new Windows forms application with only a textbox on the form. I tried it with both C# and VB.Net and it is the same in both. I have tried this in Visual Studio 2012 Update 1 running on Windows 7, and Visual Studio 2008 running on Windows XP and it behaves the same in each instance.

我可以很容易地在KeyDown事件中捕获按键组合,但是即使在设置e.Cancel = true之后,机器仍然会发出叮"的声音.

I can catch the key stroke combination in the KeyDown event easily enough, but even after setting e.Cancel = true the machine stills sounds the "ding".

是否有一种抑制声音的方法,或者甚至更好的一种方法,可以使文本框接受快捷方式,就像非多行文本框一样?

Is there a way to suppress the sound, or even better, a way to make the textbox accept the shortcut the same way a non-multiline textbox does?

推荐答案

这对许多程序员来说都是一个惊喜,但是当它处于多行模式时,本机Windows编辑控件实际上并未将Ctrl + A作为快捷方式实现.它必须由使用它的程序来实现.例如,您可以在记事本中看到此内容,该程序使用多行编辑控件.使用文件+打开+文件,选择c:\ windows \ notepad.exe,打开Accelerator节点,然后双击其中一张表.

This is a surprise to many programmers but the native Windows edit control doesn't actually implement Ctrl+A as a shortcut when it is in multi-line mode. It has to be implemented by the program that uses it. You can see this for example in Notepad, a program that uses a multi-line edit control. Use File + Open + File, select c:\windows\notepad.exe, open the Accelerator node and double-click one of the tables.

实施起来并不困难:

    private void textBox1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyData == (Keys.Control | Keys.A)) {
            textBox1.SelectAll();
            e.Handled = e.SuppressKeyPress = true;
        }
    }


更新:在.NET 4.6.1中进行了更改,System.Windows.Forms.TextBox现在也为多行文本框实现了Ctrl + A.


UPDATE: changed in .NET 4.6.1, System.Windows.Forms.TextBox now implements Ctrl+A for multi-line textboxes as well.

这篇关于当MultiLine属性为True时,全选快捷方式将失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆