创建一个返回值的自定义对话框的最简单方法是什么?

23

我想为我的C#项目创建一个自定义对话框,其中包含一个DataGridView和一个按钮。当用户单击此按钮时,将返回一个整数值给调用方,并使对话框自行终止。

我应该如何实现这个功能?

5个回答

41

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");

代码看起来很好...唯一的问题是当在主函数中调用int promptValue = Prompt.ShowDialog("Test", "123");时,整型值会立即返回。这不是期望的结果。我希望对话框在用户按下按钮时返回该值。然后该按钮也关闭表单.. - Ahmad
6
@Ahmad那就是发生的事情。 - Bas
您也可以这样写:Form prompt = new MyForm1(); prompt.ShowDialog(); return "something"; - mekb

23
  1. 在您的按钮上设置DialogResult属性为DialogResult.OK
  2. 在您的对话框上设置AcceptButton属性为您的按钮
  3. 在您的窗体中创建一个名为Result的公共属性,其类型为int
  4. 在您的按钮的click事件中设置此属性的值
  5. 以这种方式调用您的对话框

    using(myDialog dlg = new myDialog())
    {
        if(dlg.ShowDialog() == DialogResult.OK)
        {
            int result = dlg.Result;
            // whatever you need to do with result
        }
    }
    

using(myDialog dlg = new myDialog()) 语句中,我得到了以下错误:type used in a using statement must be implicitly convertible to 'System.IDisposable'. - nam

3
public static DialogResult InputBox(string title, string promptText, ref string value,bool isDigit=false)
{
    Form form = new Form();
    Label label = new Label();
    TxtProNet textBox = new TxtProNet();

    if (isDigit == true)
        textBox.TypeNumricOnly = true;

    textBox.Width = 1000;
    Button buttonOk = new Button();
    Button buttonCancel = new Button();

    form.Text = title;
    label.Text = promptText;
    textBox.Text = value;

    buttonOk.Text = "OK";
    buttonCancel.Text = "Cancel";
    buttonOk.DialogResult = DialogResult.OK;
    buttonCancel.DialogResult = DialogResult.Cancel;

    label.SetBounds(9, 20, 372, 13);
    textBox.SetBounds(12, 36, 372, 20);
    buttonOk.SetBounds(228, 72, 75, 23);
    buttonCancel.SetBounds(309, 72, 75, 23);

    label.AutoSize = true;
    textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
    buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
    buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

    form.ClientSize = new Size(396, 107);
    form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
    form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
    form.FormBorderStyle = FormBorderStyle.FixedDialog;
    form.StartPosition = FormStartPosition.CenterScreen;
    form.MinimizeBox = false;
    form.MaximizeBox = false;
    form.AcceptButton = buttonOk;
    form.CancelButton = buttonCancel;

    DialogResult dialogResult = form.ShowDialog();
    value = textBox.Text;
    return dialogResult;
}

4
请解释一下,这是什么? - Lrrr

-1
//combo box dialog c#
//
public static string DialogCombo(string text,DataTable comboSource,string DisplyMember,string ValueMember)
    {
        //comboSource = new DataTable();


        Form prompt = new Form();
        prompt.RightToLeft = RightToLeft.Yes;
        prompt.Width = 500;
        prompt.Height = 200;
        Label textLabel = new Label() { Left = 350, Top = 20, Text = text };
        ComboBox combo = new ComboBox { Left = 50, Top = 50, Width = 400 };
        combo.DataSource = comboSource;
        combo.ValueMember = ValueMember;
        combo.DisplayMember = DisplyMember;
        Button confirmation = new Button() { Text = "تایید", Left = 350, Width = 100, Top = 70 };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.Controls.Add(combo);
        prompt.ShowDialog();

        return combo.SelectedValue.ToString();
    }

-1
    public partial class DialogFormDisplay : Form
{
    public DialogFormDisplay()
    {
        InitializeComponent();
    }
    string dialogcode;
    public void Display(string code, string title, string description)
    {
        dialogcode = code;
        switch (code)
        {
            case "YesNo":
                btnLeft.Text = "Yes";
                btnLeft.BackColor = Color.ForestGreen;
                btnLeft.ForeColor = Color.White;
                btnRight.Text = "No";
                btnRight.BackColor = Color.Red;
                btnRight.ForeColor = Color.White;
                break;
            default:
                break;
        }
        displayTitle.Text = title;
        displayMessage.Text = description;
    }

    private void btnLeft_Click(object sender, EventArgs e)
    {
        dialogResultLeft(dialogcode);
    }

    private void btnRight_Click(object sender, EventArgs e)
    {
        dialogResultRight(dialogcode);
    }
    void dialogResultLeft(string code)
    {
        switch (code)
        {
            case "YesNo":
                DialogResult = DialogResult.Yes;
                MessageBox.Show("You pressed " + DialogResult);
                break;
            default:
                break;
        }
    }
    void dialogResultRight(string code)
    {
        switch (code)
        {
            case "YesNo":
                DialogResult = DialogResult.No;
                MessageBox.Show("You pressed " + DialogResult);
                break;
            default:
                break;
        }
    }
}

您可以在https://github.com/gurvirlochab/CustomNotifications上查看此内容。这里是对话框的示例截图


虽然这理论上回答了问题,但最好在此处包含答案的基本部分,并提供参考链接。 - Anton Menshov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接