C# 如何从 WinForms 中的 TextBox 中删除焦点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1140250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove the focus from a TextBox in WinForms?
提问by Callum Rogers
I need to remove the focus from several TextBoxes. I tried using:
我需要从几个 TextBox 中移除焦点。我尝试使用:
textBox1.Focused = false;
Its ReadOnly
property value is true
.
其ReadOnly
属性值为true
。
I then tried setting the focus on the form, so as to remove it from all the TextBoxes, but this also fails to work:
然后我尝试将焦点设置在表单上,以便将其从所有 TextBox 中删除,但这也无法正常工作:
this.Focus();
and the function returns false
when a textbox is selected.
并且该函数false
在选择文本框时返回。
So, how do I remove the focus from a TextBox?
那么,如何从 TextBox 中移除焦点?
采纳答案by Henk Holterman
You need some other focusable control to move the focus to.
您需要一些其他可聚焦控件来移动焦点。
Note that you can set the Focus to a Label. You might want to consider where you want the [Tab] key to take it next.
请注意,您可以将焦点设置为标签。您可能需要考虑接下来要使用 [Tab] 键的位置。
Also note that you cannot set it to the Form. Container controls like Form and Panel will pass the Focus on to their first child control. Which could be the TextBox you wanted it to move away from.
另请注意,您不能将其设置为 Form。像 Form 和 Panel 这样的容器控件会将 Focus 传递给它们的第一个子控件。这可能是您希望它远离的 TextBox。
回答by Spencer Ruport
Try disabling and enabling the textbox.
尝试禁用和启用文本框。
回答by Velociraptors
Focus
sets the input focus, so setting it to the form won't work because forms don't accept input. Try setting the form's ActiveControl
property to a different control. You could also use Select
to select a specific control or SelectNextControl
to select the next control in the tab order.
Focus
设置输入焦点,因此将其设置为表单将不起作用,因为表单不接受输入。尝试将表单的ActiveControl
属性设置为不同的控件。您还可以使用Select
选择特定控件或SelectNextControl
按 Tab 键顺序选择下一个控件。
回答by kaspi
I've found a good alternative! It works best for me, without setting the focus on something else.
我找到了一个不错的选择!它最适合我,无需将重点放在其他事情上。
Try that:
试试看:
private void richTextBox_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
回答by WhySoSerious
Focusing on the label didn't work for me, doing something like label1.Focus()
right?
the textbox still has focus when loading the form, however trying Velociraptorsanswer, worked for me, setting the Form's Active control to the label like this:
专注于标签对我不起作用,做类似的事情label1.Focus()
对吗?加载表单时,文本框仍然具有焦点,但是尝试Velociraptors回答对我有用,将表单的 Active 控件设置为如下标签:
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = label1;
}
回答by Bhawk1990
It seems that I don't have to set the focus to any other elements. On a Windows Phone 7 application, I've been using the Focus method to unset the Focus of a Textbox.
似乎我不必将焦点设置为任何其他元素。在 Windows Phone 7 应用程序上,我一直在使用 Focus 方法来取消设置文本框的焦点。
Giving the following command will set the focus to nothing:
给出以下命令会将焦点设置为空:
void SearchBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Focus();
}
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
It worked for me, but I don't know why didn't it work for you :/
它对我有用,但我不知道为什么它对你不起作用:/
回答by charith rasanga
Try this one:
试试这个:
First set up tab order.
首先设置标签顺序。
Then in form load event we can send a tab key press programmatically to application. So that application will give focus to 1st contol in the tab order.
然后在表单加载事件中,我们可以以编程方式向应用程序发送 Tab 键按下。因此,该应用程序会将焦点放在 Tab 键顺序中的第一个控件上。
in form load even write this line.
在表单加载中甚至写下这一行。
SendKeys.Send("{TAB}");
This did work for me.
这对我有用。
回答by VladL
A simple solution would be to kill the focus, just create your own class:
一个简单的解决方案是杀死焦点,只需创建自己的类:
public class ViewOnlyTextBox : System.Windows.Forms.TextBox {
// constants for the message sending
const int WM_SETFOCUS = 0x0007;
const int WM_KILLFOCUS = 0x0008;
protected override void WndProc(ref Message m) {
if(m.Msg == WM_SETFOCUS) m.Msg = WM_KILLFOCUS;
base.WndProc (ref m);
}
}
回答by marcigo36
You can also set the forms activecontrol
property to null
like
您还可以将 formsactivecontrol
属性设置为null
喜欢
ActiveControl = null;
回答by Tommix
I made this on my custom control, i done this onFocus()
我在我的自定义控件上做了这个,我在 onFocus()
this.Parent.Focus();
So if texbox focused - it instantly focus textbox parent (form, or panel...) This is good option if you want to make this on custom control.
因此,如果 texbox 聚焦 - 它会立即聚焦文本框父级(表单或面板...)如果您想在自定义控件上进行此操作,这是一个不错的选择。