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

How to: Create a Group of Radio Buttons from a String Array

This example programmatically creates a group of Windows Forms RadioButton controls and sets their Text properties to values from an array of strings.

Compiling the Code

This example requires:

  • A Windows Form with a Button control named button1. Set the Click event handler of button1 to button1_Click.

Example

private void button1_Click(object sender, System.EventArgs e)

{

string[] stringArray = new string[3];

stringArray[0] = "Yes";

stringArray[1] = "No";

stringArray[2] = "Maybe";

System.Windows.Forms.RadioButton[] radioButtons =

new System.Windows.Forms.RadioButton[3];

for (int i = 0; i < 3; ++i)

{

radioButtons[i] = new RadioButton();

radioButtons[i].Text = stringArray[i];

radioButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);

this.Controls.Add(radioButtons[i]);

}

}

Создание группы переключателей из массива строк

В этом примере программно создается группа элементов управления RadioButton Windows Forms и их свойствам Text присваиваются значения из массива строк.

Компиляция кода

Для этого примера необходимы следующие компоненты.

  • Форма Windows Form с элементом управления Button, которому задано имя button1. Задайте обработчику событий Click для button1 значение button1_Click.

Пример

<--------

How to: Create a Non-Rectangular Button

This example demonstrates how to create a button that is a different shape from the standard rectangular button. The code adds a button in the shape of a circle to the form and creates an event handler that displays a message when the circle is clicked.

Compiling the Code

This example requires a Windows Forms Application project that contains a form named Form2.

Example

public Form2()

{

//

// Required for Windows Form Designer support.

//

InitializeComponent();

// Initialize the user-defined button,

// including defining handler for Click message,

// location and size.

myButtonObject myButton = new myButtonObject();

EventHandler myHandler =

new EventHandler(myButton_Click);

myButton.Click += myHandler;

myButton.Location = new System.Drawing.Point(20, 20);

myButton.Size = new System.Drawing.Size(101, 101);

this.Controls.Add(myButton);

}

public class myButtonObject : UserControl

{

// Draw the new button.

protected override void OnPaint(PaintEventArgs e)

{

Graphics graphics = e.Graphics;

Pen myPen = new Pen(Color.Black);

// Draw the button in the form of a circle

graphics.DrawEllipse(myPen, 0, 0, 100, 100);

myPen.Dispose();

}

}

// Handler for the click message.

void myButton_Click(Object sender, System.EventArgs e)

{

MessageBox.Show("Click");

}

Создание непрямоугольной кнопки

В этом примере показано создание кнопки, форма которой отличается от стандартной прямоугольной. Код добавляет в форму круглую кнопку и создает обработчик событий, который выводит сообщение после щелчка мышью по кругу.

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