-2

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?

saadat rahimi
  • 127
  • 4
  • 10
  • 2
    Is this web application or desktop application ? – ilansch Jul 27 '13 at 17:20
  • And if desktop, is it WinForms or WPF? – RenniePet Jul 27 '13 at 17:23
  • You misunderstood. I can get the value of the text box but I want to search the database for that ID and retrieve other information such as name ,... and put them into some other text boxes . I have implemented it on text box leave event but I'd rather do it on key stroke It's WinForm – saadat rahimi Jul 27 '13 at 17:23
  • 1
    Don't tell people that they've misunderstood when you have provided so little information that it is impossible to understand your question. – RenniePet Jul 27 '13 at 17:25
  • Sorry if I insult you .I'm didn't mean it .I'm very sorry – saadat rahimi Jul 27 '13 at 17:26
  • Have you tried doing a search here on StockOverflow for "winforms textbox keystroke". You'll probably find several threads that discuss this sort of thing. – RenniePet Jul 27 '13 at 17:29
  • I'm not sure whether i can understand what you want,you have a textbox and you want to give a id number,when you press enter key from keyboard,you want to fetch some info from database containing that id,is it? – ridoy Jul 27 '13 at 17:30
  • http://stackoverflow.com/questions/3001237/how-to-catch-key-press-on-a-form-c-sharp-net http://stackoverflow.com/questions/81150/best-way-to-tackle-global-hotkey-processing-in-c these links could be good start – ilansch Jul 27 '13 at 17:31
  • Yes that is absolutely what i want to do. – saadat rahimi Jul 27 '13 at 17:34
  • The links were useful. My problem is solved. Thanks – saadat rahimi Jul 27 '13 at 18:34

2 Answers2

1
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char) Keys.Return)
    {
        ...
    }
}
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
0

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);
}
r.mirzojonov
  • 1,209
  • 10
  • 18