这是一个 C# 输入对话框函数,主要功能包括:
基础功能:创建带标题、提示文本和输入框的对话框,返回用户输入或空字符串(取消时)
增强特性:
- 支持必填项验证
- 支持正则表达式格式验证
- 实时错误提示与按钮状态更新
- 自定义验证失败消息
- 自动资源管理(using 语句)
用户体验:
- Enter/Esc 快捷键支持
- 输入有效性实时反馈
- 提交时强制格式检查
代码质量:
- 模块化验证逻辑
- 事件驱动设计
- 安全的资源释放机制
/// <summary>
/// 创建一个可配置的输入对话框,提示用户输入文本并返回结果
/// </summary>
/// <param name="prompt">显示给用户的提示文本</param>
/// <param name="title">对话框的标题(可选,默认为空)</param>
/// <param name="defaultValue">输入框的默认值(可选,默认为空)</param>
/// <param name="isRequired">是否为必填项(可选,默认否)</param>
/// <param name="validationPattern">正则验证模式(可选,默认不验证)</param>
/// <param name="validationMessage">验证失败时的提示(可选)</param>
/// <returns>用户输入的文本,如果用户取消则返回空字符串</returns>
public static string InputBox(
string prompt,
string title = "",
string defaultValue = "",
bool isRequired = false,
string validationPattern = "",
string validationMessage = "输入格式不正确")
{
using (Form form = new Form())
using (Label label = new Label())
using (TextBox textBox = new TextBox())
using (Button buttonOk = new Button())
using (Button buttonCancel = new Button())
using (ErrorProvider errorProvider = new ErrorProvider())
{
// 配置窗体属性
form.Text = title;
form.ClientSize = new Size(350, 160);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
form.KeyPreview = true;
form.FormClosing += (sender, e) => {
if (form.DialogResult == DialogResult.OK && !IsValidInput(textBox.Text))
{
e.Cancel = true;
MessageBox.Show(validationMessage, "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
};
// 配置提示标签
label.Text = prompt;
label.Location = new Point(15, 20);
label.AutoSize = true;
// 配置文本输入框
textBox.Text = defaultValue;
textBox.Location = new Point(15, 85);
textBox.Size = new Size(310, 20);
textBox.TextChanged += (sender, e) => {
errorProvider.Clear();
if (isRequired && string.IsNullOrWhiteSpace(textBox.Text))
{
errorProvider.SetError(textBox, "此字段为必填项");
}
};
// 配置确定按钮
buttonOk.Text = "确定";
buttonOk.Location = new Point(100, 120);
buttonOk.DialogResult = DialogResult.OK;
buttonOk.Enabled = !isRequired || !string.IsNullOrWhiteSpace(defaultValue);
// 配置取消按钮
buttonCancel.Text = "取消";
buttonCancel.Location = new Point(180, 120);
buttonCancel.DialogResult = DialogResult.Cancel;
// 添加控件到窗体
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
// 输入验证方法
bool IsValidInput(string input)
{
if (isRequired && string.IsNullOrWhiteSpace(input))
return false;
if (!string.IsNullOrEmpty(validationPattern) &&
!System.Text.RegularExpressions.Regex.IsMatch(input, validationPattern))
return false;
return true;
}
// 启用实时验证
textBox.TextChanged += (sender, e) => {
buttonOk.Enabled = IsValidInput(textBox.Text);
};
// 显示对话框并返回结果
return form.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
}
示例调用:
// 简单文本输入
string name = InputBox("请输入姓名");
// 带验证的邮箱输入
string email = InputBox("邮箱", "注册", isRequired:true,
validationPattern:@"^[\w-]+@[\w-]+\.[a-z]{2,}$");