Console.ReadLine() equivalent for TextBox KeyDown or other event?

919 views Asked by At

How can I return the contents of a TextBox when its KeyDown event is fired? I want to make a Console.ReadLine() equivalent. So for example:

Write("Hi! Enter your name...");
Write(ReadFromTextBox());

Is this possible?

1

There are 1 answers

3
Archlight On

You can use the textChanged event.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    Console.WriteLine(textBox1.Text);
}

And if you want to read only after the user presses enter:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Enter)
        MessageBox.Show(textBox1.Text);

}