tags:

views:

50

answers:

3

What is the simplest way to implement a Modal(popup) MessageBox that allows a custom value to be entered and returned. In my case, a String.

Maybe I am over thinking this but I figured I'd ask. I plan to just create a new Form. Add a label, a textbox, two buttons. Assign the textbox to a property and from my main form call a ShowDialog() on it.

Will I be able to still access the property that way or should I somehow return the value?

Is this a decent way of doing this?

+2  A: 

It sounds like a decent way to go except for exposing the TextBox as a property. You should only need to expose TextBox.Text.

Austin Salonen
+1  A: 

You will still be able to access the form's properties after the form has closed as long as your form variable is still in scope on the main form.

You could do something like this:

    frmPrompt frm = new frmPrompt();

    if ( frm.ShowDialog() == DialogResult.OK )
    {
        string result = frm.SomeProperty
    }
TLiebe
A using statement is required here to dispose the form.
Hans Passant
Yes, the form needs to be properly disposed of - either by enclosing it in a Using statement or disposing of it manually with frm.Dispose(). I didn't really think it was critical to the question being asked.
TLiebe
+1  A: 

Yes I actually do this, I made an input form that contains exactly what you said.

Lets call your property InputValue

using (ModalInputForm inputForm = new ModalInputForm()) {
 if (inputForm.ShowDialog == DialogResult.Ok) {
  _fieldToUse = inputForm.InputValue;
 }
}
msarchet
You *have* to pay attention to the return value of ShowDialog().
Hans Passant
@nobugz this is very valid. Edited my post.
msarchet