How to valid with a shortcut

Verno

Member
Joined
Jan 5, 2020
Messages
10
Programming Experience
1-3
I would like that when I press the enter key after filling in the fields, it triggers the event Connect on picture
 

Attachments

  • login.png
    login.png
    19.8 KB · Views: 16
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 :
C#:
        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 :

DJ3Iri0.gif


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):
C#:
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;

            SuspendLayout();

            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));
            table.SuspendLayout();

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

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

            flow.Controls.Add(accept);
            flow.Controls.Add(cancel);
            flow.ResumeLayout();

            table.Controls.Add(flow);
            table.SetColumnSpan(flow, 2);
            table.ResumeLayout();

            Controls.Add(table);

            ResumeLayout(true);
        }

        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
            };
            table.Controls.Add(textBox);

            return textBox;
        }
    }

    class TestForm : Form
    {

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            new LoginForm().ShowDialog();
        }
    }
}
 
Back
Top Bottom