0

嘿伙计们,我现在有一个问题,我有一个 8*8 的按钮阵列。他们可以通过点击来改变颜色。我想制作一个重置按钮,这样当您单击重置按钮时,它们将再次返回背景黑色。Button1 是复位按钮。它位于代码的底部。

这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Button[,] btn = new Button[8, 8];
        public Form1()
        {
            InitializeComponent();
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    btn[x, y] = new Button();
                    btn[x, y].SetBounds((50 * x) + 30, (50 * y) + 30, 40, 40);
                    btn[x, y].Click += new EventHandler(this.btnEvent_click);
                    Controls.Add(btn[x, y]);
                    btn[x, y].BackColor = Color.Black;
                }
            }

            this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
            LoadFromFile();
        }

        void btnEvent_click(object sender, EventArgs e)
        {
            Control ctrl = ((Control)sender);
            switch (ctrl.BackColor.Name)
            {
                case "Red":
                    ctrl.BackColor = Color.Green;
                    break;
                case "Black":
                    ctrl.BackColor = Color.Red;
                    break;
                case "Green":
                    ctrl.BackColor = Color.Yellow;
                    break;
                case "Yellow":
                    ctrl.BackColor = Color.Black;
                    break;
                default:
                    ctrl.BackColor = Color.Black;
                    break;
            }
        }

        void SaveEventHandler(object sender, EventArgs e)
        {
            SaveToFile();
        }

        private const string filePath = @"C:\testmap\test.txt";
        private void LoadFromFile()
        {
            if (!System.IO.File.Exists(filePath))
                return;

            byte[] data = System.IO.File.ReadAllBytes(filePath);
            if (data == null || data.Length != btn.GetLength(0) * btn.GetLength(1) * 2)
                return;

            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (y * btn.GetLength(0) + x);

                    string value = ((char)data[2 * position]).ToString() + ((char)data[2 * position + 1]).ToString();
                    Color color;
                    switch (value)
                    {
                        case "01":
                            color = Color.Red;
                            break;
                        case "00":
                            color = Color.Black;
                            break;
                        case "10":
                            color = Color.Green;
                            break;
                        case "11":
                            color = Color.Yellow;
                            break;
                        default:
                            color = Color.Black;
                            break;
                    }

                    btn[x, y].BackColor = color;
                }
            }
        }

        private void SaveToFile()
        {
            Dictionary<Form1, int> d = new Dictionary<Form1, int>();

            byte[] data = new byte[btn.GetLength(0) * btn.GetLength(1) * 2];
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (y * btn.GetLength(0) + x);
                    string value;
                    switch (btn[x, y].BackColor.Name)
                    {
                        case "Red":
                            value = "01";
                            break;
                        case "Black":
                            value = "00";
                            break;
                        case "Green":
                            value = "10";
                            break;
                        case "Yellow":
                            value = "11";
                            break;
                        default:
                            value = "00";
                            break;
                    }
                    data[2 * position] = (byte)value[0];
                    data[2 * position + 1] = (byte)value[1];
                }
            }

            System.IO.File.WriteAllBytes(filePath, data);
        }



        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
4

3 回答 3

1

就像您初始化按钮一样,您可以将背景颜色设置回黑色:

private void button1_Click(object sender, EventArgs e)
{
        for (int x = 0; x < btn.GetLength(0); x++)
        {
            for (int y = 0; y < btn.GetLength(1); y++)
            {
                btn[x, y].BackColor = Color.Black;
            }
        }
}
于 2013-01-21T09:43:12.347 回答
0
for(int i=0;i<8;i++)
{
   for(int j=0;j<8;j++)
   {
       btn[i, j].BackColor = Color.Black;
   }  
}

但你试过什么?

于 2013-01-21T09:42:58.287 回答
0

http://msdn.microsoft.com/en-AU/library/system.windows.forms.control.tag.aspx

假设您不打算将控件的 Tag 属性用于其他任何事情,您可以考虑将所有附属元素设置为特定的 Tag,然后遍历表单的控件以将它们重置为所需的 BackColor,或者您可以简单地将 Tag 设置为您的所需的 BackColor 并让重置按钮根据需要进行迭代。

于 2013-01-21T09:43:18.897 回答