为什么创建表单实例时我的文本框的内容会消失?
问题摘要
我的表格正在侦听键盘快捷键 ctrl + h 使用keydown()
方法。
按下快捷方式时,表格将打开“关于”的“大约”形式:
if (e.Control && e.KeyCode == Keys.H)
{
About about = new About();
about.ShowDialog();
}
每当我按 ctrl + h 时,它也会有效地按下BackSpace按钮。如果我在文本框中选择文本,则将删除选择。如果我被单击到文本框中,它将删除最后一个字符。
创建最小可重复的样本
使用Visual Studio 2022创建新的Windows表单应用程序。
将文本框添加到
form1
中,然后使其成为Multiline。创建第二个名为
form2
的表单。您不需要任何东西,只是表格。将
键入
事件添加到form1
。在
form1
构造函数中,添加以下行:
KeyPreview = true;
- 在
form1_keydown()
中,添加以下代码:
if (e.Control && e.KeyCode == Keys.H) {
Form2 other = new Form2();
other.ShowDialog();
}
- 运行代码,在文本框中键入某些内容,然后键入 ctrl + h 。您将看到第二个形式弹出,并且字符从文本框中消失。关闭第二个窗口,然后在文本框中选择所有文本。再次键入 ctrl + h 。第二个表单将再次弹出,所有文本将从文本框中消失。
我正在使用Microsoft Visual Studio社区2022(64位
) - 当前版本17.1.5 带有dotnet 6.0(LTS)
。
我查看了所有代码,并进行了大量的谷歌搜索。我遵循了上面概述的步骤,并且发生了同样的问题,因此我将问题隔离到2行:
Form2 other = new Form2();
other.ShowDialog();
我添加keypreview = true;
的原因是,如果用户用户,键盘快捷方式仍然可以正常工作。单击文本框。
没有任何错误消息,只有在创建新表单时按下后台面按钮的不希望的行为。
关于这个问题,我在网上什么也没有发现。如果没有人能提出解决方案,我会将其报告给微软作为错误。
Summary of issue
I have a Form that is listening for the keyboard shortcut Ctrl+H using a KeyDown()
method.
When the shortcut is pressed, the form opens an 'about' form:
if (e.Control && e.KeyCode == Keys.H)
{
About about = new About();
about.ShowDialog();
}
Whenever I press Ctrl+H, it will effectively press the backspace button as well. If I have text selected in a textbox, it will delete the selection. If I am clicked into a textbox, it deletes the last character.
Creating a minimum reproducible sample
Create a new Windows Forms application using Visual Studio 2022.
Add a TextBox to
Form1
, and make it multiline.Create a second Form called
Form2
. You don't need anything in that, just the form.Add a
KeyDown
event toForm1
.In the
Form1
constructor, add the following line:
KeyPreview = true;
- In
Form1_KeyDown()
, add the following code:
if (e.Control && e.KeyCode == Keys.H) {
Form2 other = new Form2();
other.ShowDialog();
}
- Run the code, type something into the textbox, then type Ctrl+H. You will see the second form pop up and a character disappear from the TextBox. Close the second window, and select all of the text in the TextBox. Type Ctrl+H again. The second form will pop up again, and all of the text will disappear from the TextBox.
Other information
I'm using Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.1.5
with Dotnet 6.0 (LTS)
.
I've looked through all of my code and done a fair amount of Googling. I followed the steps outlined above, and the same problem occurs, so I've isolated the problem to 2 lines:
Form2 other = new Form2();
other.ShowDialog();
The reason I add KeyPreview = true;
is so that the keyboard shortcuts still work if the user is clicked into the TextBox.
There are no error messages of any sort, just the undesired behavior of the backspace button being pressed when creating a new form.
I have found nothing on the web regarding this problem. If nobody can come up with a solution, I will report it to Microsoft as a bug.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案是确保
form
具有keypreview
将设置为true
,并添加supresskeypress = true
到您的代码。The solution is to make sure that the
Form
hasKeyPreview
set totrue
and addSupressKeyPress = true
to your code.我刚刚找到了解释解决方案的帖子。
如何禁用C#中的TextBox钥匙板shorcuts shorcuts c#中的c#?
对于有这个问题的其他人来说,这是解释。
控制+H是Backspace的ASCII代码。要克服问题,请将文本框设置为ReadOnly,直到打开表单为止,以覆盖快捷方式。
希望这对某人有帮助!
〜remodes
I just found this post that explains the solution.
How to disable a textbox keyboard shorcuts in c#?
For others who have this problem, here is the explanation.
Control+H is the ASCII code for backspace. To overcome the problem, set the textbox to readonly until the form is opened, to override the shortcut.
Hope this helps somebody!
~REMCodes