logo search
Учебник_ПОА

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.

  1. 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();

}

  1. 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();

  2. Добавьте элемент управления Button в форму и измените следующие свойства в окне Свойства.

Свойство

Значение

Имя

okButton

Текст

ОК