Reputation: 558
I have this procedure wich moves the cursor to the next textbox up/down within non multiline textbox.
When using key down from multiline textbox, cursor moves to the next textbox but does not focus in the next textbox when pressing key up idem. With key enter it moves from multiline to the next textbox and focuses it.
What can be the reason ?
I have 8 textboxes which are groupped with tag properties from 0 to 7. TxtboxNbrLimit is set to 8
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
{
if (KeyIndex < TxtboxNbrLimit)
++KeyIndex;
if (KeyIndex == TxtboxNbrLimit)
{
SaveBtn.Select();
return;
}
if (_textBox[KeyIndex].Text != "")
ChangeDone = true;
_textBox[KeyIndex].SelectionStart = 0;
_textBox[KeyIndex].SelectionLength = _textBox[KeyIndex].Text.Length;
_textBox[KeyIndex].Select();
_textBox[KeyIndex].Focus();
}
else
{
if (e.KeyCode == Keys.Up)
{
if (KeyIndex > 0)
--KeyIndex;
_textBox[KeyIndex].SelectionStart = 0;
_textBox[KeyIndex].SelectionLength = _textBox[KeyIndex].Text.Length;
_textBox[KeyIndex].Select();
_textBox[KeyIndex].Focus();
}
}
}
Upvotes: 1
Views: 819
Reputation: 89285
Try to move your logic to key up event instead of key down. That will work fine, TESTED.
Maybe multiline TextBox has some kind of event handler internally to handle up/down keyboard key pressed event, to move cursor between text lines. So your code work will be overridden by that internal behavior.
And in case you put it in key up event, the opposite happened. Your code logic will override effect of TextBox's internal behavior. So if the TextBox contains multiline text, the cursor will move to next/previous TextBox instead of moving between text lines inside the multiline TextBox.
Upvotes: 1