如何将焦点设置在Windows窗体应用程序的控制? [英] How to set focus to a control in a Windows Forms application?

查看:112
本文介绍了如何将焦点设置在Windows窗体应用程序的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows窗体应用程序,做我写的code两者同时启动应用程序时将焦点设置到控件,并随后经过我调用一个函数?

In a Windows Forms application, when do I write the code to set the focus to a control both while the application is launched and subsequently after I call a function?

举例来说,如果我有一个DropDownList,一个文本框和四个按钮,我想焦点设置为DropDownList中,其中,我写我的code?

For instance, if I have a DropDownList, a TextBox and four buttons and I want the Focus to be set to the DropDownList, where do I write my code?

将焦点设置在应用程序启动一个特定的控制,我可以设置的选项卡索引到下拉(最小值,假设tab属性设置为True下)。

To set the focus to a particular control on application launch, I can set the tab index to that DropDown (with a minimum value, under the assumption TabStop property is set to True).

现在,如果用户完成操作(比如,任何一个点击按钮事件的),然后我更新下拉之后,如果我想将焦点设置...我可以做到这一点为

Now, if the user completes an operation (say, any of the Click Button Events) and then I update the DropDown and after that if I want to set the focus...I can do it as

MyDropDownList.Focus()

注意:现在的问题是的更多的关于其中,,不是如何

NB: The question is more about where, not how?

推荐答案

目前为止最简单的解决方法是正确设置TabIndex属性,使你的'MyDropDownList'控制具有最低的指数。另一个方法则做在构造函数中。但是,你必须使用选择(),福克斯()方法还不能工作,因为控制不会成为可见直到后来。

By far the simplest solution is to set the TabIndex property correctly so that your 'MyDropDownList' control has the lowest index. The next approach is to do it in the constructor. But you have to use Select(), the Focus() method cannot work yet because the control doesn't become visible until later.

Public Sub New()
    InitializeComponent()
    MyDropDownList.Select()
End Sub

在Load事件工作为好。焦点()开始在所示事件工作。

Works in the Load event as well. Focus() starts working in the Shown event.

这篇关于如何将焦点设置在Windows窗体应用程序的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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