In my project there is a Form mainForm in which there are two textBoxes txtUserName and txtPassword and also a button btnLogin.
I have given the following txtUserName properties:
txtUserName Properties
AutoCompleteCustomSource - Collection
--> Administrator
--> Clerk
AutoCompleteMode - Suggest
AutoCompleteSource - CustomSource
btnLogin_Click Event
if (txtUserName.Text.Equals("Administrator") && txtPassword.Text.Equals("123"))
{
//function to access admin features
}
else if (txtUserName.Text.Equals("Clerk") && txtPassword.Text.Equals("123"))
{
//function to access clerk features
}
else
{
MessageBox.Show("Please Enter correct details", "Login Error");
}
I have setted the mainForm keypreview to true and implemented function to keyDown event of mainForm which is shown in the below code:
mainForm_KeyDownEvent
if (e.KeyCode.Equals(Keys.Enter)) //Invokes whenever Enter is pressed
{
btnLogin_Click(sender,e); //login
}
Now my problem is that whenever the focus is on txtUserName and pressing A, dropdown is showing to select “Administrator” (which is defined in collections as I shown in above properties). When I click Enter on keyboard it is showing MessageBox instead of selecting “Administrator”. I know that is invoking the keydown event of mainForm. How to disable the keyDown event, when it is on textbox dropdown thing so that I can press enter?
EDIT:
I tried the below code in public form() 🙁not working)
InitializeComponent();
if (txtUserName.AutoCompleteMode) { /* showing red scribbles */
this.KeyDown -= mainForm_KeyDown;
}
You should not be handling the Enter key at all. You can remove your
KeyDownhandler, and instead use theAcceptButtonproperty of the form to set the button that gets “clicked” when Enter is pressed. This is already supposed to not “click” the button when another control has already handled the Enter key.That isn’t enough for your situation, because standard Windows behaviour is for the Enter key to press the default button. Press Win+R, for example, to get the Run… dialog, start typing C:\Use, press Down to select C:\Users, press Enter, and see what happens.
In order to override that behaviour, you need to make the text box tell the form that it will be handling the Enter key itself, so that the form won’t send it to the default button. This can be done by creating a derived class and overriding
IsInputKey:However,
TextBoximplements autocompletion using theSHAutoCompletefunction, which automatically creates anIAutoCompleteobject behind the scenes. That object cannot be accessed, and because of that, theIsDroppedDownproperty that I used inIsInputKeycannot be created. It would be implemented usingIAutoCompleteDropDown.GetDropDownStatus, but since the object is inaccessible, you cannot (reliably) determine whether the dropdown list is showing.You would need to either implement the auto completion without using the built-in
AutoComplete*properties, or you would need to always suppress the Enter key (remove the&& IsDroppedDownin the aboveIsInputKey).Update: here’s how to create an
IAutoCompleteobject manually. The strings Administrator and Clerk are hardcoded. The GetDropDownStatus function is used to suppress any default button’s handling of Enter when the drop down list is visible. Feedback welcome.IAutoComplete.cs:
IAutoComplete2.cs:
AutoCompleteOptions.cs:
IAutoCompleteDropDown.cs:
AutoCompleteDropDownFlags.cs:
IAutoCompleteClass.cs:
EnumString.cs:
MyTextBox.cs: