disable key event when the focus is on autocompletion box of textbox

后端 未结 4 1123
渐次进展
渐次进展 2021-01-07 04:50

In my project there is a Form mainForm in which there are two textBoxes txtUserName and txtPassword and also a button btnLogin

4条回答
  •  时光说笑
    2021-01-07 05:33

    Actually, you have two issues.

    First off, set the AutoCompleteMode property of txtUserName to "SuggestAppend" instead of simply "Suggest." This way, if the user types the first letter or two, the correct entry will automatically be appended to the txtUSerName.Text.

    Next, modify your Form code as follows:

    void  Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode.Equals(Keys.Enter))  //Invokes whenever Enter is pressed
        {
            if (txtPassword.ContainsFocus)
            {
                btnLogin_Click(sender, e);  //login
            }
            else
            {
                this.txtPassword.Focus();
            }
        }
    }
    
    
    private void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtUserName.Text.Equals("Administrator") && txtPassword.Text.Equals("123"))
        {
            MessageBox.Show("Administrator");
        }
        else if (txtUserName.Text.Equals("Clerk") && txtPassword.Text.Equals("123"))
        {
            MessageBox.Show("Clerk");
        }
        else
        {
            MessageBox.Show("Please Enter correct details", "Login Error");
        }
    }
    

    In the above, the Key Down event handling code tests to see if the password text box has the focus (meaning, the user has presumable entered a user name already, and a password, and is ready to submit the data). If so, the btnLogin_Click event is called. Otherwise, (meaning, txtUserName probably has the focus) control is passed to txtPassword to continue data entry.

    UPDATE: re - Your comment:

    Simply kill the logic in the Key Down Event handler like so:

    Revised Event Handling Code:

    void  Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode.Equals(Keys.Enter))  //Invokes whenever Enter is pressed
        {
            btnLogin_Click(sender, e);  //login
        }
    }
    

    Note, another minor improvement (considering the overall structure of your code) would be to use a combo box for selection of UserName, and set the autocomplete source to "ListItems" then enter your options the same as with the text box. This requires the user to select from the pre-defined list. This still has scaleability issues similar to the previous, but eliminates an unnecessary step for the user if they simply make a typo while entering User Name data.

    Remember that users tend to dislike unnecessary interruption by pop-up messages. Allow them to select the appropriate "user name" from a drop down, type in the proper password, and move on.

    There are some better ways to do all of this, but this should tune up what you have into working order.

    On a final note, let me observe that eventually you will probably want to find a more robust way of performing this sort of validation. Any time you need to add users (which, in your code, appear to be defined more as "groups" you will need to add to your conditional event handling tree.

    You might check into persisting usernames and passwords in an encrypted file or database, and load them into a dictionary or something at run time. Then perform a key/value lookup on user/Password.

    Or something.

    Anyway, hope that helps.

    UPDATE 2: The complete code all in one shot. This should behave the way you are asking:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.KeyDown +=new KeyEventHandler(Form1_KeyDown);
            }
    
            void  Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode.Equals(Keys.Enter))  //Invokes whenever Enter is pressed
                {
                    btnLogin_Click(sender, e);  //login
                }
            }
    
    
            private void btnLogin_Click(object sender, EventArgs e)
            {
                if (txtUserName.Text.Equals("Administrator") && txtPassword.Text.Equals("123"))
                {
                    MessageBox.Show("Administrator");
                }
                else if (txtUserName.Text.Equals("Clerk") && txtPassword.Text.Equals("123"))
                {
                    MessageBox.Show("Clerk");
                }
                else
                {
                    MessageBox.Show("Please Enter correct details", "Login Error");
                }
            }
        }
    }
    

提交回复
热议问题