How to get the value of strValue and read in Form1_Load?

  • Thread starter Thread starter Pugita
  • Start date Start date
P

Pugita

Guest
Hello everyone, I have one question.

There have 2 forms, (Form1 & Form2). Form1 and Form2 contains 1 textbox and 1 button... after running the application, by clicking a (buttonForm1), form2 will open. The user writes "Hello" in the (textboxForm2) and after that the user click a (buttonForm2), Form2 will hide and "Hello" is appears in (textboxForm1).

So my next plan, I want to add label1.Text in Form1. This label1.Text will read the same as (textboxForm1), and label1.Text is placed in private void Form1_Load(object sender, EventArgs e).

According to the codes below, strValue = "Hello", How to make label1.Text = strValue in Form1_load? When I run the application. Error will popup: strValue does not exist in the current context.

//Form1

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = strValue;
}
public void PassValue(string strValue)
{
textBox1.Text = strValue;
}

private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2(this);
objForm2.Show();
}
}

//Form2

public partial class Form2 : Form
{
Form1 ownerForm = null;
public Form2(Form1 ownerForm)
{
InitializeComponent();
this.ownerForm = ownerForm;
}
private void button1_Click(object sender, EventArgs e)
{
this.ownerForm.PassValue(textBox1.Text);
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{

}
}

Continue reading...
 
Back
Top