开发者

c# win application [closed]

开发者 https://www.devze.com 2023-01-20 16:51 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
int a, b;

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Width = 600;
        this.Height = 436;

        for (a = 1; a <= 8; a++)
        {
            for (b = 1; b <= 8; b++)
            {
                Button btn = new Button();
                btn.Name = (((a - 1) * 8) + b).ToString();
                btn.Width = 50;
                btn.Height = 50;
                btn.Left = (b - 1) * 50;
                btn.Top = (a - 1) * 50;

                if ((a + b) % 2 == 0)
                    btn.BackColor = Color.WhiteSmoke;
     开发者_开发技巧           else
                    btn.BackColor = Color.Black;

                btn.Click += new EventHandler(btn_Click);

                this.Controls.Add(btn);
            }
        }
    }
    int i, j,y;
    void btn_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;

        if (radioButton1.Checked == true)
        {
            i = int.Parse(btn.Name);
            j = i % 8;
            for (y = 1; y <= 8; j+=8)
            {

            }
        }

how can i change other buttons backcolor?


Make a List<Button> and add your newly added buttons to this list. Then, you can change any property of button in this list with getting button's index


You'll need to keep a reference to the other buttons. Preferably: create all buttons, add them to a list. In the eventhandler, iterate that list and set the background color


Use your this.Controls reference...not exactly syntactically correct....but you get the idea...

    Button btn = (Button)sender;

    if (radioButton1.Checked == true)
    {
        i = int.Parse(btn.Name);
        j = i % 8;
        for (y = 1; y <= 8; j+=8)
        {
           if(!btn.equals(this.Controls[y]))
              this.Controls[y].BackColor = Color.Red;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消