如何从一个单一的控制移除焦点?移除、焦点

2023-09-03 22:49:59 作者:你不缺我i

在我的C#的WinForms项目,我有只上一个按钮控件的窗体。 默认情况下,该按钮控件获得焦点的形式。但我不希望按钮有史以来获得焦点。

In my C# WinForms program, I have a form that has only a single Button control on it. By default, that Button control receives the focus on the form. But I don't want the Button to ever get focus.

有没有解决办法,即使是一个需要调用非托管API?

Is there a solution, even one that would require a call to an unmanaged API?

推荐答案

在表单加载事件中,你可以在一些其他控制进行对焦。

In your form Load event you can set the focus on some other control.

如果您不希望控件永远得到重点通过键盘,还可以设置其接受tab 属性设置为false。

If you do not want the control to ever get focus via keyboard, you can also set its TabStop property to false.

如果你想要的按钮不应该有重点,当您打开窗体,那么你就需要更正的TabIndex 属性。该的TabIndex 酒店整数作为值指定在其中的控件获得焦点的顺序时,设置页键pressed。如果该控件具有的TabIndex 设置为0,然后将其更改为其他值。

If you want that the button should not have focus when you open the form, then you need to correct the TabIndex property. The TabIndex property has an integer as value which specifies the order in which the controls get focus when tab key is pressed. If the control has TabIndex set to 0 then change it to some other value.

检查的TabIndex 和接受tab MSDN上的属性。

Check the documentation for TabIndex and TabStop properties on MSDN.