今天做到一个功能,使用openFileDialog,选择文件,然后将选择了的文件放入listBox中,想判断如果要添加的数据在LISTBOX中已存在,就不要添加。

想着是用循环遍历,但是感觉很麻烦,网上找了个方法,

直接贴上代码

string str = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
{
str
= openFileDialog1.FileNames.GetValue(i).ToString();
//判断是否在LISTBOX集合中存在,不存在就添加,存在就不管
if (!listBox1.Items.Contains(str))
{
listBox1.Items.Add(str);
}


}

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2021-07-24
  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
相关资源
相似解决方案