【问题标题】:Format dynamically created Radio buttons in Columns in c#在c#的列中格式化动态创建的单选按钮
【发布时间】:2017-06-13 20:43:57
【问题描述】:

我正在以编程方式为数组/列表中的每个元素创建单选按钮,并将它们放置在 Windows 窗体中。

现在每个新的单选按钮都位于前一个单选按钮的下方。我怎样才能设法在例如之后开始一个新列4/5 单选按钮?新列应出现在先前单选按钮的右侧。

这是我目前的代码:

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

【问题讨论】:

  • 在哪里开始新栏目?你为什么不分享你到目前为止所做的事情?
  • 增加下一个单选按钮的左值?
  • @Abhishek 帖子已更新!
  • 将 RadioButtons 添加到 FlowLayoutPanel

标签: c# winforms radio-button


【解决方案1】:

使用 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++;
            }
        }

如果你真的需要你的解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 2013-02-03
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    相关资源
    最近更新 更多