code_junkie

C# How do I click a button by hitting Enter whilst textbox has focus?

Nov 12th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. TextBox tb = new TextBox();
  2. Button btn = new Button { Dock = DockStyle.Bottom };
  3. btn.Click += delegate { Debug.WriteLine("Submit: " + tb.Text); };
  4. Application.Run(new Form { AcceptButton = btn, Controls = { tb, btn } });
  5.  
  6. TextBox tb = new TextBox();
  7. Button btn = new Button { Dock = DockStyle.Bottom };
  8. btn.Click += delegate { Debug.WriteLine("Submit: " + tb.Text); };
  9. tb.KeyDown += (sender,args) => {
  10. if (args.KeyCode == Keys.Return)
  11. {
  12. btn.PerformClick();
  13. }
  14. };
  15. Application.Run(new Form { Controls = { tb, btn } });
  16.  
  17. void TextBox1_GotFocus(object sender, EventArgs e)
  18. {
  19. this.AcceptButton = ProcessTextBox1;
  20. }
  21.  
  22. void TextBox2_GotFocus(object sender, EventArgs e)
  23. {
  24. this.AcceptButton = ProcessTextBox2;
  25. }
Add Comment
Please, Sign In to add comment