Click here to Skip to main content
13,705,647 members
Rate this:
 
Please Sign up or sign in to vote.
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 16-May-18 2:36am
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 ?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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);
 }
  Permalink  
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;
}
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can do a foreach loop on the controls. A recursive function that registers every textbox to the event.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180920.1 | Last Updated 16 May 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100