I have a C# Windows Form Application that uses 10 textboxes as input fields, and I would like to be able to program these so that whichever textbox has focus can have the up arrow (Keys.Up
) or down arrow (Keys.Down
) can be pressed and focus will jump to the next or previous textbox.
So far I plan to use something like this:
if (e.KeyChar == Convert.ToChar(Keys.Up))
{
GetNextControl((TextBox)sender, false);
}
else if (e.KeyChar == Convert.ToChar(Keys.Down))
{
GetNextControl((TextBox)sender, true);
}
My only concern is whether or not this will interfere with the entry of actual text. Would the code above need to be changed to something like the code below?
if (e.KeyChar == Convert.ToChar(Keys.Up))
{
GetNextControl((TextBox)sender, false);
}
else if (e.KeyChar == Convert.ToChar(Keys.Down))
{
GetNextControl((TextBox)sender, true);
}
//any other key pressed
else
{
TextBox input = (TextBox)sender;
//add char relating to pressed key to text in TextBox
input.AppendText(e.KeyChar.ToString());
}
Is this else
clause required or will the default operations of the TextBox
handle this condition?
Thanks, Mark