-1

I am trying to get the text of a textbox and save it to a variable when the user presses the enter key (when the textbox has info and is focused) since this is already inside a method i haven't been able to put another method inside like txtbox1_keypress (or i've been doing it wrong) so I need to be just code lines that i can insert in this method

private void df1_Click(object sender, EventArgs e)
{
    txtres.Focus();
    //timer
    timeLeft = 50;
    timer1.Start();
    //mult
    do
    {
        ban = 0;
        m1 = r.Next(1, 4);
        txtm1.Text = m1.ToString();
        m2 = r.Next(2, 6);
        txtm2.Text = m2.ToString();
        res = m1 * m2;
        //here is where i want to read txtm2 and continue with the rest
        if(txtres.Text == res.ToString())
        {
            pun++;
        }
        ban++;
    } while (timeLeft > 0 || ban != 10);
    if(pun == 10 && level == 0)
    {
        level++;
        System.IO.File.WriteAllText(@".\level.lvl", level.ToString());
    }
}
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74

2 Answers2

1

Since you mentioned you want to run it inside KeyPress event, here is the example:

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == (int) Keys.Enter)
      {
        //do the logic
      }
    }
Jalle
  • 1,566
  • 5
  • 22
  • 42
0

Add a button as the accept button of the form, this will trigger whenever enter is pressed.

Also, your loop (do -- while) should be done within the timer1_tick method, and take out the loop, the timer will execute the code within every interval it ticks until the timer reaches your maximum of 50.

As was mentioned, the loop will kill your GUI and it wont allow input.