How to: Retrieve Data from a Dialog Box

Dialog boxes enable your program to interact with users and retrieve data that users have entered in the dialog box for use in your application. There are several built-in dialog boxes that you can use in your applications. For more information, see Using Built-in Dialog Boxes in Your Application. You can also create your own dialog boxes.

A common task in Windows Forms development is displaying a dialog box to collect information when a user clicks a button on the main form of an application. You can see an example of this in the Visual C# Express Edition IDE. When you add a ListBox control to a form, you can click the Items property in the Properties window to display a dialog box. This dialog box enables you to quickly type text on separate lines of a text box. The application then adds each line of text to ListBox control when you click OK. If you want to let users add their own items to a list box in your application, you can create your own string editor dialog box.

To create the main form of your application

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application, and then click OK.

    A form named Form1 is added to the project.

  3. From the Toolbox, drag a ListBox control to the form, and change the following property in the Properties window:

    Property

    Value

    Modifiers

    Public

  4. Add a Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    addItems

    Text

    Add

To create a dialog box

  1. On the Project menu, click Add Windows Form, leave the default name Form2, and then click Add.

  2. From the Toolbox, drag a Label control to the form, and change the Text property to Enter the String (one per line).

  3. Add a TextBox control to the form, and change the following properties in the Properties window.

    Property

    Value

    Multiline

    True

    Scrollbars

    Both

    Size

    255, 160

  4. Add a Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    okButton

    Text

    OK

Retrieving Data from a Dialog Box

There are several ways you can pass data from one Windows form to another. In this example, you will pass Form1 to the constructor of Form2.

To retrieve data from a dialog box.

  1. Double-click the OK button to create the default Click event handler.

    Before you add code to this procedure, you should create a variable for the main form and create a new constructor for the Form2 class.

  2. Add the following code below the default constructor. This code creates an overloaded constructor that requires Form1 as a parameter.

    Form1 mainForm;
    
    public Form2(Form1 mainForm)
    {
        this.mainForm = mainForm;
        InitializeComponent();
    
    }
    
  3. In the Click event handler of okButton, add the following code. This code clears all existing text in the list box, assigns each line of text of the text box on Form2 to an array, and then adds each item of the array to the list box on Form1.

    if (this.textBox1.Text != string.Empty)
    {
    
        mainForm.listBox1.Items.Clear();
    
        string[] stringsEntered = textBox1.Lines;
    
        for (int count = 0; count < stringsEntered.Length; count+)
        {
    
            mainForm.listBox1.Items.Add(stringsEntered[count]);
    
        }
    
    }
    this.Close();
    
  4. Right-click Form1 in Solution Explorer and click View Designer.

  5. Double-click the Add button to add the default Click event handler, and add the following code to create an instance of Form2 and display the form:

    Form2 subForm = new Form2(this);
    subForm.Show();
    
  6. Press F5 to run the code.

  7. When the form opens, click Add.

  8. When the dialog box opens, type the following list of colors, and press ENTER after each word.

    Blue

    Green

    Yellow

    Red

  9. Click OK.

  10. Verify that the data typed in the dialog box appears in the list box of Form1.

  11. Close the application.

See Also

Concepts

Using Built-in Dialog Boxes in Your Application

Designing a User Interface in Visual C#

Other Resources

Dialog Boxes (Visual C#)

Visual C# Guided Tour