[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-02-11
3
Medium Priority
?
468 Views
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.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
Comment
Question by:stanforda
  • 2
3 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 23611169
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
 

Author Comment

by:stanforda
ID: 23612871
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
 
LVL 13

Accepted Solution

by:
kaylanreilor earned 1500 total points
ID: 23620994
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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

873 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