从另一个班级的列表中填充一个组合框

时间:2018-12-25 08:00:43

标签: c# winforms

因此,我试图通过调用另一个将返回n is not None的类中的方法来从ComboBox填充List

List

1 个答案:

答案 0 :(得分:0)

嗯,List<T>没有GetCC(),但是Line有;像这样的东西:

private void startingStation_SelectedIndexChanged(object sender, EventArgs e) {
  //TODO: you have to obtain Line instance here
  Line line = new Line();

  // In order to avoid constant redrawing: we want repaint combobox once,
  // after all items being inserted
  startingStation.BeginUpdate();

  try {
    // It is line (not List<T>) that provides GetCC() method
    foreach (Station station in line.GetCC()) {
      // String interpolation - $"..." is more readable than concatenation
      startingStation.Items.Add($"{station.Number} {station.Desc}");
    } 
  }
  finally {
    startingStation.EndUpdate();
  }
}

编辑:您可以借助 Linq (专门为查询 设计的)改善GetCC()的实现:

using System.Linq;

...

class Line : Station {
  ...
  public List<Station> GetCC() {
    return file
      .Where(item => item.Number.Contains("CC"))
      .ToList();
  }
  ...
}
相关问题