2012-05-07 64 views
0

我的C#WinForm項目出現問題。如何放置我的按鈕?

在我的項目中,我有一個繪製方塊的函數,並且我有一個函數可以在運行時生成按鈕。我想要做的是按鈕將放在廣場上。

我嘗試使用2個數組;一個獲得廣場的x位置,另一個獲取y位置。

該按鈕放置在x和y位置一列一列,但它的位置對角線。

int[] locationx = new int[100]; 
    int[] locationy = new int[100]; 
    int monex = 0; 
    int money = 0; 
    private void DrawAllSquares()//z,k its many square its going to draw 
    { 
     int tempy = y; 
     for (int i = 0; i < z; i++) 
     { 
      DrawingSquares(x, y); 
      for (int j = 0; j < k - 1; j++) 
      { 
       locationy[money] = tempy; 
       money++; 
       tempy += 60; 
       DrawingSquares(x, tempy); 
      } 
      x += 120; 
      locationx[monex] = x; 
      monex++; 
      tempy = y; 
     } 

    } 
     private void button2_Click(object sender, EventArgs e) 
    { 
          Button myText = new Button(); 
      myText.Tag = counter; 
      //changeplace(); 
      myText.Location = new Point(locationx[monex2], locationy[money2]); 
      monex2++; 
      money2++; 
      buttonList.AddLast(myText); 
      myText.Text = Convert.ToString(textBox3.Text); 
      this.Controls.Add(myText); 
      buttons[counter] = myText; 
      myText.BringToFront(); 
      counter++; 
    } 

回答

1

你需要做的添加創建按鈕控件集合。

private void button2_Click(object sender, EventArgs e) 
{ 
    Button myText = new Button(); 
    myText.Tag = counter; 
    myText.Location = new Point(locationx[monex2], locationy[money2]); 
    Controls.Add(myText); // Assuming that handler 'button2_Click' is in your Form class. 
    // rest of your code 
} 

編輯

Button myText = new Button(); 
myText.Click += button2_Click; 
+0

我有一個功能 –

+0

是的,但我的意思是這條線:** Controls.Add被(會將myText); ** –

+0

鎖在我的編輯:) –