c# – ListBox.DataSource集合与ListBox.Items之间的区别?
发布时间:2020-12-15 08:25:21  所属栏目:百科  来源:网络整理 
            导读:我正在动态创建 Winforms多选ListBox并将其添加到flowpanel控件中.我从我创建的对象绑定数据源,并验证DataSource实际上有大约14个元素.当我执行listBox.SetSelected(0,true)时,我收到一个System.ArgumentOutOfRangeException错误. 我已经确定问题是,虽然Data
                
                
                
            | 我正在动态创建 
 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);
    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); 它应该工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
