2014-07-07 35 views
0

我正在尝试为文本文件的每一行动态创建一个新按钮。文本文件每行的新按钮?

我的问题是无论文本文件有多少行,它只生成一个按钮。

代码:

System.IO.StreamReader file = new System.IO.StreamReader 
    (@"D:\SupportDash\Settings\Settings.txt"); 

string[] lines = System.IO.File.ReadAllLines(@"D:\SupportDash\Settings\Settings.txt"); 
     foreach(String row in lines) 
     {   

      Button Buttona = new System.Windows.Forms.Button(); 
      Buttona.Text = "Test"; 
      Buttona.UseVisualStyleBackColor = true; 
      Buttona.Location = new System.Drawing.Point(85,28); 
      Buttona.Click += (s, e) => 
      { 
       Form DynamicForm = new Form(); 
       DynamicForm.Show(); 

      }; 

      groupBox2.Controls.Add(Button); 

      counter++; 

     } 

     file.Close(); 
    } 

我也用了一段时间,一个do-while循环尝试。 - 同样的事情发生。

我的文本文件由回车分隔。 (它使用File.ApendAllText()生成在程序中)

难道是我的程序引起的一个问题,只是认为有一条巨大的线?

回答

2

您正在将所有按钮放在同一Location,所以它可能看起来只有一个按钮,实际上每行只有一个按钮,它们只是彼此重叠。

尝试每次通过循环递增位置的Top或使用支持包装的面板控件。

+0

太棒了,我不知道为什么我认为它会为我做。谢谢。 – Wills

0

您将需要一个新的位置

定位每一个新的按钮,例如当您设置

Buttona.Location = new System.Drawing.Point(85,28); 

呼叫类似

var buttonHeight = 10; 

Buttona.Location = new System.Drawing.Point(85,28 + (count * buttonHeight )); 

你实际上是创建多个按钮,但他们都在彼此之上。

相关问题