-2

我有 ID 的文本框。我希望用户输入 ID,当他按下 Enter 键时,我从数据库中的特定 ID 中获取一些信息。

如我所述,如何捕获击键?

4

2 回答 2

1
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char) Keys.Return)
    {
        ...
    }
}
于 2013-07-27T17:51:17.593 回答
0

也许这可以帮助你:

public string GetSomethingFromID(string ID)
    {
        try
        {
            string cmdString = "SELECT columnName FROM tableName WHERE ID='" + ID + "'";

            SqlConnection con = new SqlConnection(connectionString);  // create your connection string and write it's name.
            con.Open();

            SqlCommand command = new SqlCommand(cmdString, con);
            SqlDataReader sdr = command.ExecuteReader();
            sdr.Read();
            string something = sdr[0].ToString();

            con.Close();
            return something;
        }
        catch
        {
            return null;
        }
    }

然后当在你的文本框上按下键时:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyData == Keys.Enter)
       textBox2.Text = GetSomethingFromID(textBox1.Text);
}
于 2013-07-27T17:44:48.147 回答