0

I want to sent data from one class to other using method.
e.x.
This is form1

namespace sof
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Form2 f2 = new Form2();
    private void Form1_Load(object sender, EventArgs e)
    {
        f2.Show();
    }

    public void data(string name,bool re)
    {
        label1.Text = name;
        if (re == true)
            label1.BackColor = Color.Lime;
        else
            label1.BackColor = Color.Red;
    }
}

This is Form2

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

    Form1 f1 = new Form1(); 

    private void button1_Click(object sender, EventArgs e)
    {
        f1.data("Alex",true);
    }
}
    }

Now I want from Form2 sent this data(f1.data("Alex",true);) back to Form1 and set label1 tex.



Thanks in advance for your help

BPSK
  • 15
  • 5
  • This answers your question: [Send values from one form to another form](https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) If not, there are [thousands more here](https://www.google.com/search?q=pass+data+to+another+form+c%23+site:stackoverflow.com&rlz=1C1CHFX_enUS460US460&sa=X&ved=2ahUKEwi-vc62surmAhVXHc0KHelCD30QrQIoBDAAegQIBBAN&biw=1179&bih=774) – Ňɏssa Pøngjǣrdenlarp Jan 04 '20 at 16:45

2 Answers2

0

I'm not sure if there's a reason you instantiate a new Form1 inside your Form2, but i would try obtaining a reference to caller Form1 instance and setting a propery on it directly:

public partial class Form1 : Form
{
    Form2 f2; // it's a bit of a catch - you will have to instantiate your f2 in a method to access `this`
    private void Form1_Load(object sender, EventArgs e)
    {
        f2 = new Form2(this);
        f2.Show();
    }
}

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

    private void button1_Click(object sender, EventArgs e)
    {
        f1.data("Alex", true);
    }
}

If for whatever reason your data() is not public, passing a delegate could work:

public partial class Form1 : Form
{
    Form2 f2; // it's a bit of a catch - you will have to instantiate your f2 in a method to access non-static class members
    private void Form1_Load(object sender, EventArgs e)
    {
        f2 = new Form2((name, re) => data(name, re)); // so you pass your delegate here
        f2 = new Form2(data); // this will also work given your member signature match
        f2.Show();
    }

    public void data(string name, bool re)
    {

    }
}

public partial class Form2 : Form
{
    private Action<string, bool> _setData;

    public Form2(Action<string, bool> setData)
    {       
        _setData = setData;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _setData("Alex", true);
    }
}
timur
  • 14,239
  • 2
  • 11
  • 32
0

Instead of creating a new instance of Form1 inside Form2, you need to pass a reference to Form1 into the constructor of Form2. Then use this reference to call the Form1.data() method in your button click event handler.

So your code would look like this.

Form1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

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

    public void data(string name,bool re)
    {
        label1.Text = name;
        if (re == true)
            label1.BackColor = Color.Lime;
        else
            label1.BackColor = Color.Red;
    }
}

Form2

public partial class Form2 : Form
{
    Form1 f1;

    public Form2(Form1 form1)
    {
        f1 = form1;
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f1.data("Alex",true);
    }       
}
Paul
  • 597
  • 2
  • 5