Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:27:52+00:00 2026-06-12T11:27:52+00:00

In my project there is a Form mainForm in which there are two textBoxes

  • 0

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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:27:53+00:00Added an answer on June 12, 2026 at 11:27 am

    You should not be handling the Enter key at all. You can remove your KeyDown handler, and instead use the AcceptButton property 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:

    public class MyTextBox : TextBox
    {
        protected override bool IsInputKey(Keys keyData)
        {
            return base.IsInputKey(keyData) || ((keyData & ~Keys.Shift) == Keys.Enter && IsDroppedDown);
        }
    }
    

    However, TextBox implements autocompletion using the SHAutoComplete function, which automatically creates an IAutoComplete object behind the scenes. That object cannot be accessed, and because of that, the IsDroppedDown property that I used in IsInputKey cannot be created. It would be implemented using IAutoCompleteDropDown.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 && IsDroppedDown in the above IsInputKey).

    Update: here’s how to create an IAutoComplete object 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:

    using System;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;
    
    [ComImport]
    [Guid("00bb2762-6a77-11d0-a535-00c04fd7d062")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [CoClass(typeof(IAutoCompleteClass))]
    interface IAutoComplete
    {
        void Init(HandleRef hwndEdit, IEnumString punkACL, string pwszRegKeyPath, string pwszQuickComplete);
        void Enable(bool fEnable);
    }
    

    IAutoComplete2.cs:

    using System;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;
    
    [Guid("EAC04BC0-3791-11d2-BB95-0060977B464C")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IAutoComplete2
    {
        void Init(HandleRef hwndEdit, IEnumString punkACL, string pwszRegKeyPath, string pwszQuickComplete);
        void Enable(bool fEnable);
        void SetOptions(AutoCompleteOptions dwFlag);
        AutoCompleteOptions GetOptions();
    };
    

    AutoCompleteOptions.cs:

    using System;
    
    [Flags]
    enum AutoCompleteOptions : int
    {
        None = 0x00,
        AutoSuggest = 0x01,
        AutoAppend = 0x02,
        Search = 0x04,
        FilterPrefixes = 0x08,
        UseTab = 0x10,
        UpDownKeyDropsList = 0x20,
        RtlReading = 0x40,
        WordFilter = 0x80,
        NoPrefixFiltering = 0x100,
    }
    

    IAutoCompleteDropDown.cs:

    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    
    [Guid("3CD141F4-3C6A-11d2-BCAA-00C04FD929DB")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IAutoCompleteDropDown
    {
        void GetDropDownStatus(out AutoCompleteDropDownFlags dwFlags, out StringBuilder wszString);
        void ResetEnumerator();
    }
    

    AutoCompleteDropDownFlags.cs:

    using System;
    
    [Flags]
    enum AutoCompleteDropDownFlags : int
    {
        None = 0x00,
        Visible = 0x01
    }
    

    IAutoCompleteClass.cs:

    using System;
    using System.Runtime.InteropServices;
    
    [ComImport]
    [Guid("00BB2763-6A77-11D0-A535-00C04FD7D062")]
    class IAutoCompleteClass
    {
    }
    

    EnumString.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;
    
    class EnumString : IEnumString
    {
        const int E_INVALIDARG = unchecked((int)0x80070057);
        const int S_OK = 0;
        const int S_FALSE = 1;
    
        int current;
        string[] strings;
    
        public EnumString(IEnumerable<string> strings)
        {
            this.current = 0;
            this.strings = strings.ToArray();
        }
    
        public void Clone(out IEnumString ppenum)
        {
            ppenum = new EnumString(strings);
        }
    
        public int Next(int celt, string[] rgelt, IntPtr pceltFetched)
        {
            if (celt < 0)
                return E_INVALIDARG;
    
            int num = 0;
            while (current < strings.Length && celt != 0)
            {
                rgelt[num] = strings[current];
                current++;
                num++;
                celt--;
            }
    
            if (pceltFetched != IntPtr.Zero)
                Marshal.WriteInt32(pceltFetched, num);
    
            if (celt != 0)
                return S_FALSE;
    
            return S_OK;
        }
    
        public void Reset()
        {
            current = 0;
        }
    
        public int Skip(int celt)
        {
            if (celt < 0)
                return E_INVALIDARG;
    
            if (strings.Length - current > celt)
            {
                current = strings.Length;
                return S_FALSE;
            }
    
            current += celt;
            return S_OK;
        }
    }
    

    MyTextBox.cs:

    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;
    
    public class MyTextBox : TextBox
    {
        IAutoComplete2 autoComplete;
        IAutoCompleteDropDown autoCompleteDropDown;
    
        public bool IsDroppedDown
        {
            get
            {
                if (autoCompleteDropDown == null)
                    return false;
    
                AutoCompleteDropDownFlags dwFlags;
                StringBuilder wszString;
                autoCompleteDropDown.GetDropDownStatus(out dwFlags, out wszString);
                return (dwFlags & AutoCompleteDropDownFlags.Visible) != AutoCompleteDropDownFlags.None;
            }
        }
    
        protected override void CreateHandle()
        {
            base.CreateHandle();
    
            autoComplete = (IAutoComplete2)new IAutoComplete();
            autoCompleteDropDown = (IAutoCompleteDropDown)autoComplete;
            autoComplete.SetOptions(AutoCompleteOptions.AutoSuggest);
            autoComplete.Init(new HandleRef(this, this.Handle), new EnumString(new string[] { "Administrator", "Clerk" }), null, null);
        }
    
        protected override void DestroyHandle()
        {
            ReleaseAutoComplete();
            base.DestroyHandle();
        }
    
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                ReleaseAutoComplete();
            }
            base.Dispose(disposing);
        }
    
        protected override bool IsInputKey(Keys keyData)
        {
            return base.IsInputKey(keyData) || ((keyData & ~Keys.Shift) == Keys.Enter && IsDroppedDown);
        }
    
        void ReleaseAutoComplete()
        {
            if (autoComplete != null)
            {
                Marshal.ReleaseComObject(autoComplete);
                autoComplete = null;
                autoCompleteDropDown = null;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project with a main MDI form. There is a child MDI
I have a windows form project with a main form. There is a textbox
I have the following code form.label :artists which outputs <label for=artist_artist_name>Artist name</label> How did
I Have a Windows Form Project , which has a main form,and in this
I have create a project with one main form window and also i have
in my project there's a submit button <input type=submit value=submit form onclick=window.form1.submit(); id=subBut> after
In my project there is a listview which contains textview and checkbox..Here is my
In my project there are large no. of FIXME / TODO which are to
I have a Winforms application, which hosts two PDF Viewers (Unmanaged C++) in its
I'm working with a C# project using System.Windows.Form to create the GUI, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.