要创建一个返回值的自定义对话框最简单的方法? [英] Easiest way to create a custom dialog box which returns a value?

查看:144
本文介绍了要创建一个返回值的自定义对话框最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义对话框,我的C#项目。我想在这个自定义对话框一个DataGridView,而且也将有一个按钮。当用户点击这个按钮,将返回一个整数值调用者和对话框,然后自行终止。

I want to create a custom dialog box for my C# project .. I want to have a DataGridView in this custom dialog box, and there will also be a button .. When the user clicks this button, an integer value is returned to the caller, and the dialog box then terminates itself ..

我怎样才能做到这一点?

How can I achieve this ?

推荐答案

有在C#中没有任何提示对话框。您可以创建自定义提示框,要做到这一点,而不是。

There is no prompt dialog box in C#. You can create a custom prompt box to do this instead.

  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;
        }
    }

然后使用调用它:

Then call it using:

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

这篇关于要创建一个返回值的自定义对话框最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆