0

My objective is to be able to enter a value in the textbox in form1 then press enter (when pressing the enter button the value will be pass to a method called setvalue. When the switch button is pressed then the form one will hide and open form2. Form2 has two buttons, show and exit. When show is clicked i need to display a messagebox that display the data in textbox in form1 by calling the getvalue method.

Please I'm open for ideas.

This is form one

public partial class Form1 : Form
{
    private int secretValue;

    public  void SetValue (int value){
          secretValue = value;
    }
    public int GetValue ()
    {

       return secretValue;
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.Visible = true;
        this.Hide();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnEnter_Click(object sender, EventArgs e)
    {

        secretValue = Convert.ToInt32(txtInput.Text);
        SetValue(secretValue);
    }
}

this form two

public partial class Form2 : Form
{

    public Form2()
    {
        InitializeComponent();
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Environment.Exit(0);
    }

    private void btnShow_Click(object sender, EventArgs e)
    {
        Form1 frm1 = new Form1();
           int val = frm1.GetValue();


           MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
    }
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • maybe you can just pass the data to form 2 by a constructor parameter. – Dustine Tolete Nov 18 '13 at 06:00
  • possible duplicate of [passing data between two forms using properties](http://stackoverflow.com/questions/5087934/passing-data-between-two-forms-using-properties) – slugster Nov 18 '13 at 06:08
  • This question has been asked many times before - did you search StackOverflow or check the suggested links before asking this question? – slugster Nov 18 '13 at 06:09

4 Answers4

0

Add a constructor value on your form 2. It should look like this:

public Form2(int secValue)

Then you can call it on your form 1 by passing the value

Form2 frm2 = new Form2(secretValue);

Maybe you can assign it to a global variable in your form 2, so that it can be referenced by your code in all of the form. Your form two can now be:

public partial class Form2 : Form
{

    int passedValue = 0;

    public Form2(int secValue)
    {
        InitializeComponent();
        passedValue = secValue;
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Environment.Exit(0);
    }

    private void btnShow_Click(object sender, EventArgs e)
    {
           MessageBox.Show(string.Format(passedValue.ToString(), "My Application", MessageBoxButtons.OK));
    }
}

In my opinion, there's no need for the property you created. Always remember, the values you assign to a class will be null if you change from form to form.

Dustine Tolete
  • 461
  • 1
  • 7
  • 19
0

Following code will not work becuase you are creating new instance of form1 and trying to get the value from there, It will only have the default vaue of int.

 private void btnShow_Click(object sender, EventArgs e)
    {
        Form1 frm1 = new Form1();
        int val = frm1.GetValue();
    }

Simply what you can do is to have a public property defined within Form2. and set the value of this property before showing Form2,

private void button2_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();

    //settting the value to Form2's property SecrectValueOfForm2, Now this value is available within Form2
    frm2.SecrectValueOfForm2 = ValueInForm1;
    frm2.Visible = true;
    this.Hide();
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
0

Plz try code as shown below:

public partial class Form1 : Form
{
   private int secretValue;
   Form2 frm2 = new Form2();

public  void SetValue (int value){
      secretValue = value;
}
public int GetValue ()
{

   return secretValue;
}

public Form1()
{
    InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{

    frm2 .Show();
    this.Hide();
}

private void Form1_Load(object sender, EventArgs e)
{
      frm2.Owner = this;
}

private void btnEnter_Click(object sender, EventArgs e)
{

    secretValue = Convert.ToInt32(txtInput.Text);
    SetValue(secretValue);
}
}

And On Form2:

public partial class Form2 : Form
{

public Form2()
{
    InitializeComponent();
}

private void btnExit_Click(object sender, EventArgs e)
{
    Environment.Exit(0);
}

private void btnShow_Click(object sender, EventArgs e)
{
    Form1 frm1 = new Form1();
       int val = ((Form1 )this.Owner).GetValue();


       MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Hitesh
  • 1,188
  • 5
  • 30
  • 52
0

Form2

 public Int32 _txtval { get; set; }
 private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(_txtval.ToString());
        }

Form1

 private void button1_Click(object sender, EventArgs e)
 {

            Form2 frm2 = new Form2();
            frm2._txtval = Convert.ToInt32(textBox1.Text);
            frm2.Visible = true;
            this.Hide();
  }
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36