2014-07-07 39 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 )); 

你實際上是創建多個按鈕,但他們都在彼此之上。

相關問題