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

时间:2014-07-01 12:34:55

标签: c# arrays listbox byte fill

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

spitted值的数量未确定,但我想在每个返回的项目中填充一个列表框。我该怎么做?

感谢。

修改 但是如果我想以相同的方式填充listBox但排除前三项呢?

2 个答案:

答案 0 :(得分:1)

您可以在列表框中按AddRange列出一系列项目 这样做就像这样:

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

答案 1 :(得分:1)

我不知道我是否理解你只需要AddRange使用Windows Forms

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);
}
相关问题