C#messageboxbutton里面加textbox
时间: 2023-10-19 07:26:37 浏览: 43
在C#中,可以通过自定义对话框来实现在MessageBoxButton中添加TextBox。首先,你需要创建一个自定义窗口,该窗口包含一个TextBox和MessageBoxButton。接下来,你可以在单击对话框按钮时获取TextBox的值,并根据需要执行操作。以下是示例代码:
```csharp
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 150;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top=20, Text=text };
TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=75 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.ShowDialog();
return textBox.Text;
}
```
你可以使用以下代码调用此自定义对话框:
```csharp
string result = ShowDialog("Please enter some text:", "Custom Dialog");
MessageBox.Show(result);
```
这将显示一个自定义对话框,其中包含一个TextBox和一个Ok按钮。用户输入文本后,单击Ok按钮将关闭对话框并返回TextBox的值。你可以像使用普通的MessageBox一样使用这个自定义对话框,并且可以在需要时对其进行修改以满足你的需求。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![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)