直接上代码 请帮我看看 怎么改?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Timers;namespace ElecMenuServerApp
{
    public partial class Form1 : Form
    {
        private int listenPort = 8080;
        private static int buffer_size = 1024;
        private static byte[] receiveBytes = new byte[buffer_size];
        private static int bytesCount;        private Socket serverSokect = null;
        private static Socket client = null;        private List<string> datas = new List<string>();
                public Form1()
        {
            InitializeComponent();            startRecBtn.Enabled = true;
            stopRecBtn.Enabled = false;
        }        private void startRecBtn_Click(object sender, EventArgs e)
        {
            Thread recThread = new Thread(new ThreadStart(Receive));
            recThread.Start();
            recThread.IsBackground = true;            startRecBtn.Enabled = false;
            stopRecBtn.Enabled = true;
        }        private void stopRecBtn_Click(object sender, EventArgs e)
        {
            if (serverSokect != null)
            {
                serverSokect.Close();
                serverSokect = null;
            }            startRecBtn.Enabled = true;
            stopRecBtn.Enabled = false;
        }        private void Receive()
        {
            try
            {
                serverSokect = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint ipe = new IPEndPoint(IPAddress.Any, listenPort);
                serverSokect.Bind(ipe);//绑定
                serverSokect.Listen(100);                while (true)
                {
                    client = serverSokect.Accept();                    bytesCount = client.Receive(receiveBytes, receiveBytes.Length, 0);
                    string data = "收到信息:" + "\n" + Encoding.UTF8.GetString(receiveBytes, 0, bytesCount);
                    //
                    if (data != null)
                    {
                        datas.Add(data);
                    }
                    this.Invoke(new AddCon(addcon));
                    //
                    client.Close();
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        private delegate void AddCon();
        private void addcon()
        {
            if (datas.Count > 0)
            {
                for (int i = 0; i < datas.Count; i++)
                {
                    RichTextBox rTextBox = new RichTextBox();
                    rTextBox.Location = new Point(3, 21 + i * 105);
                    rTextBox.Size = new Size(450, 100);
                    rTextBox.BorderStyle = BorderStyle.FixedSingle;
                    rTextBox.ScrollBars = RichTextBoxScrollBars.Both;
                    rTextBox.Text = datas[i];
                    rTextBox.Show();
                    if (panel1.Controls.Count < datas.Count)
                    {
                        panel1.Controls.Add(rTextBox);
                    }
                }
            }
        }        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            
        }
    }
}

解决方案 »

  1.   

    我要改成  每当sokect.close()之前 就执行判断datas的count 然后生成richtextbox的序列 并且 每个序列都显示datas里面相应的内容
      

  2.   


    报错倒是没有报错,就是每当我发送信息过来的时候,必须最小化然后最大化窗口过后才有反应。
    还有就是 当datas.count > 1的时候 richtextbox永远只显示一个 发再多的信息过来也没用 
      

  3.   


    楼主 你的panel1里面每次都添加进去了空间  只是他们的坐标都是相同的  这些控件被覆盖掉了
       for (int i = 0; i < datas.Count; i++)rTextBox.Location = new Point(3, 21 + i * 105);if (panel1.Controls.Count < datas.Count)
    你看下这三条代码   
    进入循环后    i=0   当i=0时 你的if语句一定是panel1.Controls.Count < datas.Count 这个你应该能理解吧  先有传值过来 然后在生成控件 而且一定只比datas.Count小1        所以坐标就是(3,21)   
    当i=1 或i=2,=3....   此时因为你的panel1里面又添加了一个控件  自然 就不会再小于 datas.Count了后面的循环 if里面的比较都是相等的    所以   你每次进入这个循环   只有i=0的时候才有效   而i=0  坐标就又是(3,21)  控件重复
    你改下将rTextBox.Location = new Point(3, 21 + i * 105);  改成rTextBox.Location = new Point(3, 21 + (datas.Count-1) * 105);
      

  4.   


    但是还有个问题 现在是判断panel1.controls.count < datas.count
    如果我在panel1里面除了添加richtextbox空间 我还要添加button label等空间 那怎么判断了?
      

  5.   

    那样就当然无法用这个做判断啦其实你的这个for循环是多余的  去掉这个循环吧  这样  连if语句都可以省略掉
      

  6.   


    如果去掉循环 当datas.count > 0 并且没有数据进来的时候 ,最后一个窗口岂不是一直重复叠加? 
      

  7.   

    楼主 你的这段代码不完整  你都没给出AddCon在哪里被你调用了  所以我也不知道在什么事件里激发
    你就定义一个静态变量static judge=0   在Receive方法中  
    if (data != null)
     {
        datas.Add(data);
        judge=1;
     }
    else
    {
       judge=0;
    }
    然后那个 if (datas.Count > 0)
    用if(judge==1)  来替代