如何:基于字符串数组创建一组单选按钮 (Visual C#)

更新:2007 年 11 月

此示例以编程方式创建一组 Windows 窗体 RadioButton 控件,并将其 Text 属性设置为一个字符串数组中的值。

示例

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

编译代码

此示例需要:

  • 一个含有名为 button1 的 Button 控件的 Windows 窗体。将 button1 的 Click 事件处理程序设置为 button1_Click。

请参见

概念

在 Visual C# 中设计用户界面

其他资源

Button 控件

Visual C# 指导教程