Search code examples
c#arrayslistboxbytefill

C# Populating listbox with byte array


private void sendBCode()
    {
            NetworkStream serverStream = clientSocket.GetStream();
            outStream = Encoding.ASCII.GetBytes("0000|ORD|SUPP");

            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

 /*No issues so far, I am sending a data stream in the code above. Now I need 
 to return data:*/

            byte[] inStream = new byte[1500];
            var count = serverStream.Read(inStream, 0, inStream.Length);
            string returndata = Encoding.ASCII.GetString(inStream, 0, count);

 /*The data I am returning looks like: "0000|ORD|SUPPS|MWH|GGR|MBS" , I need to
   split this data and populate a listBox with it, as you can see below, I can split
   the returned data.*/

            string[] s = null;
            clsConn.prdType PRD = new clsConn.prdType();
            s = returndata.Split('|');

             aaaa = s[1];
             bbbb = s[2]; //etc...
     }

The number of spitted values are undetermined, but I want to fill a listbox with every item returned. How would I do that?

Thanks.

EDIT: But what if I want to populate the listBox the same way but excluding the first three items?


Solution

  • You can a range of items by AddRange in listbox Do that like this code:

     NetworkStream serverStream = clientSocket.GetStream();
            outStream = Encoding.ASCII.GetBytes("0000|ORD|SUPP");
    
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
    
            /*No issues so far, I am sending a data stream in the code above. Now I need 
            to return data:*/
    
            byte[] inStream = new byte[1500];
            var count = serverStream.Read(inStream, 0, inStream.Length);
            string returndata = Encoding.ASCII.GetString(inStream, 0, count);
    
            /*The data I am returning looks like: "0000|ORD|SUPPS|MWH|GGR|MBS" , I need to
              split this data and populate a listBox with it, as you can see below, I can split
              the returned data.*/
    
            string[] s = null;
            clsConn.prdType PRD = new clsConn.prdType();
            s = returndata.Split('|');
    
            aaaa = s[1];
            bbbb = s[2]; //etc...
            //Excluding First three items
    
            string[] s_copy = new string[s.Length - 3] ;
            Array.Copy(s, 3, s_copy, 0, s.Length - 3);
    
            ///-------
            listBox1.Items.AddRange(s_copy);