c00000fd
c00000fd

Reputation: 22275

Return custom class object from a WinForm derived class

Say, if I have my own data class defined as such:

public class MyData
{
    public string v1;
    public int v2;
    //etc.
}

I can pass it to my own Form-derived class and use it to initialize the form controls:

public partial class MyForm : Form
{
    private MyData dataIn;

    public MyForm(MyData data)
    {
        InitializeComponent();

        dataIn = data;
    }
}

private void MyForm_Load(object sender, EventArgs e)
{
    //Init controls in 'MyForm' from 'dataIn'
}

private void buttonSave_Click(object sender, EventArgs e)
{
    //Save button was clicked

    //Read data from 'MyForm', but
    //how to return it back in 'MyData' class?

    //Close form
    this.DialogResult = System.Windows.Forms.DialogResult.OK;
    this.Close();
}

And the form itself is called as such:

//Initial data for the form
MyData data = new MyData(){
    v1 = "test",
    v2 = 123
};

MyForm dlg = new MyForm(data);
if (dlg.ShowDialog() == DialogResult.OK)
{
    //User clicked Save -- how to get data back in 'MyData'?
}

The issue is how to have my MyData to be filled with data and returned from MyForm?

Upvotes: 0

Views: 1084

Answers (2)

Corey
Corey

Reputation: 16574

You should provide a method for accessing the result data outside the form, either a property you can read or a method you can call from elsewhere:

public partial class MyForm : Form
{
    private MyData dataIn;
    // Option 1: property that can be read from another class
    public MyData result { get; private set; }

    // Option 2: method you can call to get the result object
    public MyData GetResult()
    {
        return result;
    }

    public MyForm(MyData data)
    {
        InitializeComponent();

        dataIn = data;
    }

    private void buttonSave_Click(object sender, EventArgs e)
    {
        // set result object
        result = new MyData { v1 = "some string value", v2 = 123 }

        this.DialogResult = System.Windows.Forms.DialogResult.OK;
        this.Close();
    }
}

Then your caller does something similar to:

MyData dlgResult = null;
using (MyForm dlg = new MyForm(data))
{
    if (dlg.ShowDialog() == DialogResult.OK)
        dlgResult = dlg.GetResult();
}
if (dlgResult != null)
{
    // do something with the result
    //....
}

Note that Form.Close() does not dispose the form. Until it is disposed, you can still perform operations on it. The using (MyForm... syntax ensures that the form is correctly disposed once you're finished with it.

Upvotes: 1

IDeveloper
IDeveloper

Reputation: 1299

Create property of type MyData in MyForm:

public partial class MyForm : Form
{
    private MyData dataIn;
    public MyData dataOut {get; set;}

    public MyForm(MyData data)
    {
        InitializeComponent();

        dataIn = data;
        dataOut = new MyData { v1 = "hi!", v2 = 2013 }; 
    }
}

Then you can read it:

MyForm dlg = new MyForm(data);
if (dlg.ShowDialog() == DialogResult.OK)
{
    MyData dataFromForm = dlg.dataOut;
}

Upvotes: 0

Related Questions