【问题标题】:Refresh a listbox to display new values刷新列表框以显示新值
【发布时间】:2013-09-03 20:32:44
【问题描述】:

这是一个新手问题,很抱歉。

我正在用我在线抓取的值填写我的文本框并将它们传递给列表框,如下所示:

        // textBox1.Text = test.ToString();
        string[] names = result.Split('|');
        foreach (string name in names)
        {

            listBox1.Items.Add(name);
        }

但是,我试图单击一个文件夹并让从那里显示的文件显示在我的 listbox1 中。这是我尝试过的:

   using (var testy = new WebClient())
        {

            test = testy.DownloadString("http://server.foo.com/images/getDirectoryList.php?dir=test_folder");
            string[] names1 = test.Split('|');
            foreach (string name in names1)
            {
                listBox1.Items.Clear();
                listBox1.Items.Add(name);
                listBox1.Update();
            }

        }

但所发生的只是我的列表框清空并且没有刷新。我怎样才能实现我想做的事情?

【问题讨论】:

    标签: c# winforms listbox


    【解决方案1】:

    在你做任何其他事情之前,从 foreach 中删除 clear 和 update

      listBox1.Items.Clear();
      foreach (string name in names1)
      {
    
         listBox1.Items.Add(name);
    
      }
      listBox1.Update();
    

    【讨论】:

      【解决方案2】:

      你的台词

              foreach (string name in names1)
              {
                  listBox1.Items.Clear();
                  listBox1.Items.Add(name);
                  listBox1.Update();
              }
      

      使得对于每个字符串,您都将删除列表中的其他项目。

      我很确定这不是你想要的

      【讨论】:

        【解决方案3】:

        使用绑定源

        BindingSource bs = new BindingSource();
        List<string> names1 = new List();
        bs.DataSource = names1;
        comboBox.DataSource = bs;
        
           using (var testy = new WebClient())
            {
        
                test = testy.DownloadString("http://server.foo.com/images/getDirectoryList.php?dir=test_folder");
                names1.AddRange(test.Split('|'));
                bs.ResetBindings(false);
        
            }
        

        BindingSource 将为您处理一切。

        【讨论】:

          猜你喜欢
          • 2018-09-23
          • 1970-01-01
          • 2016-12-28
          • 2012-04-15
          • 2012-03-29
          • 1970-01-01
          • 2013-03-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多