title: Making a stream with MakeStream() in windows forms
body: <p>I want to make a stream in my c# code to send data. The sending of the data happens when you click on a button in windows forms. Because I have multiple buttons I implemented a separate send function. However when I try to "NetworkStream stream = tcpclnt.GetStream();" in the Form class I get the error that the SendMessage() function cannot find 'stream'.</p>
  
  <p>I already tried to make a separate function for making the stream but because those aren't global my other function can't recognise 'stream'</p>
  
  <p>I also lookt at the error but I cannot find a solution where my other functions still have access to 'stream'.</p>
  
  <p>Here is my first part of the code:</p>
  
  <pre><code>   public partial class Form1 : Form
      {
      List&lt;Panel&gt; listPanel = new List&lt;Panel&gt;();
      TcpClient tcpclnt = new TcpClient(); // Makes a new TcpClient
  
      public Form1()
      {
          InitializeComponent();
          NetworkStream stream = tcpclnt.GetStream();
      }
  
  
      private void ConnectButton_Click(object sender, EventArgs e)
      {
          ConnectPanel.BringToFront();
      }
  
     public void MakeConnectionButton_Click(object sender, EventArgs e)
      {
          try // Error handeling
          {
  
              int x = 0; // Sets port number 0
              Int32.TryParse(PortTextBox.Text, out x); // Convert string from textbox to int
              tcpclnt.Connect(HostTextBox.Text, x); // uses the ip-address and port to connect to server
              MessageconnectBox.Text = "Connected"; // Shows user that connection is made
  
          }
          catch (Exception ex1)
          {
              ErrorMessageBox1.Text = "Error, client cannot connect" + ex1.StackTrace; // Writes error to messageconnectbox
          }
      }
  
     private void Profile8Button_Click(object sender, EventArgs e)
      {
          Profile8Panel.BringToFront();
          try
          {
              String ReceiveData = String.Empty;
              ReceiveData = SendMessage("1018");
          }
          catch (Exception ex13)
          {
              ErrorMessageBox1.Text = "Error, in profile 8 button" + ex13.StackTrace; // Writes error to messageconnectbox
          }
      }
  </code></pre>
  
  <p>....."a lot of other functions that call SendMessage()"</p>
  
  <pre><code>   public string SendMessage(string message)
      {
          Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); // Translate the passed message into ASCII and store it as a Byte array.
          stream.Write(data, 0, data.Length);  // Send the message to the connected TcpServer.
          Console.WriteLine("Sent: {0}", message);
          data = new Byte[256]; // Buffer to store the response bytes.
          String responseData = String.Empty;  // String to store the response ASCII representation.
          Int32 bytes = stream.Read(data, 0, data.Length); // Read the first batch of the TcpServer response bytes.
          responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
          return responseData;
      }
  </code></pre>
  
  <p>}</p>