简体   繁体   中英

How to not loose the parameter when instancing new form

I have my Code like this:

    private void button3_Click(object sender, EventArgs e)
    {
        schema s1 = new schema(readedImage);
        s1.ShowDialog();
        if(s1.imgToReturn != null)
        {
            readedImage = s1.imgToReturn;
            s1.imgToReturn = null;
        }
        s1.Dispose();
    }

Because I am creating a new instance, i dont get the "readedImage" in the new form when calling the .ShowDialog() later. Is there a way to give the parameters in the .ShowDialog Action?

edit: this is the constructor of the shema form:

    public schema(Image readedImage = null)
    {
        InitializeComponent();
        imgToReturn = readedImage;
    }

Greetings and thanks for help

I am not too sure what you mean in the question.

But I think this might help you

private void button3_Click(object sender, EventArgs e)
{
    schema s1 = new schema(readedImage);
    if(s1.ShowDialog() == DialogResult.OK)
    {
        if(s1.imgToReturn != null)
        {
            readedImage = s1.imgToReturn;
            s1.imgToReturn = null;
        }
        s1.Dispose();
    }
}

And in the dialog you are showing make sure to set the property DialogResult once the user is done and everything has worked out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM