Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 209
Ahmad Avatar asked Mar 05 '12 15:03

Ahmad


People also ask

How do I create a custom dialog box?

Android App Development for Beginners This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is dialog C#?

A dialog box in C# is a type of window, which is used to enable common communication or dialog between a computer and its user. A dialog box is most often used to provide the user with the means for specifying how to implement a command or to respond to a question. Windows. Form is a base class for a dialog box.


2 Answers

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"); 
like image 111
Bas Avatar answered Oct 01 '22 13:10

Bas


  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     } } 
like image 28
Steve Avatar answered Oct 01 '22 15:10

Steve