In my project there is a Form mainForm in which there are two textBoxes txtUserName and txtPassword and also a button btnLogin>
Try this. Hopefully it will not cause any problem on pressing enter while your focus is on txtUsername or else where
If you write a in txtUserName and press enter, Your Admministrator choice will be selected from your autocompletecustomsource using regular expression and focus will go to txtPassword. My regular expression is very flexible you can made it bit restricted as following to match strictly from beginning and also can remove ignore case
Regex rg = new Regex("^" + txtUserName.Text);
private void mainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter))// && !txtUserName.Focus())// && intFlag.Equals(0))
{
if (txtUserName.Text.Length > 0)
{
if (txtUserName.Focused)
{
Regex rg = new Regex(txtUserName.Text, RegexOptions.IgnoreCase);
for (int i = 0; i < txtUserName.AutoCompleteCustomSource.Count; i++)
{
if (rg.IsMatch(txtUserName.AutoCompleteCustomSource[i]))
{
txtUserName.Text = txtUserName.AutoCompleteCustomSource[i];
txtPassword.Focus();
return;
}
}
}
if (txtPassword.Text.Length > 0)
{
btnLogin_Click(null, null); //login
}
else
{
//MessageBox.Show("Please Give a Password");
txtPassword.Focus();
}
}
else
{
//MessageBox.Show("Please Give a username");
txtUserName.Focus();
}
}
//if (txtPassword.ContainsFocus)
//{
// btnLogin_Click(sender, e); //login
//}
//else
//{
// this.txtPassword.Focus();
//}
}