c# - 创建返回值的自定义对话框的最简单方法?

标签 c# winforms dialog customdialog

我想为我的 C# 项目创建一个自定义对话框。我想在这个自定义对话框中有一个DataGridView,并且还会有一个按钮。当用户单击此按钮时,一个整数值将返回给调用者,然后对话框自行终止。

我怎样才能做到这一点?

最佳答案

C#中没有提示对话框。您可以创建自定义提示框来执行此操作。

  public static class Prompt
    {
        public static int ShowDialog(string text, string caption)
        {
            Form prompt = new Form();
            prompt.Width = 500;
            prompt.Height = 100;
            prompt.Text = caption;
            Label textLabel = new Label() { Left = 50, Top=20, Text=text };
            NumericUpDown inputBox = new NumericUpDown () { Left = 50, Top=50, Width=400 };
            Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 };
            confirmation.Click += (sender, e) => { prompt.Close(); };
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(inputBox);
            prompt.ShowDialog();
            return (int)inputBox.Value;
        }
    }

然后调用它:

 int promptValue = Prompt.ShowDialog("Test", "123");

关于c# - 创建返回值的自定义对话框的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9569489/

相关文章:

c# - 实例化委托(delegate) (Func<T, T>) 的各种方式之间的区别?

带有淡入/淡出和加载问题的 Jquery 对话框

windows - 在 Ruby 中使用 Windows 窗体

C# Windows 窗体倒计时器

css - 如何在 Material-UI 中使图像稍微偏离对话框

c# - 我怎样才能知道元素是否不可见

c# - 以编程方式将天 (1-31) 绑定(bind)到下拉列表,并将 0 绑定(bind)到天 < 10?

c# - 什么是 .axd 文件?

c# - 是否可以从 Excel 电子表格 (VBA) 中获取 WinForms "Button"?

c# - 当 FormBorderStyle 为 None 时,如何以编程方式显示窗口的系统菜单?