Setting a Controls Focus?

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

I have created a c-sharp app that has keyup, keydown events tied to the
form. The form also has some other controls, buttons, labels and 1 custom
control that I made which inherits from user control.

My problem is ... When I first start the app all my keyboard events work
fine and by doing debugging I can see that my form has the focus. After I
click on a button or my custom control the focus is shifted to those
controls and the keyup, keydown events no longer fire. So to fix this I
thought I would set the focus back to the form at the end of a button press
event. I went into this code in the debugger and the this.focus() returns
false which I think means focus was not given back to the main form but I
don't understand why. I have also tried setting the tabstop property on all
the buttons to false, but that didn't help. Once I click somewhere inside my
app then the keyup and keydown events no longer get called. Basically I want
the main form to always have focus I guess so that my keyup, and keydown
events fire.

Any ideas would be great !
Thanks
Jay
 
Set the form's Keypreview attribute to ture in the designer. This allows
and keyboard events for controls to be registered with the form as well.
 
Back
Top