Newbie: Can Buttons on Form never have focus?

  • Thread starter Thread starter Alex Gray
  • Start date Start date
A

Alex Gray

Hi,

I have a simple problem. I have a Form with two buttons. If i click
the up or down arrow keys, the buttons get focused. I don't want the
buttons to ever have focus, similar to the Windows Accessories
Calculator.

I've been searching around this forum, and from what i understand i
have to override the button's "KeyPress" function so that it just
returns. Is that the right thing to do?

Thanks,
-alex-
 
Alex,

Here is a kind of cheesy solution to your problem. Place a textbox on the
form, set it's Location.Y property to -50 (or anything to get it off the
visible area of the form). then add this one line of code to the KeyPress
event of the text box:

e.Handled = true;

this will keep the buttons from getting focus, and avoid the textbox ever
doing anything that might cause problems.

Kirk Graves
KRGIT Software
 
Hi Alex,
You can hook each button's Enter event. If you have any control on the form
that you want to keep the focus just do
(You can use the same method for all buttons on the form is method of the
form class):

private void button_Enter(object sender, System.EventArgs e)
{
focusCtrl.Focus();
}

If you don't want any control to be focused (?) just set forms'
ActiveControl property to null
Assuming that the event handler (you can use the same method for all buttons
on the form) is method of the form class:

private void button_Enter(object sender, System.EventArgs e)
{
this.ActiveControl = null;
}
 
Hi Guys,

I tried both the methods and they both work! Fantastic! Putting a
text box at -5 location so that it's hidden was also suggested to me
by someone at work, but I feel i'm hacking it.

I ended up using Stoitcho Goutsev's idea of overriding the Enter
method and setting focus (in my case setting "this.ActiveControl =
null;") to anything i want.

Thank you so much!
-alex-
 
Back
Top