title: how to use serial port throughout my Windows Forms application?
body: <p>I'm attempting to make a C# from program that is a simple box with four buttons: left, right, up, and down. then when a button is initially pressed(but not let go) it will send a a serial line. then when the button is unclicked(user lets go of left mouse button) i want to send another serial command.</p>
  
  <p>the issue is that each <code>void OnClick... (object sender, System.EventArgs e)</code> is separate from the others and the serial port "port" doesn't work throughout the whole program. i have in main for serial use:</p>
  
  <pre><code>SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
  port.Open();
  </code></pre>
  
  <p>i didn't want to open and close the serial port each time i want to send something because it may be too close together(in terms of time) and may cause the "serial port in use" issue and or lag in the sending of the serial line.</p>
  
  <p>slimmed down as much as i could.</p>
  
  <p>code:</p>
  
  <pre><code>    public class EAS_ControlForm : System.Windows.Forms.Form
      {
          private Button Up4;
          private Button Down3;
          private Button Left2;
          private Button Right1;
          public EAS_ControlForm()
          {
              Text = "Etch-a-Sketch Control";
              Down3 = new Button ();
              Up4 = new Button ();
              Left2 = new Button ();
              Right1 = new Button ();
  
              Down3.Text = "Down";
              Down3.Name = "Down3";
              Down3.Size = new System.Drawing.Size (72, 30);
              Down3.Location = new System.Drawing.Point ((ClientRectangle.Width - Down3.Size.Width) / 2, ClientRectangle.Height - 10);
              Controls.AddRange(new System.Windows.Forms.Control[] {this.Down3});
              Down3.Click += new System.EventHandler(OnClickDown3);
  
              Up4.Text = "Up";
              ///...button up4 stuff here like down 3 above.
  
              Left2.Text = "Left";
              ///...button left2 stuff here like down 3 above.
  
              Right1.Text = "Right";
              ///...button right1 stuff here like down 3 above.
          }
  
          static public void Main() 
          {
              SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
              port.Open();
  
              Application.Run(new EAS_ControlForm());
          }
  
          void OnClickDown3 (object sender, System.EventArgs e)
          {
              port.Write("&lt;3,100&gt;");
          }
  
          void OnClickUp4 (object sender, System.EventArgs e)
          {
              port.Write("&lt;4,100&gt;"); //error here because of port initialization not in same code
          }
  
          void OnClickLeft2 (object sender, System.EventArgs e)
          {
              port.Write("&lt;2,100&gt;"); 
          }
  
          void OnClickRight1 (object sender, System.EventArgs e)
          {
              port.Write("&lt;1,100&gt;");
          }
      }
  }
  </code></pre>