我想为我的 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/