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

Mag*_*num 6 c# winforms

我正在动态创建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;
Run Code Online (Sandbox Code Playgroud)

下面是我尝试和工作的替代解决方案,但为什么我会使用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;
Run Code Online (Sandbox Code Playgroud)

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

Neo*_*isk 4

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

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

在一个全新的 C# 项目中进行了测试,并在listBox1表单上放置了一个 put ,上面的代码位于Form_Load.

编辑:我没有意识到ListBox在运行时创建 a 可能会产生影响,特别是因为何时设置所选项目很重要。这段代码的工作原理:

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);
Run Code Online (Sandbox Code Playgroud)

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

listBox.SetSelected(0, true);
listBox.SetSelected(1, true);
flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox);
Run Code Online (Sandbox Code Playgroud)

有了这个:

flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox);
listBox.SetSelected(0, true);
listBox.SetSelected(1, true);
Run Code Online (Sandbox Code Playgroud)

它应该有效。

  • 这段代码和他的代码之间的区别在于,您在设计时将列表框放在表单上,​​就像OP在运行时将其放在表单上一样。 (2认同)