c#实现messagebox带输入的弹窗
时间: 2024-01-19 14:03:26 浏览: 69
你可以使用C#中的InputBox类来实现带输入的MessageBox弹窗,这个类需要自己编写实现,以下是一个简单的实现示例:
```csharp
using System;
using System.Windows.Forms;
public static class InputBox
{
public static string Show(string prompt, string title = "", string defaultValue = "")
{
Form form = new Form();
Label promptLabel = new Label();
TextBox textBox = new TextBox();
Button okButton = new Button();
Button cancelButton = new Button();
form.Text = title;
promptLabel.Text = prompt;
textBox.Text = defaultValue;
okButton.Text = "OK";
cancelButton.Text = "Cancel";
okButton.DialogResult = DialogResult.OK;
cancelButton.DialogResult = DialogResult.Cancel;
promptLabel.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
okButton.SetBounds(228, 72, 75, 23);
cancelButton.SetBounds(309, 72, 75, 23);
promptLabel.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
okButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
cancelButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new System.Drawing.Size(396, 107);
form.Controls.AddRange(new Control[] { promptLabel, textBox, okButton, cancelButton });
form.ClientSize = new System.Drawing.Size(Math.Max(300, promptLabel.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = okButton;
form.CancelButton = cancelButton;
DialogResult dialogResult = form.ShowDialog();
string result = textBox.Text;
return dialogResult == DialogResult.OK ? result : "";
}
}
```
使用方法:
```csharp
string input = InputBox.Show("请输入内容:", "标题", "默认值");
if (!string.IsNullOrEmpty(input))
{
MessageBox.Show("您输入的内容是:" + input);
}
```