简体   繁体   English

我可以在C#MessageBox中添加控件吗?

[英]Can I add controls to the C# MessageBox?

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? 我可以为标准消息框添加一些自定义控件以获取读取输入值,例如用户名和密码的文本字段,或者我应该使用“确定,取消”按钮和文本字段创建自定义winform?

Related: Which control to use for quick text input (inputbox)? 相关: 哪个控件用于快速文本输入(输入框)?

you can use the Interaction.InputBox method wich is located in the Microsoft.VisualBasic namespace 您可以使用位于Microsoft.VisualBasic命名空间中的Interaction.InputBox方法

try this 尝试这个

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

You will need to create a custom WinForm to do this. 您需要创建一个自定义WinForm来执行此操作。 You can make it work the same way as a MessageBox by returning a DialogResult on the Show method. 您可以通过在Show方法上返回DialogResult使其工作方式与MessageBox相同。

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); 您可以执行类似上面的操作但稍微清理一下,添加您需要的可扩展性(根据按钮和输入显示您需要等)...然后只需将其称为ValueHolder value = GimmeValues.GetInput(this); where this would represent an IWin32Window ... this将代表IWin32Window ......

The resulting value of value would be the selected nonsense and you could perform your logic.. 得到的值的value将是选定的废话,你可以执行你的逻辑..

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

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. 如果您希望Form的行为与MessageBox类似,只需在Form上创建一个静态Show()方法,该方法创建一个实例并向用户显示该框。 That static method can also handle returning the values you are interested in from your custom form (much like DialogResult). 该静态方法还可以处理从您的自定义表单返回您感兴趣的值(很像DialogResult)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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