Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all,

 

I'm in the process of writing my first application for the compact framework.

 

I need to write an event handler that handles all keyup events. Now obviously I could simply override the OnKeyUp event of the form, which is something I have tried with limited success.

 

Whenever another control on the form has the focus the keyup event handler will obviously not fire.

 

I would have thought that this would be a common problem, I did a few searches on msdn but it didn't come up with anything that looked quite like what I was looking for.

 

Is there a solution to this?

Posted

Ok I have a working solution, however I am very interested to see if you guys think this was the right way to do it because I must confess this is a very new area for me.

 

At run time I recurse through each control on my form and assign the keyup event of that control an event handler like so:

       void AssignHandlers(Control container)
       {
           foreach (Control ctrl in container.Controls)
           {
               ctrl.KeyUp += new KeyEventHandler(Handle_KeyUp);
               AssignEventHandlers(ctrl);
           }
       }

       void Handle_KeyUp(object sender, KeyEventArgs e)
       {
           switch (e.KeyData)
           {
               case Keys.Down:
                   //do stuff;
                   break;
           }
       }

 

Any comments (good or bad) you have on how I've achieved this would be greatly appreciated.

 

Many Thanks

Posted
Set the forms .KeyPreview to true.

Hey thanks for answering, unfortunately I cannot use the KeyPreview property of the form because I'm using the compact framework and I don't seem to have that option available to me.

 

I think my solution is the best way of doing this but if someone has another way of doing this I'm all ears :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...