We help IT Professionals succeed at work.

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

stanforda asked
Medium Priority
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.
Watch Question

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.


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.
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.