Can I add controls to the C# MessageBox?

南笙酒味 提交于 2019-12-22 08:13:00

问题


Could I add some custom control to the standard Message Box for read input value, for example text fields for user name and password, or I should create custom winform with "Ok,Cancel" buttons and text fields?

Related: Which control to use for quick text input (inputbox)?


回答1:


Create your own.

Creating a custom modal (or otherwise) input dialog isn't all that difficult and you can built the extensibility you need for reuse.

public class ValueHolder {
    public string SomeInput { get; set; }
    public DialogResult Result { get; set; }
}

public class GimmeValues : Form {
    //... HAS A TEXTBOX and Okay Buttons...

    private GimmeValues() {        
        okButton.DialogResult = DialogResult.OK;
        cancelButton.DialogResult = DialogResult.Cancel;
        // ... other stuff
    }

    public static ValueHolder GetInput(IWin32Window owner) {
        using (GimmeValues values = new GimmeValues()) {
            DialogResult result = values.ShowDialog(owner);
            return new ValueHolder { 
                SomeInput = values.Textbox1.Text,
                Result = result
            };
        }
    }
}

Okay I just wrote that all in this editor so forgive any syntax mistakes.
You could do something like the above but clean it up a little, add the extensibility you need (in terms of buttons and inputs showing that you need etc)... then just call it like ValueHolder value = GimmeValues.GetInput(this); where this would represent an IWin32Window...

The resulting value of value would be the selected nonsense and you could perform your logic..

if(value.Result == DialogResult.OK && !string.IsNullOrEmpty(value.SomeInput)){
    //TODO: Place logic....
}



回答2:


you can use the Interaction.InputBox method wich is located in the Microsoft.VisualBasic namespace

try this

 Microsoft.VisualBasic.Interaction.InputBox("Enter a Value Here", "Title", "Your Default Text",200,100);



回答3:


You will need to create a custom WinForm to do this. You can make it work the same way as a MessageBox by returning a DialogResult on the Show method.




回答4:


You'll have to create a custom form to handle that.

If you want the Form to behave like MessageBox, just create a static Show() method on your Form that creates an instance and shows the box to the user. That static method can also handle returning the values you are interested in from your custom form (much like DialogResult).



来源:https://stackoverflow.com/questions/3892570/can-i-add-controls-to-the-c-sharp-messagebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!