如何删除列表框

时间:2016-07-15 08:32:13

标签: c# asp.net

我想从此随机数中删除异常值。即列表框编号是2 10 12 60 ...我想删除2和60.

Random r = new Random();

for (int i = 0; i < 30; i++)
{
    lst1.Items.Add(r.Next(1, 60).ToString());
    lst2.Items.Add(r.Next(1, 1000).ToString());
}

1 个答案:

答案 0 :(得分:1)

首先准备列表,然后将其分配给UI:

  // Simplest, not thread safe
  private static Random r = new Random();
  ...

  private static List<int> MakeMyList(int count, int topBorder) {
    // Raw list with outliners
    List<int> list = Enumerable
      .Range(0, count)
      .Select(i => r.Next(1, topBorder))
      .ToList();

    //TODO: compute the tolerable interval here
    double leftBorder = ...
    double rightBorder = ...

    // outliners dropping
    list.RemoveAll(item => item < leftBorder || item > rightBorder);

    return list;       
  }

...

  // Assigning to the UI
  foreach(item in MakeMyList(30, 60))
    lst1.Items.Add(item.ToString()); 

  foreach(item in MakeMyList(30, 1000))
    lst2.Items.Add(item.ToString());