Easiest way to create a custom dialog box which returns a value?
Asked Answered
M

5

24

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?

Misdate answered 5/3, 2012 at 15:41 Comment(0)
W
42

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");
Will answered 5/3, 2012 at 15:45 Comment(3)
code looks fine .. The only problem is the int value would be returned as soon as int promptValue = Prompt.ShowDialog("Test", "123"); is called in the main function. This is not desired. I want the dialog to return the value when the user presses the button. And then the button also closes the form ..Misdate
@Misdate that's what happens.Will
You could also go: Form prompt = new MyForm1(); prompt.ShowDialog(); return "something";Dexterdexterity
P
24
  1. On your button set the DialogResult property to DialogResult.OK
  2. On your dialog set the AcceptButton property to your button
  3. Create a public property in your form called Result of int type
  4. Set the value of this property in the click event of your button
  5. Call your dialog in this way

    using(myDialog dlg = new myDialog())
    {
        if(dlg.ShowDialog() == DialogResult.OK)
        {
            int result = dlg.Result;
            // whatever you need to do with result
        }
    }
    
Pearsall answered 5/3, 2012 at 15:59 Comment(1)
On the using(myDialog dlg = new myDialog()) statement, I'm getting the following error: type used in a using statement must be implicitly convertible to 'System.IDisposable'Mender
A
2
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;
}
Ailsa answered 14/2, 2015 at 9:49 Comment(1)
please explain, what is this?Cyclohexane
A
-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();
    }
Ailsa answered 14/2, 2015 at 9:46 Comment(0)
A
-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;
        }
    }
}

You can check this out on https://github.com/gurvirlochab/CustomNotifications Here is a sample screenshot of the DialogBox

Adjoint answered 12/4, 2020 at 17:47 Comment(1)
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Lianna

© 2022 - 2024 — McMap. All rights reserved.