0

I have two textboxes in my windows form named txtUserName and txtPassword, now I'm populating the txtUserName using the code

AutoCompleteStringCollection myCollection = new AutoCompleteStringCollection();
_userEMailList = UserCredentials.GetUserEmails();

foreach (var item in _userEMailList)
{
    myCollection.Add(item);
}

txtUserName.AutoCompleteCustomSource = myCollection;

where UserCredentials.GetUserEmails() method returns a List<string>.

Now the user will get suggestions and thus can select from one of the options.

So my problem is that when the user selects from one of the suggestion, the value of txtPassword should auto-fill. I have a method which takes EMail-ID as a parameter and returns the password. In web application, we can use onblur but as my code is part of a windows application, so JavaScript usage is not possible.

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
JDK
  • 125
  • 2
  • 14

1 Answers1

2

You should just check for a press of Enter. Selecting an item from autocomplete with the mouse will also trigger this:

private void txtUserName_KeyDown(object sender, KeyEventArgs e) 
{
    if (e.KeyData == Keys.Enter) 
    {
        this.txtPassword.Text = GetPassword(this.txtUserName.Text);
    }
}
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55