简体   繁体   English

AcceptsReturn TextBox无法正常运行

[英]AcceptsReturn on TextBox not functioning correctly

I have a simple search field on a form that is set as multiline (which I understand is the only way to change a text box's height) and has the flag AcceptsReturn set to false. 我在一个设置为多行的表单上有一个简单的搜索字段(我理解这是更改文本框高度的唯一方法)并将标志AcceptsReturn设置为false。

However, when I press enter within that control, instead of it activating the default button as it should, it puts in a return character. 但是,当我在该控件中按Enter键,而不是按原样激活默认按钮时,它会返回一个返回字符。

Now, I've also attempted using the KeyPress event to check if the Enter key has been pressed to activate the search-button click function in the hope that it would override this return behaviour - but it hasn't. 现在,我还尝试使用KeyPress事件检查是否已按下Enter键以激活搜索按钮单击功能,希望它会覆盖此返回行为 - 但事实并非如此。 Now it just runs the search AND inserts a return character. 现在它只运行搜索并插入一个返回字符。

I'm running Visual Studio 2010 (although this problem seemed to be present in 2008 too before I converted it) and C# .NET 2.0. 我正在运行Visual Studio 2010(虽然这个问题在转换之前似乎也存在于2008年)和C#.NET 2.0。 Any solutions? 有解决方案吗

I see that an answer has already been posted, which mentions the AcceptButton property, but I figure I would state more clearly why that's necessary: quoth MSDN , on AcceptsReturn, "If there is no default button for the form, the ENTER key will always create a new line of text in the control, regardless of the value of this property." 我看到已经发布了一个答案,其中提到了AcceptButton属性,但我想我会更清楚地说明为什么这是必要的:quoth MSDN ,在AcceptsReturn上,“如果表单没有默认按钮,则ENTER键将始终无论此属性的值如何,都在控件中创建一个新的文本行。“ (I just tried it out on a dummy form - by "default button", they did in fact mean the form's AcceptButton property. With one set, the value of AcceptsReturn made a difference; without one, it had no effect.) (我只是在虚拟表单上试了一下 - 通过“默认按钮”,它们实际上意味着表单的AcceptButton属性。有一组,AcceptsReturn的值有所不同;没有一个,它没有效果。)

As for KeyPress, while that is obviously not the best way in this case, I have had to use tricks like that in the past - did you remember to set e.Handled to true in the case that you handled the event yourself? 至于KeyPress,虽然这在这种情况下显然不是最好的方法,但我不得不在过去使用这样的技巧 - 你是否记得在自己处理事件的情况下将e.Handled设置为true?

The form has a property called AcceptButton . 表单有一个名为AcceptButton的属性。 Is that pointing to the button you are calling the default button ? 这是指向您正在调用默认按钮的按钮吗?

I just wrote a little test and it seems to work for me. 我刚刚写了一点测试,它似乎对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM