How to valid with a shortcut


Jan 5, 2020
Programming Experience
I would like that when I press the enter key after filling in the fields, it triggers the event Connect on picture


  • login.png
    19.8 KB · Views: 14
If you want to do something then you need to handle the event that is raised at the appropriate time. What event is raised when the user hits the Enter key? That's the event you need to handle and then you need to write the appropriate code to do what you want to do.
Please refrain from asking for code on the forums unless you are willing to pay the developers for their work. If you have code that you have written yourself, then you need to post that code here and explain what it is you're trying to do, and what problem(s) you are experiencing with that code. Also be sure to post your code in code tags when posting to the forums, as this helps keep the code formatting and makes for easier readability.

A general key up event looks like the below. You can also invoke click on the button which would hold your action for logging in, if the enter key is pressed. Reading the keyeventargs we can check what key was pressed and in this case if it is enter key, we perform click on our button1 which would then log you in. You need to write your own code for that. These are the events you'd use, keep in mind that its generally frowned upon to ask for code on any forum :
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
            if (e.KeyCode == Keys.Enter) button1.PerformClick();

        private void button1_Click(object sender, EventArgs e)
            MessageBox.Show("Called by key press or clicked");
See : Control.Click Event (System.Windows.Forms) and Control.KeyUp Event (System.Windows.Forms)
okay understood, and thank you for the explanation. I thought that it should indeed be put on the form. Thanks for the clarification
The way to get those events into your code file is to do as i I have in the gif, so don't just copy and paste :


Or alternatively write your own events and handlers yourself, which is what I generally do. Since I don't like using or working with the designer, and I prefer to hand-code my forms. But that's just personal preference, since I have trust issues with the designer generated code.
No need to quote the person directly above you, especially in whole. Quotes like that just takes up massive scrolling spaces for people browsing on mobiles. Use the quick reply area at the bottom instead. :)

Happy to help!
Here's a quick demo that uses the AcceptButton (on line 23):
using System;
using System.Drawing;
using System.Windows.Forms;

namespace SimpleWinForms
    class LoginForm : Form
        TextBox _txtUserName;
        TextBox _txtPassword;

        public string Username => _txtUserName.Text;
        public string Password => _txtPassword.Text;

        public LoginForm()
            Text = "Login";
            ShowIcon = false;
            SizeGripStyle = SizeGripStyle.Show;

            Button btnConnect = new Button() { Text = "Connect" };
            btnConnect.Click += (o, e) => DialogResult = DialogResult.OK;
            AcceptButton = btnConnect;

            var btnCancel = new Button() { Text = "Cancel" };
            btnCancel.Click += (o, e) => DialogResult = DialogResult.Cancel;
            CancelButton = btnCancel;

            LayoutControls(accept: btnConnect, cancel: btnCancel);

        void LayoutControls(Button accept, Button cancel)
            Padding = new Padding(5);
            Size = new Size(260, 150);
            AutoSize = true;


            var table = new TableLayoutPanel()
                Dock = DockStyle.Fill,
                AutoSize = true,
                ColumnCount = 2,
                RowCount = 3
            table.ColumnStyles.Add(new ColumnStyle());
            table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80f));

            _txtUserName = AddLabelAndTextBox(table, "User Name:");
            _txtPassword = AddLabelAndTextBox(table, "Password:");

            var flow = new FlowLayoutPanel()
                AutoSize = true,
                Anchor = AnchorStyles.Right | AnchorStyles.Bottom


            table.SetColumnSpan(flow, 2);



        TextBox AddLabelAndTextBox(TableLayoutPanel table, string label)
            table.Controls.Add(new Label()
                Text = label,
                AutoSize = true,
                Anchor = AnchorStyles.Right

            var textBox = new TextBox()
                AutoSize = true,
                Anchor = AnchorStyles.Left | AnchorStyles.Right

            return textBox;

    class TestForm : Form

        static void Main()
            new LoginForm().ShowDialog();

Latest posts

Top Bottom