C# 这段代码和他的代码是,您在设计时将listbox放在窗体上,就像OP在运行时将它放在窗体上一样。由于您在设计时将控件放在父控件上,因此在设置数据源时可以看到您的控件,使其正常工作,OP在尝试访问Items集合后设置父控件。因此这是一个呈现问题。问题是我不能

C# 这段代码和他的代码是,您在设计时将listbox放在窗体上,就像OP在运行时将它放在窗体上一样。由于您在设计时将控件放在父控件上,因此在设置数据源时可以看到您的控件,使其正常工作,OP在尝试访问Items集合后设置父控件。因此这是一个呈现问题。问题是我不能,c#,winforms,C#,Winforms,这段代码和他的代码是,您在设计时将listbox放在窗体上,就像OP在运行时将它放在窗体上一样。由于您在设计时将控件放在父控件上,因此在设置数据源时可以看到您的控件,使其正常工作,OP在尝试访问Items集合后设置父控件。因此这是一个呈现问题。问题是我不能将一个项目设置为选中的项目,直到表单绘制完成,这是非常不幸的。我需要解决这个问题,否则我必须跟踪创建的所有listbox元素,以便稍后绑定到这些值:(是的,我发现了这一点。我在添加的横线下方添加了一些额外的代码,以演示禁用.DataSource


这段代码和他的代码是,您在设计时将listbox放在窗体上,就像OP在运行时将它放在窗体上一样。由于您在设计时将控件放在父控件上,因此在设置数据源时可以看到您的控件,使其正常工作,OP在尝试访问Items集合后设置父控件。因此这是一个呈现问题。问题是我不能将一个项目设置为选中的项目,直到表单绘制完成,这是非常不幸的。我需要解决这个问题,否则我必须跟踪创建的所有listbox元素,以便稍后绑定到这些值:(是的,我发现了这一点。我在添加的横线下方添加了一些额外的代码,以演示禁用.DataSource对象并仅使用Items集合。但是,如果您查看Neolik在下面写的另一个答案,您可以看到设置DataSource并使用SetSelected对他有效。我正在试图找出为什么n噢
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;
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;
...cannot add items to the Item collection when DataSource is set.
string[] ds = {"123","321"};
listBox1.DataSource = ds;
listBox1.SetSelected(1, true);
MessageBox.Show(listBox1.Items.Count.ToString()); //returns 2
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);
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);
    //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);
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; }
}
    var lst = new List<Test>()
                  {
                      new Test("jens"),
                      new Test("Tom"),
                      new Test("John"),
                      new Test("Don"),
                      new Test("Jenny"),
                  };
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);