弄个防止listBox重复添加Items的小东西都脑大啊

busuxiaodudu 2012-04-15 11:15:14
依然重复添加了,根本没办法防止重复的items , 了个去啊。

        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!");
}
}

}
...全文
128 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
了解了 这样是可以避免的嘛
busuxiaodudu 2012-04-15
  • 打赏
  • 举报
回复
鸟解了,各位大哥,SelectedItems是选中的项。。
busuxiaodudu 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
来晚了
[/Quote]

晚了
busuxiaodudu 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
你还选中干嘛??


C# code

private void button2_Click(object sender, EventArgs e)
{
IPHostEntry iphost = Dns.GetHostEntry(textBox1.Text);
foreach (IPAddress ip in iphost.AddressList)
……
[/Quote]

够晕菜的,不知道SelectItems是虾米意思呢?
老董杂货铺 2012-04-15
  • 打赏
  • 举报
回复
来晚了
老董杂货铺 2012-04-15
  • 打赏
  • 举报
回复

private void button3_Click(object sender, EventArgs e)
{
List<string> lst = new List<string>();
lst.Add("1");
lst.Add("2");
lst.Add("3");
lst.Add("4");
lst.Add("1");
foreach (string str in lst)
{
if (!listBox1.Items.Contains(str))
{
listBox1.Items.Add(str);
}

}
}
stonespace 2012-04-15
  • 打赏
  • 举报
回复
低级错误,呵呵,
EnForGrass 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

你还选中干嘛??

C# code

private void button2_Click(object sender, EventArgs e)
{
IPHostEntry iphost = Dns.GetHostEntry(textBox1.Text);
foreach (IPAddress ip in iphost.AddressList)
……
[/Quote]
画蛇添足,你只是添加
暖枫无敌 2012-04-15
  • 打赏
  • 举报
回复
你还选中干嘛??


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!");
}
}

}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧