Ahm*_*mad 18 c# dialog winforms customdialog
我想为我的C#项目创建一个自定义对话框..我想在这个自定义对话框中有一个DataGridView,并且还会有一个按钮..当用户单击此按钮时,会向调用者返回一个整数值,然后对话框自行终止..
我怎样才能做到这一点?
Bas*_*Bas 35
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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后用以下方法调用:
int promptValue = Prompt.ShowDialog("Test", "123");
Run Code Online (Sandbox Code Playgroud)
Ste*_*eve 18
以这种方式调用您的对话框
using(myDialog dlg = new myDialog())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
int result = dlg.Result;
// whatever you need to do with result
}
}
Run Code Online (Sandbox Code Playgroud)