title: How to pass a button array into a function
body: <p>I'm trying to create a function that will send a serial string to a com port and change colors every time a button is pressed. </p>
  
  <p>I have figured out how to do this with one button, but now I want to make an array of 140 buttons that can all be passed through this function. </p>
  
  <p>My problem is that all 140 buttons perform the function of the first button, not what their individual function should be, which is send a different serial command and turn blue when it is on. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      bool[] buttonStatus = new bool[140];
      Button[] buttonArray = new Button[140];
  
      private SerialPort LB;//"LB for left bottom module
  
      public Form1()
      {
          InitializeComponent();
          Initializing();
          CreatingNewButtons();
      }
  
      /// &lt;summary&gt;
      /// Initializes Serial ports to be used
      /// &lt;/summary&gt;
      private void Initializing()
      {
          try
          {
              LB = new SerialPort();
              LB.BaudRate = 57600;
              LB.PortName = "COM9";
              LB.Open();
          }
          catch(Exception)
          {
              MessageBox.Show("error connecting");
          }
  
      }
  
      /// &lt;summary&gt;
      /// Creates all 140 buttons at startup
      /// &lt;/summary&gt;
      private void CreatingNewButtons()
      {
          int horizotal = 80;
          int vertical = 30;
  
          for (int i = 0; i &lt; buttonArray.Length; i++)
          {
              buttonArray[i] = new Button();
              buttonArray[i].Size = new Size(25, 25);
              buttonArray[i].Location = new Point(horizotal, vertical);
              if ((i == 10) || (i == 20) || (i == 30) || (i == 40) || (i == 50) ||
                  (i == 60) || (i == 70) || (i == 80) || (i == 90) || (i == 100) || 
                  (i == 110) || (i == 120) || (i == 130))
              {
                  vertical = 30;
                  horizotal = horizotal + 30;
              }
              else
                  vertical = vertical + 30;
              this.Controls.Add(buttonArray[i]);
              // Add a Button Click Event handler
              buttonArray[i].Click += new EventHandler(buttonArray_Click);
          }
      }
  
      void sendStringToAllModules (string stringToSend)
      {
          LB.WriteLine(stringToSend);//write string text to arduino
      }
  
      private void sendStringBtn_Click(object sender, EventArgs e)
      {
          LB.WriteLine(stringTextBox.Text);//write string text to arduino
      }
  
      private void receiveStringBtn_Click(object sender, EventArgs e)
      {
          receiveStringTextBox.Text = LB.ReadExisting();
      }
  
      #region pushbuttonCode
  
      /// &lt;summary&gt;
      /// send function index and sends either on or off to leds of modules
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;&lt;/param&gt;
      /// &lt;param name="e"&gt;&lt;/param&gt;
      void toggleLEDs(int i)
      {
  
          if (!buttonStatus[i])
          {
              sendStringToAllModules("1;" + i + ";");
              buttonArray[i].BackColor = Color.CornflowerBlue;
              buttonStatus[i] = !buttonStatus[i];
          }
          else
          {
              sendStringToAllModules("2;" + i + ";"); 
              buttonArray[i].BackColor = Color.Gainsboro; 
              buttonStatus[i] = !buttonStatus[i]; 
          } 
      } 
  
      private void buttonArray_Click(object sender, EventArgs e)//this button works
      {
          toggleLEDs(0);
      }
      private void buttonArray1_Click(object sender, EventArgs e)//this one doesn't
      { 
          toggleLEDs(0); 
      } 
      #endregion 
  }
  </code></pre>
  
  <p>Any help is greatly appreciated! </p>