如何在项目的每个文本框中调用事件。 [英] How to invoke an event in every textbox in the project.

查看:73
本文介绍了如何在项目的每个文本框中调用事件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真正想要的是调用所有textBox项目的Enter和Leave事件,而不必通过文本框设置此文本框事件。



我创建了一个键盘,我希望它在用户想要键入内容时打开(输入事件),并在textBox失去焦点时关闭键盘。



挑战已经启动来吧伙计!!!!



我尝试过:



我试过按文本框​​调用键盘文本框,但这对我来说效率不高,因为这个项目太粗糙了。

What I really want is to call the Enter and Leave event of all the textBox's project, without me having to set this textbox event by textbox.

I created a keyboard and I want it to open whenever the user wants to type something (Enter event), and close the keyboard when the textBox loses focus.

The challenge was launched, come on guys !!!!

What I have tried:

I've tried putting invoke the keyboard textbox by textbox, but this is not very productive for me, because the project is too granda to do this.

推荐答案

public Form1()
 {
     InitializeComponent();
     RegisterTextBoxEvent(this);
 }


 private void RegisterTextBoxEvent(Control baseCtrl)
 {
     foreach(Control ctrl in baseCtrl.Controls)
     {
         if(ctrl.GetType() == typeof(TextBox))
         {
             ((TextBox)ctrl).Enter += MyEnterEvent;
         }

         RegisterTextBoxEvent(ctrl);
     }
 }

 private void MyEnterEvent(object sender, EventArgs e)
 {
     Console.WriteLine(((TextBox)sender).Name);
 }


您可以对控件执行foreach循环。一个递归函数,用于将每个文本框注册到事件。
You can do a foreach loop on the controls. A recursive function that registers every textbox to the event.


这篇关于如何在项目的每个文本框中调用事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆