检测嵌套 TableLayoutPanel 内所有文本框的按键事件

发布于 2024-12-18 02:40:55 字数 1018 浏览 0 评论 0原文

我有一系列嵌套的 TableLayoutPanel 控件,每个控件都包含许多 TextBox 控件。

我认为为每个文本框创建一个按键事件是疯狂的,所以我想做的是有一个通用的事件方法,然后在 FormLoad 事件上为所有文本框应用该事件。我想要做的是查看用户是否在任何这些文本框中按下了 Enter 键。

这是我常用的方法(我希望没有问题!):

private void ApplyFiltersOnEnterKey(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
    {
        tsApplyFilters_Click(this, null);
    }
}

并且我在表单的 Load 事件中有以下代码:

    //Applying common event for all textboxes in filter options!
foreach (var control in tableCriterias.Controls)
{
    var textBox = control as TextBox;
    if (textBox != null)
        textBox.KeyPress += new KeyPressEventHandler(this.ApplyFiltersOnEnterKey);
} 

好吧,也许您已经猜到了,上面的代码不起作用!我可以列出我能想到的问题:

  • tableCriterias 它是父 TableLayoutPanel ,所有其他布局面板都在其中,它本身位于一系列 Panel 内部 SplitContainer 和...我需要在循环中指出它吗?
  • 或者我是否递归循环主布局面板内的每个布局面板?
  • 或者整个想法都是错误的?!!!

谢谢。

I have a series of nested TableLayoutPanelcontrols which each of them contains lots of TextBox controls.

I think it is insane to make a keypress event for each of the textboxes, So what I am trying to do is to have a common event method and then apply the event for all textboxes on FormLoad event. What I want to do is to see if the user has pressed Enter key in any of those textboxes.

This is my common method (I hope nothing is wrong with it!):

private void ApplyFiltersOnEnterKey(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
    {
        tsApplyFilters_Click(this, null);
    }
}

And I have the following code in Load event of my form:

    //Applying common event for all textboxes in filter options!
foreach (var control in tableCriterias.Controls)
{
    var textBox = control as TextBox;
    if (textBox != null)
        textBox.KeyPress += new KeyPressEventHandler(this.ApplyFiltersOnEnterKey);
} 

Well, maybe you can guess already, the codes above does not work! I can list the problems I can think of:

  • tableCriterias which is the parent TableLayoutPanel and all the other layout panels are inside it, is itself inside a series of Panel SplitContainer and....Do I need to point this in my loop?
  • Or do I recursively loop over each layoutpanel inside the main layoutpanel?
  • Or the whole idea is wrong?!!?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2024-12-25 02:40:55
private void Recursive(TableLayoutPanel tableCriterias)
{
    foreach (var control in tableCriterias.Controls)
    {
        var textBox = control as TextBox;
        if (textBox != null)
            textBox.KeyPress += new KeyPressEventHandler(this.ApplyFiltersOnEnterKey);
        else if(control is TableLayoutPanel)
            Recursive(control as TableLayoutPanel);
    } 
}

并为 parent TableLayoutPanel 调用此方法

private void Recursive(TableLayoutPanel tableCriterias)
{
    foreach (var control in tableCriterias.Controls)
    {
        var textBox = control as TextBox;
        if (textBox != null)
            textBox.KeyPress += new KeyPressEventHandler(this.ApplyFiltersOnEnterKey);
        else if(control is TableLayoutPanel)
            Recursive(control as TableLayoutPanel);
    } 
}

And call this method for parent TableLayoutPanel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文