简体   繁体   English

在C#的“列”中格式化动态创建的单选按钮

[英]Format dynamically created Radio buttons in Columns in c#

I am creating radio buttons programatically for each element in an array/list and place them in a windows form. 我正在以编程方式为数组/列表中的每个元素创建单选按钮,并将它们放置在Windows窗体中。

Right now every new radio button is place under the previous one. 现在,每个新的单选按钮都位于上一个按钮的下方。 How can i manage to start a new column after eg 4/5 radio buttons? 如何在例如4/5单选按钮之后设法开始新的一列? The new column should appear on the right side of the previous radio-buttons. 新列应出现在以前的单选按钮的右侧。

This is my code so far: 到目前为止,这是我的代码:

for (int i = 0; i < startShapes.Count; i++)
{
    RadioButton rdb = new RadioButton();
    rdb.Text = startShapes.Values.ElementAt(i).Equals("") ? startShapes.Keys.ElementAt(i) : startShapes.Values.ElementAt(i);
    rdb.Size = new Size(100, 30);
    this.Controls.Add(rdb);
    rdb.Location = new Point(45, 70 + 35 * i);
    rdb.CheckedChanged += (s, ee) =>
    {
        var r = s as RadioButton;
        if (r.Checked)
            this.selectedString = r.Text;
    };
}

How about using TableLayoutPanel? 如何使用TableLayoutPanel?

        Dictionary<string, string> startShapes = new Dictionary<string, string>();
        for(int i=0;i<20;i++)
            startShapes.Add("Shape " +i, "Shape " +i);
        int row = 0;
        int col = 0;
        tableLayoutPanel1.RowStyles.Clear();
        tableLayoutPanel1.ColumnStyles.Clear();
        tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
        tableLayoutPanel1.RowCount= 0;
        tableLayoutPanel1.ColumnCount = 1;

        foreach (var kvp in startShapes)
        {
            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tableLayoutPanel1.RowCount++;
            RadioButton rdb = new RadioButton();
            rdb.Text = string.IsNullOrEmpty(kvp.Value) ? kvp.Key : kvp.Value;
            rdb.Size = new Size(100, 30);
            rdb.CheckedChanged += (s, ee) =>
            {
                var r = s as RadioButton;
                if (r.Checked)
                    this.selectedString = r.Text;
            };
            tableLayoutPanel1.Controls.Add(rdb, col, row);
            row++;
            if (row == 5)
            {
                col++;
                row = 0;
                tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                tableLayoutPanel1.ColumnCount++;
            }
        }

And if you really need your solution: 如果您真的需要解决方案:

int left = 45;
int idx = 0;
for (int i = 0; i < startShapes.Count; i++)
{
    RadioButton rdb = new RadioButton();
    rdb.Text = startShapes.Values.ElementAt(i).Equals("") ? startShapes.Keys.ElementAt(i) : startShapes.Values.ElementAt(i);
    rdb.Size = new Size(100, 30);
    this.Controls.Add(rdb);
    rdb.Location = new Point(left, 70 + 35 * idx++);
    if (idx == 5)
    {
        idx = 0; // reset row
        left += rdb.Width + 5; // move to next column
    }
    rdb.CheckedChanged += (s, ee) =>
    {
        var r = s as RadioButton;
        if (r.Checked)
            this.selectedString = r.Text;
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM