質問

I have text box for ID. I want user to enter the ID and when he presses the Enter key I fetch some information from that specific ID from database.

how can capture the key stroke as I described?

役に立ちましたか?

解決

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char) Keys.Return)
    {
        ...
    }
}

他のヒント

Maybe this can help you:

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;
        }
    }

And then when the key is pressed on your textBox:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyData == Keys.Enter)
       textBox2.Text = GetSomethingFromID(textBox1.Text);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top