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.

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]);
    }
}

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.

See Also

Concepts

Designing a User Interface in Visual C#

Other Resources

Button Controls

Visual C# Guided Tour