-2

我有一个列表框和一个列表视图。当我在 Listbox 上选择多个项目并单击添加时,它将第一个选定的项目添加到列表视图中。我无法克服这个问题。同时,扩展了Listbox选择模式。

void reinit()
{
    if (_adrlinks == null)
        return;
    object li = listAdr.SelectedItem;
    listAdr.Items.Clear();
    foreach (LnkAdresse ele in _adrlinks) {
        if (((Filter == eFilter.all) || (Filter == eFilter.basic && ele.RType <= 1) ||
             (Filter == eFilter.synteilnehmer && ele.RType == 8) ||
             (Filter == eFilter.deliveryteam && ele.RType >= 16 && ele.RType <= 31) ||
             (Filter == eFilter.explorationteam && ele.RType >= 32 && ele.RType <= 63)
            ) && (int)ele.mut >= 0)
        {
            listAdr.Items.Add(ele);  //listAdr is my listview
            if (ele == li)
                listAdr.SelectedItem = li;
        }
    }
    //NotifyContent("changeDoc", "", "");
}
4

2 回答 2

1

虽然不直接回答您的问题,但我建议您使您的代码更具可读性和可维护性。也许你的问题会消失?

一个建议:创建一个像这样的过滤器方法

private bool TakeLinkAddress(eFilter filter, LnkAdresse linkAddress)
{
    if ((int)linkAddress.mut < 0) {
        return false;
    }
    int rType = linkAddress.RType;
    switch (filter) {
        case eFilter.all:
            return true;
        case eFilter.basic:
            return rType <= 1;
        case eFilter.synteilnehmer:
            return rType == 8;
        case eFilter.deliveryteam:
            return rType >= 16 && rType <= 31;
        case eFilter.explorationteam:
            return rType >= 32 && rType <= 63;
        case else:
            return false;
    }
}

也给你的变量更好的名字。现在你的循环看起来好多了:

foreach (LnkAdresse linkAddress in _adrlinks) {
    if (TakeLinkAddress(filter, linkAddress)) {
        linkAddressListView.Items.Add(linkAddress);
        if (linkAddress == selectedLinkAddress) {
            linkAddressListView.SelectedItem = selectedLinkAddress;
        }
    }
}
于 2012-09-28T23:01:06.840 回答
1

您的问题非常不清楚,要从列表框中获取多个选定项目,您可以使用 SelectedItems 属性,但是如果您这样做,我不得不对您想用它做什么做出巨大的猜测。

或许

void reinit()
{
  if (_adrlinks == null) return;
  listAdr.Items.Clear();
  foreach(var li in ListAdr.SelectedItems)
  {
    foreach (LnkAdresse ele in _adrlinks)
    {
      if (((Filter == eFilter.all) || (Filter == eFilter.basic && ele.RType <= 1) || (Filter == eFilter.synteilnehmer && ele.RType == 8)
                || (Filter == eFilter.deliveryteam && ele.RType >= 16 && ele.RType <= 31) || (Filter == eFilter.explorationteam && ele.RType >= 32 && ele.RType <= 63))
                && (int)ele.mut >= 0)
      {
        listAdr.Items.Add(ele);  //listAdr is my listview
        // commented out as it won't ever be true in a multiselect scenario
        // and wouldn't make any sense if it did 
        //if (ele == li) listAdr.SelectedItem = li;
      }
    }
  }
  //NotifyContent("changeDoc", "", "");
}

也许 ???

于 2012-09-28T21:55:17.217 回答