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

Отображение текста в надписи

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

  2. В диалоговом окне Создание проекта выберите Приложение Windows Forms, а затем нажмите кнопку ОК.

Откроется новый проект Windows Forms.

  1. Из панели элементов перетащите в форму элемент управления Label.

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

    Свойство

    Значение

    Имя

    changeText

    Текст

    Изменить текст

    Размер

    80, 23

  3. Дважды щелкните кнопку, чтобы создать обработчик событий changeText_Click, и добавьте следующий код.

    this.label1.Text = "Time " + DateTime.Now.ToLongTimeString();

  4. Add another Button control to the form, and change the following properties

    Property

    Value

    Name

    changeColor

    Text

    Change Color

    Size

    80, 23

  5. Double-click the button to create the changeColor_Click event handler, and add the following code:

    Random randomColor = new Random();

    this.label1.ForeColor = Color.FromArgb(randomColor.Next(0, 256),

    randomColor.Next(0, 256), randomColor.Next(0, 256));

  6. Press F5 to run the program.

  7. Click Change Text and verify that the text in the label is updated.

  8. Click Change Color and verify that the font of the text is changed to a new color.