Link to home
Start Free TrialLog in
Avatar of stanforda
stanforda

asked on

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.
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

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.
Avatar of stanforda
stanforda

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial