日期:2014-05-18  浏览次数:21082 次

弄个防止listBox重复添加Items的小东西都脑大啊
依然重复添加了,根本没办法防止重复的items , 了个去啊。

C# code
        private void button2_Click(object sender, EventArgs e)
        {
            IPHostEntry iphost = Dns.GetHostEntry(textBox1.Text);


                    //string ipaddress = ip.AddressFamily.ToString();
                    //listBox1.Items.Add(ipaddress);

                 foreach (IPAddress ip in iphost.AddressList)
                  {
                     if(!listBox1.SelectedItems.Contains(ip.ToString()))
                     {
                         listBox1.Items.Add(ip.ToString());
                     }
                     else
                     {
                         MessageBox.Show("Item Exists!");   
                     }
                  }
        
        }


------解决方案--------------------
你还选中干嘛??

C# code

private void button2_Click(object sender, EventArgs e)
{
       IPHostEntry iphost = Dns.GetHostEntry(textBox1.Text);
        foreach (IPAddress ip in iphost.AddressList)
        {
             if(!listBox1.Items.Contains(ip.ToString()))
              {
                   listBox1.Items.Add(ip.ToString());
              }
               else
               {
                     MessageBox.Show("Item Exists!");   
                }
      }
        
}