Search code examples
c#textboxfocus

Text box in focus when program starts


How can I set my text box "Fahrenheit" in focus when the program starts that I don't have to click it manually? Because the program has only 1 function and it would be more comfortable.

namespace Fahrenheit
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void label2_Click(object sender, EventArgs e)
    {
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      this.ActiveControl = textBox1;

      double Fahrenheit = Convert.ToDouble(textBox1.Text);
      double Celsius = (Fahrenheit - 32) * 5.0 / 9.0;

      textBox2.Text = Celsius.ToString("F");
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
  }
}

Solution

  • the line this.ActiveControl = textBox1; should be below the InitializeComponent(); Like this:

    public Form1()
    {
      InitializeComponent();
      this.ActiveControl = textBox1;
    }