将焦点设置在Windows窗体的实例上 [英] Setting Focus on an instance of windows form
问题描述
我有一个C#程序。
当你按下按钮时,我希望它加载另一个具有不同tab的窗体。我希望它从不同的标签开始,当我按下不同的按钮。
例如:
我按下'''student''按钮,我希望它打开窗体表格并选择学生标签。
,当我按下'''老师''按钮时,我希望它打开相同的窗体,并选择教师标签。
我用这个代码来打开窗体:
I have a C# program.
when you push a button,I want it to load another windows form that has different tabs.I want it to start from different tabs,when I push different buttons.
for example:
I push the: ''student'' button,I expect it to open the windows form with student tab selected.
and when I push the: ''teacher'' button, I expect it to open the same windows form with teacher tab selected.
I use this code to open windows form:
Form frm = new Form2();
frm.ShowDialog() // or even Show(). but neither worked :(
我使用了select()和focus()方法。但他们没有''工作:(
我也使用这种方法:
I have used select(),and focus() methods.but they didn''t work :(
and also I used this approach:
ActiveControl = frm.Controls["tbTabs"].Controls["studentTab"];
但它也没有工作:(
but it didn''t work either :(
推荐答案
您需要使用myForm.Activate()
: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activate。 aspx [ ^ ]。
一些背景:焦点只不过是键盘焦点。只有一个焦点窗口e系统一次。激活是不同的:它仅应用于顶级窗口(如表单;默认情况下它们都是顶级),显示活动窗口标题等,但它也会影响将输入事件分派给应用程序。 />
当一个窗口有一个带键盘焦点的控件,然后取消激活时,该控件总是松散焦点。当你再次激活一个表单时,上次有焦点的控件将再次获得它。
-SA
You need to usemyForm.Activate()
: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activate.aspx[^].
Some background: focus means nothing but "keyboard focus". There is only one focused window in the whole system at a time. Activation is something different: it is applied only to top-level windows (like forms; they are all top-level by default), shows active window title, etc., but it also effects dispatching of input events to an application.
When a window has a control with keyboard focus, and then deactivated, this control always loose focus. When you activate a form again, the control which had focus last time will get it again.
—SA
这篇关于将焦点设置在Windows窗体的实例上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!