0

我有一个文本框数组

 _textBox = new TextBox[]
 { 
        textBox1, 
        textBox2,
        textBox3,
        textBox4,
        textBox5, 
        textBox6, 
        textBox7, 
        textBox8, 
        textBox9 
  };

我用这个方法通过击键在这个数组中向上/向下移动:

    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
        {
            if (KeyIndex < RowsLimit - 1)
                ++KeyIndex;
            else
            {
                if (KeyIndex != 7)
                    paymntgrp.Visible = false;
                SaveBtn.Select();
                KeyIndex = 0;
                return;
            }
            _textBox[KeyIndex].SelectionStart = 0;
            _textBox[KeyIndex].SelectionLength = _textBox[KeyIndex].Text.Length;
            _textBox[KeyIndex].Select();
            if (KeyIndex != 7)
                paymntgrp.Visible = false;
            else
                paymntgrp.Visible = true;
        }
        else
        {
            if (e.KeyCode == Keys.Up)
            {
                if (KeyIndex > 0)
                    --KeyIndex;
                _textBox[KeyIndex].SelectionStart = 0;
                _textBox[KeyIndex].SelectionLength = _textBox[KeyIndex].Text.Length;
                _textBox[KeyIndex].Select();
                if (KeyIndex != 7)
                    paymntgrp.Visible = false;
                else
                    paymntgrp.Visible = true;
            }
        }
    }

但是,当我在文本框之一中随机单击鼠标时,Keyindex 保持不变并从原来的位置继续。

如何将 Keyindex 设置为鼠标的位置。?

我应该在每个文本框 Keyindex = 0, Keyindex= 1 ...下写吗?(在方法 Mouse_Click for ex 下)

还是有更有效的方法?

4

1 回答 1

0

这将起作用(假设 KeyIndex 是一个类变量)

private void textBox_MouseDown(object sender, MouseEventArgs e)
{
    TextBox txtBox = (TextBox)sender;

    KeyIndex = Array.IndexOf(_textBox, txtBox);
}

更好的是使用 GotFocus() 事件。这将处理对控件的制表符以及单击:

private void textBox_GotFocus(object sender, EventArgs e)
{
    TextBox txtBox = (TextBox)sender;

    KeyIndex = Array.IndexOf(_textBox, txtBox);
}
于 2013-11-01T16:00:28.833 回答