C#用字节数组填充列表框

C#用字节数组填充列表框,c#,arrays,listbox,byte,fill,C#,Arrays,Listbox,Byte,Fill,吐出值的数量尚未确定,但我想用返回的每个项填充一个列表框。我该怎么做 谢谢 编辑: 但是,如果我想以相同的方式填充列表框,但不包括前三个项目,该怎么办?您可以通过在列表框中添加一系列项目 按照以下代码进行操作: private void sendBCode() { NetworkStream serverStream = clientSocket.GetStream(); outStream = Encoding.ASCII.GetByte

吐出值的数量尚未确定,但我想用返回的每个项填充一个列表框。我该怎么做

谢谢

编辑:
但是,如果我想以相同的方式填充列表框,但不包括前三个项目,该怎么办?

您可以通过在列表框中添加一系列项目 按照以下代码进行操作:

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...
     }

我不知道我是否理解您只需要将
AddRange
Windows窗体一起使用

 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);

您可以使用string.Join函数和string.Split创建文本流,使用listbox addrange函数添加项目范围基于我上面发布的代码,我该如何做?您可以使用array.copy并将值复制到新数组并填充它!!
private void Form1_Load(object sender, System.EventArgs e)
{
    string[] s = null;
    clsConn.prdType PRD = new clsConn.prdType();
     s = returndata.Split('|');

    listBox1.Items.AddRange(s);
}