【问题标题】:How to focus the child control when Tab is pressed?按下 Tab 时如何聚焦子控件?
【发布时间】:2016-05-19 06:25:21
【问题描述】:

我有一个自定义的TextBox 控件,这个控件里面有一个Windows 默认的TextBox 控件。由于按下 Tab 时它有一个子控件,因此光标不会直接聚焦在子控件上。需要双击 Tab 键。所以第一个 Tab 是给父母的,第二个是给孩子的。如何通过一个 Tab 键做到这一点?

自定义文本框是:

    class MyTextBox : Control
    {
        TextBox innerBox;
        public MyTextBox()
        {
            //.....
            innerBox = new TextBox();
            Controls.Add(innerBox);
            innerBox.Parent = this;
            innerBox.TabIndex = this.TabIndex;
            //.....
        }
        //.....
    }

【问题讨论】:

  • 将基类更改为 UserControl。或者将其更改为 TextBox 并且根本不添加“内框”。
  • 感谢@HansPassant,它成功了。您介意解释一下为什么UserControl 可以解决问题吗?
  • 它被设计为子控件的容器,而 Control 类不是。看看UserControl.WndProc() method 看看它是怎么做的..

标签: c# winforms custom-controls tabindex


【解决方案1】:

您可以使用tabindex 属性,并将子级指定为tabindex="1"。这将使子控件的标记类似于

<input type="textbox" runat="server" id="TB_id" tabindex="1"></input>

【讨论】:

  • 我注意到我的问题中有一个标签错误。对于那个很抱歉。实际上,这是一个winform问题
猜你喜欢
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2021-08-13
  • 2014-08-30
相关资源
最近更新 更多