Passing keypress event from one control to another inside a user control

Posted on 2009-02-11
Last Modified: 2013-11-07
I have a Windows UserControl with two constituent controls; a combo box and a datagrid view. The primary objective for the control is to provide a multi-column combo list. I basically have the control working correctly. If I type some text into the combo list text box and then drop down the datagridview shows correctly and gets its contents from the database according the the text entered. For example, if I were using it to show Part Numbers and type "AB", then dropdown the list I see all parts beginning with "AB". No problem. While the drop down is still showing I want to continue to be able to enter text and have then list contents refined as the user provides more characters. The actual filtering part is fine as I just refresh the data in the combo TextChanged event. I had code in the keypress event of the datagridview to update the "Text" property of the combo box, but this produced some weird behaviour. For example, it moved the cursor to the beginning of the text, highlighting the entire text contents... just to name a couple of strange things.
The code looked like this;
        private void dataGridViewEntity_KeyPress(object sender, KeyPressEventArgs e)
                    this.cboEntity.Text += Convert.ToChar(e.KeyChar).ToString();
I then decided to see if I could just pass the keypress directly to the combo box so it just deals with it properly;
        private void dataGridViewEntity_KeyPress(object sender, KeyPressEventArgs e)
This fires the KeyPress event on cboEntity no problem, but it doesn't actually update the text. So if I type the letter "a" while the datagridview has focus, this does not appear in the combo text area using the above code.

I looked at Sendkey class, but realise that I would probably need to set focus to the combo, then use this... thing is I still want the datagridview to have the focus.

Thanks in advance.
Question by:stanforda
    LVL 13

    Expert Comment

    Probably you should add the OnKeyPress even handler for your combo and program yourself the behaviour in it by setting the "Text" property of the combo with the info in the KeyPressEventArgs param or by reading the datagrid directly if accessible.

    Author Comment

    I am wondering why this didn't work;
            private void dataGridViewEntity_KeyPress(object sender, KeyPressEventArgs e)
    ... because it seems like the correct approach. It just seems as though it says "OK I will accept this keypress event"... but then ignores the character the was typed.
    LVL 13

    Accepted Solution

    this.cboEntity.OnKeyPress(e) doesn't write anything in the combo. It is just raising the event KeyPress programmatically. But it seems that when a user presses a key, then the KeyDown event is raised first IF THE CONTROL HAS THE FOCUS.
    You can probably try to wrtite this.cboEntity.OnKeyDown(e) instead but as I said, since you correctly fire the KeyPress event with the correct info (that's what you've said),  then why not overriding the OnKeyPress event handler ?
            private void cboEntity_Press(object sender, KeyPressEventArgs e)
                    this.Text = Convert.ToChar(e.KeyChar).ToString();
                    e.Handled = false;

    BTW, the use of this event seems to be usually different. See the example here :
    It can certainly give you some ideas.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now