【问题标题】:listbox items disappearing / not displaying when inserting插入时列表框项目消失/不显示
【发布时间】:2012-08-02 19:45:25
【问题描述】:

我正在两个列表框之间创建一个数据传输控件。 我在两个框中都显示了项目,其中包含数据的文本和属性值。 当我从一个转移到另一个时,我可以访问转移的项目(使用调试)并查看 text 属性(不是 value 属性),但它没有显示在我发送到的列表框中。 我什至尝试过刷新对象,但没有成功。

谁能告诉我我做错了什么?

private void btnToLeft_Click(object sender, EventArgs e)
    {
            Telerik.WinControls.UI.RadListDataItem item = new Telerik.WinControls.UI.RadListDataItem(lstRight.SelectedItem.DisplayValue.ToString(), lstRight.SelectedItem.Value);
            lstLeft.Items.Add(item);
            lstRight.Items.RemoveAt(lstRight.SelectedItem.RowIndex);
            lstLeft.Refresh();
            lstRight.Refresh();
    }

【问题讨论】:

    标签: c# winforms visual-studio-2008 telerik


    【解决方案1】:

    这是我用于类似情况的代码。

    private void btnToLeft_Click(object sender, EventArgs e)
    {
          if (lstRight.Items.Count == 0) { return; }
          if (lstRight.SelectedItem == null) { return; }
    
          RadListDataItem item = lstRight.SelectedItem;
          lstRight.Items.Remove(item);
          lstLeft.Items.Add(item);
    }
    

    你可以像这样让它更通用一点。

    private void MoveToTargetListBox(RadListControl sourceListBox, RadListControl targetListBox)
    {
      try
      {
        if (sourceListBox.Items.Count == 0) { return; }
        if (sourceListBox.SelectedItem == null) { return; }
    
        RadListDataItem item = sourceListBox.SelectedItem;
        sourceListBox.Items.Remove(item);
        targetListBox.Items.Add(item);
      }
      catch (Exception ex)
      {
        //handle Exception
      }
    }
    
    private void btnToLeft_Click(object sender, EventArgs e)
    {
      MoveToTargetListBox(lstRight, lstLeft);
    }
    
    private void btnToRight_Click(object sender, EventArgs e)
    {
      MoveToTargetListBox(lstLeft, lstRight);
    }
    

    【讨论】:

    • 谢谢,这几乎对我有用 - 但是,当我将物品移过来时,价值就丢失了。 ...知道如何让它保持/持久吗?
    【解决方案2】:

    我想我明白了。 . .我引用了 lstRight.SelectedItem.DisplayValue.ToString() 而不是文本值 - lstRight.SelectedItem.Text

    现在好像可以了。 ... !

    【讨论】:

      猜你喜欢
      • 2013-10-14
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多