Should I use form1_Load event to have text box information displayed when form is opened?

StackOverflow https://stackoverflow.com/questions/10386705

  •  04-06-2021
  •  | 
  •  

Pergunta

I have a winform with a button that the user clicks when they want to generate a certain report. When this button is clicked, another winform opens up. The second winform has a text box and a button to take you back to the first winform.

When the second from opens up, I want the text box to already have the report displayed. Therefore, all the user has to do is is look at it and go back to the previous form when finished.

To do this, would I assign the text box to the appropriate method and put it in a Form1_Load event method?

I've never used the Form1_Load event method so I'm a little unsure if this is the proper way of doing it.

Foi útil?

Solução

Yes, of course, in the Form_Load event you have access to all of your controls already Initialized by the form constructor via InitializeComponent().
Then you can call

private void Form_Load(object s, EventArgs e)
{
    textBox1.Text = "your_report_title";
}

Outras dicas

Yes, this would be the correct use of Form1_Load().

private void Form1_Load(object sender, EventArgs e)
{
    textbox1.Text = "Whatever is supposed to go here"
}

Alternatively you can use the constructor of the form which should already be there.

public Form2(string text)
{
    InitializeComponent();

    textBox1.Text = text;
}

Then just open the form using

Form2 form2 = new Form2("text that should be displayed");
form2.Show();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top