I'm currently working on a personal project and i have a bit of an issue. In the keypad display i ma unable to navigate up and down. Pressing the arrow keys only follows the button tabindex rather than the direction of the arrow key. For example: if i press the down arrow, instead of changing focus to button4, the focus moves to button2.
Although i have come up with some code that works, it does introduce a new problem. The OK button is initially hidden (until there have been 4 key presses), however in my code not only is the focus changed to it, it is also accessible through the keyboard. My question is: Is there a more elegant or efficient way of manipulating which button is focused?
Thank you.
(If it is of any help, i'm using visual studio 2008, C#, .NET3.5)
So far i have come up with this code:
private void frm_Login_KeyDown(object sender, KeyEventArgs e)
{
Control[] MyButtons = new Control[]{b1,b2,b3,b4,b5,b6,b7,b8,b9,btn_OK,b0,btn_Cancel};
switch (e.KeyValue)
{
case 13: //return
{ e.Handled = false; break; }
case 39: //Right
{
if (IndexofFocussedButton < 11) { IndexofFocussedButton++; }
else { IndexofFocussedButton = 0; }
break;
}
case 38: //Up
{
if (IndexofFocussedButton > 2) { IndexofFocussedButton-=3; }
else { IndexofFocussedButton +=9; }
break;
}
case 37: //Left
{
if (IndexofFocussedButton != 0) { IndexofFocussedButton--; }
else { IndexofFocussedButton=11; }
}
break;
case 40: //Down
{
if (IndexofFocussedButton < 9) { IndexofFocussedButton +=3; }
else { IndexofFocussedButton-=9; }
break;
}
}
if (e.KeyValue != 13)
{
e.Handled = true; //Don't pass on the keystroke
MyButtons[IndexofFocussedButton].Focus(); // Set the focus to the selected control
Application.DoEvents();
}
Loading
VulpesPosted Sep 27, 2013, 6:27 AM
I suspect the problem here is that you should be doing all this for the buttons themselves rather than the form even though the form is handling the KeyDown event first as your code currently stands.
To override the IsInputKey method for the buttons, you'd need to use a 'custom' button class which inherits from Button which is messy though you could avoid this by handling the PreviewKeyDown event for the 'standard' buttons.
What I'd be inclined to do is to handle the KeyDown and PreviewKey for the buttons themselves rather than intercept them at form level.
You could, of course, use the same handler for all the buttons and determine which one has been clicked by replacing this code:
Stefan CazacuPosted Sep 27, 2013, 5:03 AM
protected override bool IsInputKey(Keys keyData)
{
if ((keyData == Keys.Left)||(keyData == Keys.Right)|| (keyData == Keys.Up)||(keyData == Keys.Down))
{ return true; }
else { return base.IsInputKey(keyData); }
}
VulpesPosted Sep 26, 2013, 11:00 AM
In addition to the existing code, try handling the Form's PreviewKeyDown event (add the handler from the designer) as follows:
Stefan CazacuPosted Sep 26, 2013, 10:18 AM
VulpesPosted Sep 26, 2013, 9:33 AM
Button b = Form.ActiveControl as Button;
It should be:
Button b = this.ActiveControl as Button;
Stefan CazacuPosted Sep 26, 2013, 5:11 AM
VulpesPosted Sep 25, 2013, 1:39 PM