繁体   English   中英

ListBox.DataSource集合与ListBox.Items之间的区别?

[英]The difference between ListBox.DataSource collection versus ListBox.Items?

我正在动态创建Winforms多选ListBox并将其添加到flowpanel控件中。 我从我创建的对象绑定数据源,并验证DataSource实际上有大约14个元素。 当我执行listBox.SetSelected(0, true)我收到一个System.ArgumentOutOfRangeException错误。

我已经确定问题是,虽然DataSource有14个元素,但Item集合没有(0),因此抛出异常。 我的问题是为什么这两者彼此不同,为什么我不简单地在数据源中添加一个foreach项添加到项集合中?

以下是我到目前为止的代码:

case InsertableItemParameter.ParameterType.ListBox:
    //note: two-way bindings are not possible with multiple-select listboxes
    Label lblListBox = new Label();
    lblListBox.Text = param.DisplayText;
    ListBox listBox = new ListBox();
    listBox.DataSource = param.Values;
    listBox.DisplayMember = "Value";
    listBox.SelectionMode = SelectionMode.MultiExtended;
    listBox.Size = new System.Drawing.Size(flowPanel.Size.Width - lblListBox.Size.Width - 10, 100);
    listBox.SetSelected(0, true);   //will throw argument out of range exception here!
    listBox.SetSelected(1, true);
    flowPanel.Controls.Add(lblListBox);
    flowPanel.Controls.Add(listBox);
    flowPanel.SetFlowBreak(listBox, true);
    break;

下面是我尝试和工作的替代解决方案,但为什么我会使用DataSource与Items集合?

case InsertableItemParameter.ParameterType.ListBox:
    //note: two-way bindings are not possible with multiple-select listboxes
    Label lblListBox = new Label();
    lblListBox.Text = param.DisplayText;
    ListBox listBox = new ListBox();
    //listBox.DataSource = param.Values;
    listBox.DisplayMember = "Value";
    listBox.SelectionMode = SelectionMode.MultiExtended;
    listBox.Size = new System.Drawing.Size(flowPanel.Size.Width - lblListBox.Size.Width - 10, 100);
    listBox.BeginUpdate();
    foreach (String paramater in param.Values)
    {
        listBox.Items.Add(paramater);
    }
    listBox.EndUpdate();
    listBox.SetSelected(0, true);
    listBox.SetSelected(1, true);
    flowPanel.Controls.Add(lblListBox);
    flowPanel.Controls.Add(listBox);
    flowPanel.SetFlowBreak(listBox, true);
    break;

答案 :感谢所有回复。 这里的问题是可见性和win-form呈现。 虽然除了少数人之外没有真正解决DataSource和Items集合之间的差异,但是在完成绘制表单之后调用SetSelected()方法解决了我的问题的真正来源。 这导致我的应用程序设计中的许多问题,我必须解决,但这是问题所在。 请参阅我标记为答案的回复。

您的问题可能在其他地方,因为此代码工作正常:

string[] ds = {"123","321"};
listBox1.DataSource = ds;
listBox1.SetSelected(1, true);
MessageBox.Show(listBox1.Items.Count.ToString()); //returns 2

在一个全新的C#项目中测试,其中listBox1放在表单上,​​上面的代码位于Form_Load

编辑:我没有意识到在运行时创建一个ListBox可能会有所作为,特别是因为它在设置所选项时很重要。 此代码有效:

string[] ds = { "123", "321" };
ListBox lst = new ListBox();
lst.DataSource = ds;
lst.Size = new Size(100,100);            
this.Controls.Add(lst);
//make sure to call SetSelected after adding the ListBox to the parent
lst.SetSelected(1, true);

感谢@Brad指出这一点。 回到原来的问题,替换这个:

listBox.SetSelected(0, true);
listBox.SetSelected(1, true);
flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox);

有了这个:

flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox);
listBox.SetSelected(0, true);
listBox.SetSelected(1, true);

它应该工作。

来自MSDN的项目

使用此属性可以获取对当前存储在ListBox中的项列表的引用。 使用此引用,您可以添加项目,删除项目以及获取集合中项目的计数。 有关可以使用项集合执行的任务的更多信息,请参阅ListBox.ObjectCollection类参考主题。

数据源来自MSDN

实现IList或IListSource接口的对象,例如DataSet或Array。 默认值为null

我不是这方面的专家,但从我看来,似乎Items允许你添加/修改列表中的内容,而Datasource检索和设置内容。

您有两种选择如何在ListBox获取数据。 您可以设置DataSource ,也可以通过listBox.Items.Add(paramater)手动添加项目。 你不能两者都做,因为他们会互相踩到你的错误

...cannot add items to the Item collection when DataSource is set.

我不知道为什么有两个不同的收藏品。 Items属性似乎更简单。

我找到了例外的原因:显然你必须按照特定的顺序做事,比如:

    //init the listbox
    var listBox1 = new ListBox();
    listBox1.Location = new System.Drawing.Point(122, 61);
    listBox1.Size = new System.Drawing.Size(205, 147);
    listBox1.SelectionMode = SelectionMode.MultiExtended;
    Controls.Add(listBox1); //<-- point of interest

    //then set the DataSource
    listBox1.DataSource = lst;
    listBox1.DisplayMember = "Name";
    listBox1.ValueMember = "Age";

    //then set the selected values
    listBox1.SetSelected(0, true);
    listBox1.SetSelected(1, true);

我的Test类看起来像这样:

public class Test
{
    private static Random r = new Random();
    public Test (string name)
    {
        Name = name;
        Age = r.Next(16, 45);
    }

    public string Name { get; set; }

    public int Age{ get; set; }
}

并且lst声明如下:

    var lst = new List<Test>()
                  {
                      new Test("jens"),
                      new Test("Tom"),
                      new Test("John"),
                      new Test("Don"),
                      new Test("Jenny"),
                  };

仅当Control可见时,才会从DataSource填充Items集合。 由于您动态创建控件,因此它不会添加到父控件中,因此不可见。 因此,您首先需要一个在屏幕上可见的控件。 在代码中设置DataSource,然后在FlowChart上显示Control之前设置所选项,因为它未添加到Parent控件。 您应该更改语句的顺序。 您应该将listBox添加到FlowPanel ,它将从DataSource填充Items集合,您可以在其上执行SetSelected()方法。 试试这个并记下初始代码执行的更改顺序:

ListBox listBox = new ListBox();
listBox.DataSource = param.Values;
listBox.DisplayMember = "Value";
listBox.SelectionMode = SelectionMode.MultiExtended;
listBox.Size = new System.Drawing.Size(flowPanel.Size.Width - lblListBox.Size.Width - 10, 100);
flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox); //notice that you first add the listBox to the flowChart
listBox.SetSelected(0, true);   //and then you have items in the Items collection which you can select
listBox.SetSelected(1, true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM