C# 如何将文本框作为按键功能c的参数发送?

C# 如何将文本框作为按键功能c的参数发送?,c#,function,parameters,textbox,C#,Function,Parameters,Textbox,我有10个文本框。我想要一个通用的KeyDown函数,这样我可以在调用它时发送一个参数。我在textbox1中输入一些文本,然后按“回车”键,然后光标聚焦发送文本框(例如:textbox2),在调用KeyDown函数时将其作为参数发送 private void textBox1_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { textBox

我有10个文本框。我想要一个通用的KeyDown函数,这样我可以在调用它时发送一个参数。我在textbox1中输入一些文本,然后按“回车”键,然后光标聚焦发送文本框(例如:textbox2),在调用KeyDown函数时将其作为参数发送

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.Enter)
        {
            textBox2.Focus();
        }
    }

TextBox
实例通过
sender
参数发送:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    TextBox textBox = sender as TextBox;

    if (e.KeyCode == Keys.Enter)
    {
        // if we can focus:
        //   1. it's a textbox instance that has called Key Down event
        //   2. the textbox can be focused (it's visible, enabled etc.)  
        // then set keyboard focus  
        if ((textBox != null) && textBox.CanFocus) 
        {
            textBox.Focus();
            // you may find useful not proceeding "enter" further
            e.Handled = true; 
        }
    }
}

请确保已为所有感兴趣的文本框(
textBox1
textBox10
)指定了相同的
textBox1\u KeyDown
方法。
TextBox
实例通过
sender
参数发送:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    TextBox textBox = sender as TextBox;

    if (e.KeyCode == Keys.Enter)
    {
        // if we can focus:
        //   1. it's a textbox instance that has called Key Down event
        //   2. the textbox can be focused (it's visible, enabled etc.)  
        // then set keyboard focus  
        if ((textBox != null) && textBox.CanFocus) 
        {
            textBox.Focus();
            // you may find useful not proceeding "enter" further
            e.Handled = true; 
        }
    }
}

请确保您为所有感兴趣的文本框指定了相同的
textBox1\u KeyDown
方法(
textBox1
textBox10

Dmitry Bychenko给出的答案有您需要的依据。但是,如果您希望始终选择下一个文本框,则首先需要该顺序的某种列表,并将其填充到类构造函数中:

private TextBox[] textBoxOrder;

public Form Form1()
{
    InitializeComponent();
    textBoxOrder = new TextBox[]
    {
        textBox1, textBox2, textBox3, textBox4, textBox5,
        textBox6, textBox7, textBox8, textBox9, textBox10
    };
}
然后,在密钥侦听器中,您可以执行以下操作来选择下一个密钥侦听器:

TextBox nextBox = null;
for (Int32 i = 0; i < textBoxOrder.Length; i++)
{
    if (textBoxOrder[i] == sender)
    {
        if (i + 1 == textBoxOrder.Length)
            nextBox = textBoxOrder[0]; // wrap around to first element
        else
            nextBox = textBoxOrder[i + 1];
        break;
    }
}
if (nextBox != null)
    nextBox.Focus();
TextBox nextBox=null;
对于(Int32 i=0;i
Dmitry Bychenko给出的答案有你需要的依据。但是,如果您希望始终选择下一个文本框,则首先需要该顺序的某种列表,并将其填充到类构造函数中:

private TextBox[] textBoxOrder;

public Form Form1()
{
    InitializeComponent();
    textBoxOrder = new TextBox[]
    {
        textBox1, textBox2, textBox3, textBox4, textBox5,
        textBox6, textBox7, textBox8, textBox9, textBox10
    };
}
然后,在密钥侦听器中,您可以执行以下操作来选择下一个密钥侦听器:

TextBox nextBox = null;
for (Int32 i = 0; i < textBoxOrder.Length; i++)
{
    if (textBoxOrder[i] == sender)
    {
        if (i + 1 == textBoxOrder.Length)
            nextBox = textBoxOrder[0]; // wrap around to first element
        else
            nextBox = textBoxOrder[i + 1];
        break;
    }
}
if (nextBox != null)
    nextBox.Focus();
TextBox nextBox=null;
对于(Int32 i=0;i
请阅读本手册,尤其是Uh。几乎所有C#中的侦听器都已经包含一个
对象发送器
。@nyrguds我想他是说在
textBox1
的on lick事件中,他想传递一个文本框而不是它本身,例如
textBox2
parameter@AlfieGoodacre除非是固定的旋转顺序,否则是不可能的;在这种情况下,答案是简单地将它们全部放在某个全局数组中,然后检查您当前所在的数组。请阅读。几乎所有C#中的侦听器都已经包含一个
对象发送器
。@nyrguds我想他是说在
textBox1
的on lick事件中,他想传递一个文本框而不是它本身,例如
textBox2
parameter@AlfieGoodacre除非是固定的旋转顺序,否则是不可能的;在这种情况下,答案是简单地将它们全部放在某个全局数组中,然后检查您当前所在的数组。