数据绑定并不构成负荷工作之前 - 只有在窗体加载初始化组合框项目 [英] Databinding doesn't work before Form Load - ComboBox Items initialized only after Form load

查看:120
本文介绍了数据绑定并不构成负荷工作之前 - 只有在窗体加载初始化组合框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个winform 2组合框,一个人从名单MYLIST其他的我想有从第一的选定索引的所有值,以后所有的值。但我认为这是将项目加载到组合框的一个问题。

I have two ComboBoxes on a winform, one has all the values from a List "MyList" the other I want to have all the values from the selected index of the first, onwards. But I think there is a problem with when items are loaded into the ComboBoxes.

public partial class Form1 : Form
{
    public Form1()
    {
        ComboBox box = new ComboBox();
        box.DropDownStyle = ComboBoxStyle.DropDownList;
        box.DataSource = MyList.ToList();
        box.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
        Tab_Page.Controls.Add(box);

        ComboBox box2 = new ComboBox();
        box2.DropDownStyle = ComboBoxStyle.DropDownList;
        foreach (object o in box.Items)
            {
                box2.Items.Add(o);
            }
        Tab_Page.Controls.Add(box2);
        box2.Items.RemoveAt(0);
        //This last line throws an error
        //"InvalidArgument=Value of '0' is not valid for 'index'."
    }}



这个错误是因为BOX2有其收藏中没有的项目,即使箱有从这个错误被抛出的瞬间列出所有的值。

The error is because box2 has no items in its collection, even though box has all the values from the List at the moment this error is thrown.

所以我不知道如何/时,到底是加载到项目集合项目,如何解决?本

So I was wondering how/when exactly are items loaded to the Items collection and how can I fix this?

推荐答案

数据绑定的形式和控制在 创建 状态和之前的窗体和控件获得可见他们不是在创建状态。

Data-binding will not work before the form and control be in Created status and before the form and controls get visible they are not in Created status.

问题此处是因为这样的事实。您正在使用数据绑定添加在构造项目第一个组合,并且如上所述,首先将数据绑定不会在环路有等工作,项目集合。组合是空的,没有的项目将加入到第二个组合

The problem here is because of above fact. You are using data-binding to add items to first combo in constructor and as mentioned above, the data-binding will not work there and so in the loop, Items collection of first combo is empty yet and no items will be added to second combo.

您可以使用这些选项解决问题:

You can solve the problem using either of these options:


  • 直接添加值项目第一个组合的: box.Items.AddRange(MyList.ToArray( ));

  • 把代码在所示加载形式的事件。

  • 请尝试检索第一个组合的项目前的形式显示。例如呼叫 this.Show(); 数据源第一个组合的赋值后。调用 this.Show(); THIS.VISIBLE = TRUE; 引起的形式和其所有可见的控制得到创建。所以控制应该是可见的,并应以可见形式的可见部分中的一员。

  • Add values directly to Items of first combo: box.Items.AddRange(MyList.ToArray());
  • Put the codes in Shown or Load event of the form.
  • Make the form visible before trying to retrieve Items of first combo. For example call this.Show(); after assigning values to DataSource of first combo. Calling this.Show(); or this.Visible = true; causes the form and all of its visible controls get Created. So the controls should be visible and should be a member of visible part of a visible form.

这篇关于数据绑定并不构成负荷工作之前 - 只有在窗体加载初始化组合框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆