这是我的代码,在线等候。做的是一个获取局域网内的IP地址,我想在for循环完之后就结束这个事件,请问这个事件这么写???????
private void Form2_Paint(object sender, PaintEventArgs e)
        {
                        SolidBrush sb = new SolidBrush(Color.Black);
            //PingIP地址
            Ping ping = new Ping();
            int i = 0;
            for (i= 140; i < 145;i++ )
            {
               
                string myip = s + i.ToString();                byte[] ips = Array.ConvertAll<string, byte>(myip.Split('.'), Convert.ToByte);
                myip = string.Join(".", Array.ConvertAll<byte, string>(ips, Convert.ToString));
                //转换IP地址
                IPAddress ipaddress = IPAddress.Parse(myip);
                PingReply pReply = ping.Send(ipaddress);                if (pReply.Status == IPStatus.Success)
                {                    System.Windows.Forms.Application.DoEvents();
                    IPHostEntry hostEntry = Dns.GetHostByAddress(ipaddress);
                    name = hostEntry.HostName.ToString();
                    string str_ServerIp = Dns.Resolve(Dns.GetHostName()).AddressList[0].ToString();                    if (ipaddress.ToString() == str_ServerIp)
                    {
                        listBox1.Items.Add(ipaddress + "   " + name + "本机IP");
                    }
                    else
                    {
                        listBox1.Items.Add(ipaddress + "   " + name);                    }                    PictureBox pictBox = new PictureBox();
                    pictBox.Image = Image.FromFile(@"E:\工作文件夹\HuoQuIP\WinLanIP\Images\server.jpg");
                    pictBox.Location = new Point(x + 40, y + 40);
                    pictBox.Size = new Size(36, 29);                    Label lab = new Label();
                    lab.Location = new Point(pictBox.Location.X, pictBox.Location.Y + pictBox.Height);
                    lab.Text = hostEntry.AddressList[0].ToString();                    this.Controls.Add(lab);
                    this.Controls.Add(pictBox);
                    int x1 = picMyImg.Location.X;  //获取界面已有的X,Y的坐标
                    int y1 = picMyImg.Location.Y;                    int x2 = pictBox.Location.X;   //获取动态生成的pictBox的X,Y坐标
                    int y2 = pictBox.Location.Y + pictBox.Height;                    Graphics gh = e.Graphics;
                    gh.DrawLine(p, x1, y1, x2, y2);  //用于连线操作                    x = x + 60;
                }            }
          
            
                    }

解决方案 »

  1.   

    执行   this.Paint -= new PaintEventHandler(Form2_Paint);
      

  2.   

        不停的Paint,说明中间有某个操作会激发Paint事件,使得程序一直嵌套着执行。可惜我半天没看出来是哪一步触发了Paint事件,你最好单步跟踪下,注意调试时别让别的窗体覆盖你的Form2.    不过最好不要把这样的代码放在private void Form2_Paint(object sender, PaintEventArgs e)事件里。为什么
    不拖个按钮,然后点击下,把处理代码放在private void button1_Click(object sender, EventArgs e)里呢?