如何在c#中获得所有winforms形式的边框样式

时间:2013-12-17 03:16:14

标签: c# .net winforms

我是C#的新手,正在完成一个教程。我正在尝试为组合框添加边框样式,我希望能够检索所有可能的边框样式。

private void Form1_Load(object sender, EventArgs e)
    {
        /*
        foreach (FormBorderStyle f in FormBorderStyle)
        {
        }
       */
        // This return only the current one.
        FormBorderStyle borderStyleEntry = this.FormBorderStyle;
        borderSelector.Items.Add(borderStyleEntry );
    }

2 个答案:

答案 0 :(得分:5)

FormBorderStyleEnum。您需要foreach超过Enum的值。

foreach (FormBorderStyle f in (FormBorderStyle[])Enum.GetValues(typeof(FormBorderStyle)))
{
    borderSelector.Items.Add(f.ToString());
}

为了完整性,根据@elgonzo的评论,以下内容显示了如何从Enum获取String值。

private void borderSelector_SelectedIndexChanged(object sender, EventArgs e)
{
    this.FormBorderStyle = (FormBorderStyle)Enum.Parse(typeof(FormBorderStyle), borderSelector.SelectedItem.ToString());
}

答案 1 :(得分:4)

您可以先将FormBorderStyle枚举转换为List<string>,然后将其设置为组合框的数据源。

List<string> values = Enum.GetNames(typeof(FormBorderStyle)).ToList();
comboBox1.DataSource = values;