Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
Posted
Updated 16-May-18 3:43am
v2
Comments
BillWoodruff 16-May-18 13:46pm    
"the project is too granda "

does this mean that you need to find all TextBoxes on all open Forms at run-time ? More than one Form ?

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);
 }
 
Share this answer
 
Comments
Richard Deeming 17-May-18 12:34pm    
if(ctrl.GetType() == typeof(TextBox))


It would probably be better to use the as operator[^] here:
TextBox txt = ctrl as TextBox;
if (txt != null)
{
    txt.Enter += MyEnterEvent;
}


Or, if you're using an up-to-date compiler, use pattern matching[^]:
if (ctrl is TextBox txt)
{
    txt.Enter += MyEnterEvent;
}
You can do a foreach loop on the controls. A recursive function that registers every textbox to the event.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900