Eitan Yona
Eitan Yona

Reputation: 75

how to place my button?

I have a problem with my C# WinForm project.

In my project I have a function to draw a square, and I have a function that makes buttons at run time. What I want to do is that the button will place on the square.

I try to use 2 arrays; one gets the x location of the square, and the other gets the y location.

The button is placed at the x and y location one by one in columns but its place them diagonal.

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

Upvotes: 0

Views: 160

Answers (1)

Kamil Lach
Kamil Lach

Reputation: 4629

You need do add created button to Form Controls collection.

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;

Upvotes: 1

Related Questions