Home > Net >  How to Auto click the button after typing the ID number to textbox (Visual Studio C#)
How to Auto click the button after typing the ID number to textbox (Visual Studio C#)

Time:11-04

i want to auto click the search button

Search button >>>

i want the button to auto click after entering the school ID of textbox

CodePudding user response:

you should first determine how you want to detect the ID has entered. then you should add a handler to that event to call the search button click handler . for example if you want to do the search after user pressed the 'Enter' key, add a handler to 'KeyPress' envent of the School ID text box and call the search button's click event handler if the key is 'Enter' key:

      private void SchoolID_keypressed(object sender, KeyPressEventArgs e)
      {
         if (e.KeyChar == Convert.ToChar(Keys.Enter))
            Search_Click(sender, EventArgs.Empty);
      }

P.S.: I assumed you are developing WinForm application

CodePudding user response:

You can try using keyboard events.

  1. Receive input from the keyboard. Find the workspace that receives keyboard input. For example, in the default form form1, right-click into the property and set keypreview to true. If not set to true, the default is false and cannot receive keyboard input.

    enter image description here

  2. Design UI as shown below. The textbox binds the keyboard event to trigger the click of the serarch button.

    enter image description here enter image description here

  3. Code logic: input data in the text box -> press the space bar -> the event triggers the click of the button button.

     private void FrmLogin_KeyDown(object sender, KeyEventArgs e)
     {
         School.UID = textBox1.Text;
         if (e.KeyCode == Keys.Space) // press the space bar
         {
            if(School.UID == "20191097")
             {
                 this.button1.PerformClick();
    
             }
    
         }
    
    
     }
    
     private void button1_Click(object sender, EventArgs e)
     {
         MessageBox.Show("Hello: "   School.UID);
     }
    
      internal class School
    {
         public static string UID = "";
    }
    

Test code:

enter image description here

  • Related