2012-05-07 3 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

Вам нужно сделать, добавить созданную кнопку Форма коллекции Controls.

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 
} 

EDIT:

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

я есть, что функция –

+0

да, но я имею в виду эту строку: ** Controls.Add (MYTEXT); ** –

+0

замок на моем редактирования :) –

Смежные вопросы