(C#) 在程式內添加物件及事件

想要達成基本的GUI而不用一直重工,所以打算編輯一些方便的工具。

以button為例子之後將擴充相關的UI及知識

private void Form1_Load(object sender, EventArgs e)
{
    int width, height;
    buttonTable = new Button[9];

    width = 60;
    height = 60;

    for(int i = 0; i < 9; i++)
    {
        buttonTable[i] = new Button();
        buttonTable[i].Size = new Size(width, height);
        buttonTable[i].Text = i.ToString();
        buttonTable[i].BackColor = Color.Peru;
        buttonTable[i].Click += new EventHandler(btnDemo_Click);
        this.Controls.Add(buttonTable[i]);
        if (i <= 2) buttonTable[i].Location = new System.Drawing.Point(150 + i * 60, 110);
        else if (i > 2 && i <= 5) buttonTable[i].Location = new System.Drawing.Point(150 + (i - 3) * 60, 170);
        else buttonTable[i].Location = new System.Drawing.Point(150 + (i - 6) * 60, 230);
    }
}

protected void btnDemo_Click(object sender, EventArgs e)
{
    Button temp = (Button)sender;
    MessageBox.Show(temp.Text);
}

猜你喜欢

转载自www.cnblogs.com/ollie-lin/p/10807286.html
今日推荐