title: Save data to a file when a button is clicked?
body: <p>I'm making an application for a ledcube in C#.
  All it does now is allow the user to click on some buttons to change the color of the desired led's. 
  I'm trying to imply a function which allows the user to save the current configuration (of colors) to a file, which later can be read by a programming bord.
  The saved file must contain binary code for each led (whether the led is on, and which color: off, red, green, orange).
  I was thinking that 1 led can contain a value between 00 - 11 (so 0 and 3).</p>
  
  <pre><code>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[,] laag_1 = new Button[8, 8];
      Button[,] laag_2 = new Button[8, 8];
      Button[,] laag_3 = new Button[8, 8];
      Button[,] laag_4 = new Button[8, 8];
      Button[,] laag_5 = new Button[8, 8];
      Button[,] laag_6 = new Button[8, 8];
      Button[,] laag_7 = new Button[8, 8];
      Button[,] laag_8 = new Button[8, 8];
  
      public Form1()
      {
          InitializeComponent();
          for (int x = 0; x &lt; laag_1.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_1.GetLength(1); y++)
              {
                  laag_1[x, y] = new Button();
                  laag_1[x, y].SetBounds((50 * x) + 100, (50 * y) + 30, 40, 40);
                  laag_1[x, y].Click += new EventHandler(this.btnEvent_click);
                  Controls.Add(laag_1[x, y]);
                  laag_1[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_2.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_1.GetLength(1); y++)
              {
                  laag_2[x, y] = new Button();
                  laag_2[x, y].SetBounds((50 * x) + 520, (50 * y) + 30, 40, 40);
                  laag_2[x, y].Click += new EventHandler(this.btnEvent_click);
                  Controls.Add(laag_2[x, y]);
                  laag_2[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_3.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_3.GetLength(1); y++)
              {
                  laag_3[x, y] = new Button();
                  laag_3[x, y].SetBounds((50 * x) + 940, (50 * y) + 30, 40, 40);
                  laag_3[x, y].Click += new EventHandler(this.btnEvent_click);
                  Controls.Add(laag_3[x, y]);
                  laag_3[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_4.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_4.GetLength(1); y++)
              {
                  laag_4[x, y] = new Button();
                  laag_4[x, y].SetBounds((50 * x) + 1360, (50 * y) + 30, 40, 40);
                  laag_4[x, y].Click += new EventHandler(this.btnEvent_click);
                  Controls.Add(laag_4[x, y]);
                  laag_4[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_5.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_5.GetLength(1); y++)
              {
                  laag_5[x, y] = new Button();
                  laag_5[x, y].SetBounds((50 * x) + 100, (50 * y) + 520, 40, 40);
                  laag_5[x, y].Click += new EventHandler(this.btnEvent_click);
                  Controls.Add(laag_5[x, y]);
                  laag_5[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_6.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_6.GetLength(1); y++)
              {
                  laag_6[x, y] = new Button();
                  laag_6[x, y].SetBounds((50 * x) + 520, (50 * y) + 520, 40, 40);
                  laag_6[x, y].Click += new EventHandler(this.btnEvent_click);
                  Controls.Add(laag_6[x, y]);
                  laag_6[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_7.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_7.GetLength(1); y++)
              {
                  laag_7[x, y] = new Button();
                  laag_7[x, y].SetBounds((50 * x) + 940, (50 * y) + 520, 40, 40);
                  laag_7[x, y].Click += new EventHandler(this.btnEvent_click);
                  Controls.Add(laag_7[x, y]);
                  laag_7[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_8.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_8.GetLength(1); y++)
              {
                  laag_8[x, y] = new Button();
                  laag_8[x, y].SetBounds((50 * x) + 1360, (50 * y) + 520, 40, 40);
                  laag_8[x, y].Click += new EventHandler(this.btnEvent_click);
                  Controls.Add(laag_8[x, y]);
                  laag_8[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\laag_1.txt";
      private void LoadFromFile()
      {
          if (!System.IO.File.Exists(filePath))
              return;
  
          byte[] data = System.IO.File.ReadAllBytes(filePath);
          if (data == null || data.Length != laag_1.GetLength(0) * laag_1.GetLength(1) * 2)
              return;
  
          for (int x = 0; x &lt; laag_1.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_1.GetLength(1); y++)
              {
                  int position = (y * laag_1.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;
                  }
  
                  laag_1[x, y].BackColor = color;
              }
          }
  
      }
  
      private void SaveToFile()
      {
          Dictionary&lt;Form1, int&gt; d = new Dictionary&lt;Form1, int&gt;();
  
          byte[] data = new byte[laag_1.GetLength(0) * laag_1.GetLength(1) * 2];
          for (int x = 0; x &lt; laag_1.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_1.GetLength(1); y++)
              {
                  int position = (y * laag_1.GetLength(0) + x);
                  string value;
                  switch (laag_1[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)
      {
          for (int x = 0; x &lt; laag_1.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_1.GetLength(1); y++)
              {
                  laag_1[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_2.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_2.GetLength(1); y++)
              {
                  laag_2[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_3.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_3.GetLength(1); y++)
              {
                  laag_3[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_4.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_4.GetLength(1); y++)
              {
                  laag_4[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_5.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_5.GetLength(1); y++)
              {
                  laag_5[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_6.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_6.GetLength(1); y++)
              {
                  laag_6[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_7.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_7.GetLength(1); y++)
              {
                  laag_7[x, y].BackColor = Color.Black;
              }
          }
          for (int x = 0; x &lt; laag_8.GetLength(0); x++)
          {
              for (int y = 0; y &lt; laag_8.GetLength(1); y++)
              {
                  laag_8[x, y].BackColor = Color.Black;
              }
          }
      }
  
   }
  }
  </code></pre>