2017-06-13 16 views
0

配列/リスト内の各要素に対してラジオボタンをプログラムで作成し、それらをウィンドウのフォームに配置します。フォーマットを動的に作成するC#の列のラジオボタン

現在、新しいラジオボタンはすべて前のラジオボタンの下に配置されています。たとえば、後に新しい列を開始するにはどうすればいいですか? 4/5ラジオボタン?新しい列は、前のラジオボタンの右側に表示されます。

これは、これまでの私のコードです:TableLayoutPanelの使用に関する

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

スタート新しい列を?なぜあなたは今まで何をしたのですか? – Abhishek

+0

次のラジオボタンの左の値を大きくしますか? – Gusman

+0

@Abhishek投稿が更新されました! – dnks23

答えて

1

どのように?

 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; 
    }; 
} 
関連する問題