using System; using System.Globalization; using System.Windows.Forms; namespace TemuKJD.UserComponent; public partial class InputDialog : Form { private readonly InputDialogType inputType; private readonly Func validator; /// 用户输入的内容(点确定后有效) public string InputText { get; private set; } = string.Empty; public InputDialog() : this(string.Empty) { } /// 输入类型,数字按当前区域格式验证。 /// 类型验证通过后调用;返回错误信息,null 或空字符串表示通过。 public InputDialog(string prompt, string title = "输入", string defaultValue = "", InputDialogType inputType = InputDialogType.Text, Func validator = null) { if (!Enum.IsDefined(typeof(InputDialogType), inputType)) throw new ArgumentOutOfRangeException(nameof(inputType)); InitializeComponent(); this.inputType = inputType; this.validator = validator; Text = title; lblPrompt.Text = prompt; txtInput.Text = defaultValue; txtInput.SelectAll(); } private void btnOk_Click(object sender, EventArgs e) { string value = txtInput.Text; string error = null; if (inputType == InputDialogType.Integer && !int.TryParse(value, out _)) error = "请输入有效的整数(-2147483648 至 2147483647)。"; else if (inputType == InputDialogType.Decimal && !decimal.TryParse(value, out _)) error = "请输入有效的小数。"; else if (validator != null) { try { error = validator(value); } catch (Exception ex) { error = "验证失败:" + ex.Message; } } lblError.Text = error ?? string.Empty; if (!string.IsNullOrEmpty(error)) { txtInput.Focus(); return; } InputText = value; DialogResult = DialogResult.OK; Close(); } private void txtInput_TextChanged(object sender, EventArgs e) { lblError.Text = string.Empty; } /// 弹出输入框并返回输入内容,取消返回 null;validator 返回错误信息,空值表示通过。 public static string Show(string prompt, string title = "输入", string defaultValue = "", InputDialogType inputType = InputDialogType.Text, Func validator = null) { using (var dlg = new InputDialog(prompt, title, defaultValue, inputType, validator)) { return dlg.ShowDialog() == DialogResult.OK ? dlg.InputText : null; } } /// 输入数值,取消返回 null;支持整数、float、double 和 decimal,按当前区域格式解析。 /// 接收解析后的数值,返回错误信息;null 或空字符串表示通过。 public static T? Show(string prompt, string title = "输入", T defaultValue = default, Func validator = null) where T : struct, IConvertible { var typeCode = Type.GetTypeCode(typeof(T)); if (typeof(T).IsEnum || typeCode < TypeCode.SByte || typeCode > TypeCode.Decimal) throw new NotSupportedException("仅支持整数、float、double 和 decimal 类型。"); T value = default; using (var dlg = new InputDialog(prompt, title, defaultValue.ToString(CultureInfo.CurrentCulture), validator: text => ValidateNumber(text, validator, out value))) { return dlg.ShowDialog() == DialogResult.OK ? value : (T?)null; } } private static string ValidateNumber(string text, Func validator, out T value) where T : struct, IConvertible { value = default; try { value = (T)Convert.ChangeType(text, typeof(T), CultureInfo.CurrentCulture); } catch (FormatException) { return $"请输入有效的 {typeof(T).Name} 数值。"; } catch (OverflowException) { return $"输入超出 {typeof(T).Name} 的取值范围。"; } if (value is float single && (float.IsNaN(single) || float.IsInfinity(single)) || value is double number && (double.IsNaN(number) || double.IsInfinity(number))) return "请输入有限数值。"; return validator?.Invoke(value); } } public enum InputDialogType { Text, Integer, Decimal }