NAWAF WABS
NAWAF WABS

Reputation: 27

Getting a value from second Form using C#

I want to get value from second (form2 to form1) automatically or by press button on second form but i cant

im able to get value from form1 to form2 but no form2 to form1

i try this one

private void button1_Click(object sender, EventArgs e)
{               
    Form1 ytr = new Form1();

    ytr.totalcost.Text = textBox3.Text;
}

not working

UPDATE ( code on form 2 )

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

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            multpl();
        }



        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            multpl();
        }

        private void multpl()
        {
            int a, b;

            bool isAValid = int.TryParse(textBox1.Text, out a);
            bool isBValid = int.TryParse(textBox2.Text, out b);

            if (isAValid && isBValid)
                textBox3.Text = (a * b).ToString();

            else
                textBox3.Text = "Invalid input";


        }

        private Form1 form1Instance { get; set; }

        public Form2 (Form1 form1)
        {
            this.form1Instance = form1;
        }



        private void button1_Click(object sender, EventArgs e)
        {



            this.form1Instance.totalcost.Text = textBox3.Text;


        }


    }
}

Error:Object reference not set to an instance of an object.

Upvotes: 0

Views: 116

Answers (2)

David
David

Reputation: 218827

You're creating a new instance of Form1 and never showing it. Instead, you want to set the value on the instance of Form1 that you already have.

Presumably Form1 creates and shows an instance of Form2, yes? Since Form2 is expecting to interact back with Form1, it should require a reference to it. Add that requirement in your Form2 constructor. Something like this:

private Form1 form1Instance { get; set; }

public Form2(Form1 form1)
{
    this.form1Instance = form1;
    InitializeComponent();
}

That way it's required when creating an instance of Form2. So in Form1 when you create that instance, you can pass a reference to itself:

var form2 = new Form2(this);
form2.Show();

Then later in your click handler you can reference the instance that you're holding in that property:

private void button1_Click(object sender, EventArgs e)
{
    this.form1Instance.totalcost.Text = textBox3.Text;
}

From here you can even continue to refactor the functionality into custom simple classes instead of passing around form references. There are a variety of approaches you could take, including custom events or messages of some kind. But ultimately you'll need to refer to the existing instance of Form1 in order to change anything on that instance, not create a new one.

Upvotes: 1

Daniel B
Daniel B

Reputation: 3185

The best way to approach this is using delegate:

in Form2:

public Action<int> OnSetTotalCost;
private void button1_Click(object sender, EventArgs e)
{
  Form1 ytr = new Form1();
  var talCost = int.parse(textBox3.Text);
  if(OnSetTotalCost != null) OnSetTotalCost(talCost);
}

and in Form1 class

var form2 = new Form2(this); // u dont need to pass form1
form2.OnSetTotalCost = SetTotalCost;
form2.Show();

and u can define SetTotalCost as:

private void SetTotalCost(int totalCost)
{
  txtTotalcost.Text = totalCost.ToSttring();
}

Upvotes: 0

Related Questions