How can I maked this approach's opposite

Socarsky

Well-known member
Joined
Mar 3, 2014
Messages
59
Programming Experience
Beginner
How can I passing values from a Form to another one directly? A receiver Form will be showing itself on screen and listening passing values which come from a main form.

I know a way to do with delegate and event but I need its opposite way. Here is what I can do these code lines. This able to do only Form2 passes value to Form1 (main form). I need this approach's opposite. So, Form1 will be sender Form2 will be listener.

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.IdentityUpdated += new Form2.IdentityUpdateHandler(Form2_ButtonClicked);
            f.Show();
        }

        private void Form2_ButtonClicked(object sender, IdentityUpdateEventArgs e)
        {
            textBox1.Text = e.FirstName;
        }
    }


public partial class Form2 : Form
    {
        public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);
        public event IdentityUpdateHandler IdentityUpdated;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            string sFirstName = txtFirstName.Text;
            IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(sFirstName);
            IdentityUpdated(this, args);
        }
    }

    public class IdentityUpdateEventArgs : System.EventArgs
    {
        private string mFirstName;
        public IdentityUpdateEventArgs(string sFirstName)
        {
            this.mFirstName = sFirstName;
        }

        public string FirstName
        {
            get    { return mFirstName; }
        }
    }
 
Top Bottom