• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

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

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.cboEntity.OnKeyPress(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.
0
stanforda
Asked:
stanforda
  • 2
1 Solution
 
kaylanreilorCommented:
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.
0
 
stanfordaAuthor Commented:
I am wondering why this didn't work;
        private void dataGridViewEntity_KeyPress(object sender, KeyPressEventArgs e)
        {
                this.cboEntity.OnKeyPress(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.
0
 
kaylanreilorCommented:
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 :
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress.aspx
It can certainly give you some ideas.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now