检测嵌套 TableLayoutPanel 内所有文本框的按键事件
我有一系列嵌套的 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 TableLayoutPanel
controls 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 parentTableLayoutPanel
and all the other layout panels are inside it, is itself inside a series ofPanel
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并为 parent TableLayoutPanel 调用此方法
And call this method for parent TableLayoutPanel