简体   繁体   English

如何通过单击制表符将控件集中在其父窗体的面板上添加的窗体中

[英]How to focus a control in a form which is added on a panel of its parent form by tabbing index

I'm working on winforms in c#. 我正在用C#开发Winforms。 I have a form which loads other forms in its panel. 我有一个在其面板中加载其他表格的表格。 Now my child forms have many textboxes. 现在,我的子窗体有许多文本框。

I want to set focus on one of those textboxes by loading my children forms by setting tab index to zero. 我想通过将选项卡索引设置为零来加载我的子窗体,从而将注意力集中在这些文本框之一上。

But it isn't happening when i load my children forms. 但是,当我加载我的孩子表格时,这不会发生。 I have taken care of the tab stop properties & i also went through the tabbing order of the forms. 我已经照顾了制表位的属性&我也经历了表格的制表顺序。 But the problem is still there. 但是问题仍然存在。

When i load children forms from startup it focuses zero indexed control. 当我从启动加载子窗体时,它将聚焦于零索引控制。 I guess my problem is that i am loading these forms in a panel of a parent form. 我想我的问题是我正在将这些表单加载到父表单的面板中。 Any solution to this problem? 这个问题有什么解决办法吗?

The Windows Forms controls in the following list are not selectable. 以下列表中的Windows窗体控件是不可选的。 Controls derived from these controls are also not selectable. 从这些控件派生的控件也是不可选择的。 Panel GroupBox PictureBox ProgressBar Splitter Label LinkLabel (when there is no link present in the control) 面板GroupBox PictureBox ProgressBar拆分器标签LinkLabel(当控件中没有链接时)

Focus is a low-level method intended primarily for custom control authors. 焦点是主要用于自定义控件作者的低级方法。 Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms. 而是,应用程序程序员应对子控件使用Select方法或ActiveControl属性,对窗体使用Activate方法。

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx https://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx

You can set focus to a control in a form which is added on a panel of its parent form by creating an instance of child form in the parent form and then setting focus to child form controls. 通过在父窗体中创建子窗体的实例,然后将焦点设置到子窗体控件,可以将窗体上的控件设置为焦点,该控件添加到其父窗体的面板上。 Make an instance of child form: ChildForm formInstanceName = new ChildForm(); 制作子窗体的实例:ChildForm formInstanceName = new ChildForm();

And then you can set the focus property for particular control as: formInstanceName .controlname.Focus(); 然后,您可以将特定控件的focus属性设置为:formInstanceName .controlname.Focus();

And before doing this make sure you add the child form to the respective panel. 并且在执行此操作之前,请确保将子窗体添加到相应的面板中。

Here is the snippet of the code I used: Form1 childform1 = new Form1();//creating an instance of child form private void btn_Click(object sender, EventArgs e) { 这是我使用的代码段:Form1 childform1 = new Form1(); //创建子窗体的实例private void btn_Click(object sender,EventArgs e){

Panel2.Controls.Clear();//To clear existing controls on panel
  Panel2.Controls.Add(childform1); // To add child form controls on the panel.
  childform1 .textbox1.Focus(); //To set focus to control of child form

} }

I hope this helps you.. 我希望这可以帮助你..

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

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