Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_for_Beginners.doc
Скачиваний:
32
Добавлен:
23.11.2019
Размер:
2.4 Mб
Скачать

Создание основной формы приложения

  1. В меню Файл выберите команду Создать проект.

Откроется диалоговое окно Создание проекта.

  1. Выберите элемент Приложение Windows Forms и нажмите кнопку ОК.

Форма с именем Form1 добавится к проекту.

  1. Перетащите элемент управления ListBox из панели элементов в форму и измените следующие свойства в окне Свойства.

    Свойство

    Значение

    Модификаторы

    Public

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

Свойство

Значение

Имя

addItems

Текст

Добавить

Создание диалогового окна

  1. В меню Проект выберите Добавить форму Windows, оставьте имя по умолчанию Form2, затем щелкните Добавить.

  2. Перетащите элемент управления Label из панели элементов в форму и измените свойства Текст на Введите элементы (по одному на каждой строке).

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

    Свойство

    Значение

    Многострочность

    True

    Полосы прокрутки

    Обе

    Размер

    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.

  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

Текст

ОК

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]