简体   繁体   English

如何放置我的按钮?

[英]how to place my button?

I have a problem with my C# WinForm project. 我的C#WinForm项目有问题。

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; 我尝试使用2个数组; one gets the x location of the square, and the other gets the y location. 一个获取正方形的x位置,另一个获取y位置。

The button is placed at the x and y location one by one in columns but its place them diagonal. 该按钮一列一列地放置在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++;
    }

You need do add created button to Form Controls collection. 您需要向Form 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM