Ahmad Ahmad - 10 months ago 72
C# Question

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

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 ?

Bas Bas
Answer Source

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(); };
            return (int)inputBox.Value;

Then call it using:

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