what is the cleanest way to create a form that returns a value?

  • Thread starter Thread starter Matteo Cima
  • Start date Start date
M

Matteo Cima

Hi,
i need to show a modal form with a list of choices and when the user hits
ok, the form should close and return the selected value. I tried in many
ways but i can't say it's "elegant" enough... any ideas?

in the form (frmList) i create:
public string ShowDialogFormAndReturnValue()
{
// do some stuff
this.showdialog();
return iReturn;
}

iReturn is a private form's variable that is valorized when the user selects
the item...


when i want to use it i do:

frmList myForm = new frmList();
string myReturnValue = myForm.ShowDialogAndReturnValue();


where should i close the form?
i'd love to have the form close itself when returning the result... but
can't get that!

now i have to add myForm.Dispose() after the previous code...


any ideas?

regards,
Matteo.
 
Matteo said:
Hi,
i need to show a modal form with a list of choices and when the user hits
ok, the form should close and return the selected value. I tried in many
ways but i can't say it's "elegant" enough... any ideas?

in the form (frmList) i create:
public string ShowDialogFormAndReturnValue()
{
// do some stuff
this.showdialog();
return iReturn;
}

iReturn is a private form's variable that is valorized when the user selects
the item...


when i want to use it i do:

frmList myForm = new frmList();
string myReturnValue = myForm.ShowDialogAndReturnValue();


where should i close the form?
i'd love to have the form close itself when returning the result... but
can't get that!

now i have to add myForm.Dispose() after the previous code...

You probably also want to handle OK and Cancel, which is what ShowDialog
would return. I'd just create a public property and access that, ie:

if(myForm.ShowDialog == DialogResult.OK)
{
string value = myForm.SelectedValue;

// now use the value
}


--

..o0O0o.__.o0O0o.__.o0O0o.__.o0O0o.__.o0O0o.__.o0O0o.__.o0O0o.
| Matt Kunze Sometimes there's a point.|
| Build Master Fooly Fool This is not one of those |
| DataSplice Software Developer times. |
=============================================================
 
Thank you for your answer!
Yes, but i want the form dispose itself... so i can't access its properties
like myForm.SelectedValue.

Matteo
 
That's a clean way!
but it gives an error 'cause the form doesn't inherit System.IDispsable

the error is: "Cannot implicitly convert type 'mynamespace.myform' to
'System.IDisposable'

how to add IDisposable to a form?

Cheers,
Matteo.
 
Thank you!
i just didn't remember of that little, cute, coma to separate inherited
classes! :-)

Now it works in a breeze!
Cheers,
Matteo.
 
Just caught myself when I said put your inheritance
declaration in the constructor... ooops, but you got it
figured out as I was sure you would. I picked a bad week
to give up sniffing glue I guess.

Cheers, Ian
 
Back
Top