开发者

What's the simplest way to ask for user input through Modal in Win Form

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 s开发者_开发知识库till access the property that way or should I somehow return the value?

Is this a decent way of doing this?


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


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;
 }
}


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
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜