Friday 10 June 2016

c# pass an object from form1 to form2 and back to form1

take a data object instantiated on form1 and pass that object a form2. Work on the data object in form2 and then pass that data back to form1

objects are past by reference between forms

----------------------------------------------------------------------------
//outline overview

 public partial class YourSecondForm : Form
{
    object PreserveFromFirstForm;

    public YourSecondForm()
    {
       ... its default Constructor...
    }

    public YourSecondForm( object ParmFromFirstForm ) : this()
    {
       this.PreserveFromFirstForm = ParmFromFirstForm;
    }

    private void YourSecondFormMethodToManipulate()
    {
       // you would obviously have to type-cast the object as needed
       // but could manipulate whatever you needed for the duration of the second form.
       this.PreserveFromFirstForm.Whatever = "something";
    }


}

http://stackoverflow.com/questions/4887820/how-do-you-pass-an-object-from-form1-to-form2-and-back-to-form1

-------------------------------------------------------------------------------------------
//example

public partial class Form1 : Form
    {    
        IStudentManager Manager =
            ManagerFactory.GetManager(ConfigurationManager.AppSettings["PersistanceType"]);

        public Form1()
        {
            InitializeComponent();

            uxStudents.DataSource = Manager.GetAll();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Manager.Save();
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Object Manager in Form 1 is passed to another form by reference
            var frm = new AddStudentForm(Manager);

            frm.ShowDialog();
         
            //To add to this: from the first form, you would create an instance of the second form and then show it using mySecondForm.ShowDialog(). Since you're passing the parameter object by reference, you can change it however you like in the second form, and those changes will remain in the object when the ShowDialog() call returns.
 
            //AddStudentForm modified data on reference object Manager in Form1
            //refresh the grid with any new students added above.
            uxStudents.DataSource = null;
            uxStudents.DataSource = Manager.GetAll();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            var result = MessageBox.Show("Are you sure you want to leave this wonderful form?", "Form Closing Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(result == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }

---------------------------------------------------------------------------------------------

public partial class AddStudentForm : Form
    {
        //Property on AddStudentForm refer to object Manager in form1.
        IStudentManager Manager { get; set; }

        public AddStudentForm(IStudentManager manager)
        {
            InitializeComponent();
            Manager = manager;
        }

        private void uxOK_Click(object sender, EventArgs e)
        {
            Student std = new Student();
            std.Id = int.Parse(uxID.Text);
            std.FirstName = uxFirstName.Text;
            std.LastName = uxLastName.Text;
            std.RegistrationDate = uxRegisterDate.Value;

            //modifying object Manager on form1
            Manager.Add(std);

            ClearAll();
        }

        private void ClearAll()
        {
            foreach(var ctrl in this.Controls)
            {
                if(ctrl is TextBox)
                {
                    ((TextBox)ctrl).Clear();
                }
            }
            uxID.Focus();
        }

    }

No comments:

Post a Comment