本帖最后由 稳稳地 于 2025-3-9 22:30 编辑
直接贴代码:复制即可使用- public static class InputDialog
- {
- // 显示输入对话框并返回用户输入的内容
- public static string ShowDialog()
- {
- // 创建一个新的Form作为输入对话框
- Form inputForm = new Form
- {
- Width = 300,
- Height = 100,
- Text = "输入对话框",
- MaximizeBox=false,
- MinimizeBox=false,
- FormBorderStyle= FormBorderStyle.FixedSingle,
- StartPosition= FormStartPosition.CenterScreen
- };
- // 创建一个TextBox供用户输入内容
- TextBox inputTextBox = new TextBox
- {
- Location = new System.Drawing.Point(10, 10),
- Width = 260
- };
- // 创建一个按钮,点击后关闭对话框并返回输入内容
- Button okButton = new Button
- {
- Text = "确定",
- Location = new System.Drawing.Point(90, 35)
- };
- // 定义一个字符串变量来存储用户输入的内容
- string userInput = "";
- // 为按钮的点击事件添加事件处理程序
- okButton.Click += (sender, e) =>
- {
- // 获取用户输入的内容
- userInput = inputTextBox.Text;
- // 关闭对话框
- inputForm.Close();
- };
- // 将TextBox和Button添加到Form中
- inputForm.Controls.Add(inputTextBox);
- inputForm.Controls.Add(okButton);
- // 显示对话框并等待用户输入
- inputForm.ShowDialog();
- // 返回用户输入的内容
- return userInput;
- }
- public static DialogResult Show(out string strText)
- {
- string strTemp = string.Empty;
- FrmInputDialog inputDialog = new FrmInputDialog();
- inputDialog.TextHandler = (str) => { strTemp = str; };
- DialogResult result = inputDialog.ShowDialog();
- strText = strTemp;
- return result;
- }
- }
复制代码 调用方法代码:
- string strcontent = InputDialog.ShowDialog();
- MessageBox.Show($"输入值:==={strcontent}");
复制代码
|