0

This is the first form( it contains an OK button and a textbox)

namespace Testt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public int dimx;
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            this.Hide();
            f2.ShowDialog();
            this.Show();

            dimx = int.Parse(textBox1.Text);
            MessageBox.Show(dimx.ToString());


        }
    }
}

This is the second form (it contains an OK button + a messageBox when OK is pressed)

namespace Testt
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f=new Form1();
            MessageBox.Show(f.dimx.ToString());
        }
    }
}

I want to write the value of 6 in the textbox press OK, then form2 pops up and when i press OK on the second form it should display 6 not 0..what am i doing wrong?

spoder
  • 9
  • 2
  • It's because you're instantiating a new `Form1()` in `Form2` when you click the button - the default value of the `f.dimx` will be `0`, as it's a new value – Geoff James Oct 14 '16 at 13:08
  • When doing this kind of thing, it's good to decouple the forms by using a Model View Controller or Model View Presenter (or similar) approach. [Have a look at my answer here for a simple example.](http://stackoverflow.com/a/15605436/106159) – Matthew Watson Oct 14 '16 at 13:22

3 Answers3

1

You could make it so that your form takes dimx as a variable, so it would look like this

public partial class Form2 : Form
{
    private int dimX;
    public Form2(int dimx)
    {
        InitializeComponent();
        dimX = dimx;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(dimX.ToString());
    }
}

alternatively you could pass the form itself, changing

public Form2(int dimx)

into

public Form2(Form1 f1)

You would then also have to replace

private int dimX;
//and
dimX = dimx;
//and
MessageBox.Show(dimX.ToString());

with

private Form1 f;
//and
f = f1;
//and
MessageBox.Show(f.dimx.ToString());
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26
  • what changes do i have to make in form1? – spoder Oct 14 '16 at 13:19
  • The change to form one will be changing `Form2 f2 = new Form2();` into `Form2 f2 = new Form2(dimx);` or `Form2 f2 = new Form2(this);` Also you must remove the line `Form1 f=new Form1();` from form 2 – Alfie Goodacre Oct 14 '16 at 13:31
  • Well should it work? I get the same answer 0..Sorry but im quite new at c# :D – spoder Oct 14 '16 at 13:42
0

You are creating a NEW form object on every onclick event and that is not the way it works...

private void button1_Click(object sender, EventArgs e)
{
      Form1 f=new Form1();  // no that way!!!
      MessageBox.Show(f.dimx.ToString());
}

use instead a callback, delegate

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

So im using this now

Form1:

namespace Testt
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public int dimx;

            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2(this);
                this.Hide();
                f2.ShowDialog();
                this.Show();

                dimx = int.Parse(textBox1.Text);
                //MessageBox.Show(dimx.ToString());


            }
        }
    }

and Form2:

namespace Testt
{

    public partial class Form2 : Form
    {
        private Form1 f;
        public Form2(Form1 f1)
        {
            InitializeComponent();
            f=f1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(f.dimx.ToString());
        }
    }
}
spoder
  • 9
  • 2