using System; using System.Drawing; using System.Windows.Forms; namespace TemuKJD.UserComponent; public class InputDialog : Form { private TextBox txtInput; private Button btnOk; private Button btnCancel; /// 用户输入的内容(点确定后有效) public string InputText { get; private set; } = string.Empty; public InputDialog(string prompt, string title = "输入", string defaultValue = "") { // 窗体基本设置 this.Text = title; this.FormBorderStyle = FormBorderStyle.FixedDialog; this.StartPosition = FormStartPosition.CenterParent; this.MaximizeBox = false; this.MinimizeBox = false; this.ClientSize = new Size(360, 130); // 提示标签 var lblPrompt = new Label { Text = prompt, Location = new Point(15, 15), AutoSize = true, MaximumSize = new Size(330, 0) }; // 输入框 txtInput = new TextBox { Text = defaultValue, Location = new Point(15, 45), Width = 330 }; txtInput.SelectAll(); // 确定按钮 btnOk = new Button { Text = "确定", DialogResult = DialogResult.OK, Location = new Point(185, 85), Width = 75 }; btnOk.Click += (s, e) => { InputText = txtInput.Text; this.DialogResult = DialogResult.OK; this.Close(); }; // 取消按钮 btnCancel = new Button { Text = "取消", DialogResult = DialogResult.Cancel, Location = new Point(270, 85), Width = 75 }; this.Controls.AddRange(new Control[] { lblPrompt, txtInput, btnOk, btnCancel }); this.AcceptButton = btnOk; this.CancelButton = btnCancel; } /// 便捷静态方法:弹出输入框并返回输入内容,取消返回 null public static string Show(string prompt, string title = "输入", string defaultValue = "") { using (var dlg = new InputDialog(prompt, title, defaultValue)) { return dlg.ShowDialog() == DialogResult.OK ? dlg.InputText : null; } } }