定义一个messageboxbutton,弹出框可以输入信息,根据输入信息再弹出messagebox
时间: 2024-02-23 22:02:35 浏览: 26
在C#中,可以通过自定义MessageBoxButton来实现弹出框可以输入信息,并根据输入信息再弹出MessageBox。以下是示例代码:
```csharp
public static DialogResult ShowInputDialog(ref string input)
{
Form inputBox = new Form();
inputBox.Width = 300;
inputBox.Height = 150;
inputBox.Text = "Input Box";
Label inputLabel = new Label() { Left = 50, Top = 20, Text = "Please enter your input:" };
TextBox inputTextBox = new TextBox() { Left = 50, Top = 50, Width = 200, Text = input };
Button okButton = new Button() { Text = "Ok", Left = 150, Width = 50, Top = 75 };
okButton.Click += (sender, e) => { input = inputTextBox.Text; inputBox.DialogResult = DialogResult.OK; };
inputBox.Controls.Add(inputLabel);
inputBox.Controls.Add(inputTextBox);
inputBox.Controls.Add(okButton);
DialogResult result = inputBox.ShowDialog();
return result;
}
```
你可以使用以下代码调用此自定义MessageBoxButton:
```csharp
string input = "";
DialogResult dialogResult = ShowInputDialog(ref input);
if (dialogResult == DialogResult.OK)
{
MessageBox.Show("You entered: " + input);
}
```
这将显示一个弹出框,其中包含一个TextBox和一个Ok按钮。用户输入文本后,单击Ok按钮将关闭对话框并将输入文本作为参数传递给调用方。调用方可以在需要时使用此输入文本执行操作,并在必要时弹出另一个MessageBox。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)