我使用了用户控件,编译通过后在工具栏里出现了组件,然后其中一个拖到窗体里就提示“创建组件xxx失败&……未将对象设置引用到实例”,其他用户控件正常

解决方案 »

  1.   

    在控件的Load方法里加上 DesignMode 的判断。
      

  2.   

    检查自定义控件中load过程或者sub new过程中的代码,尽量不要在控件最终显示前操作与控件显示或者加载有关的代码.
      

  3.   

    感谢楼上两位的回答,果然我把其中的一个加载控件事件移动后就可以了:
            public panelMyLibrary()
            {
                InitializeComponent();
                //BindControls();注释这句
            }        void BindControls()
            {
                List<Book> listBooks = bookSevices.GetAllBooks();
                int len = listBooks.Count;
                if (listBooks.Count > 0)
                {
                    dataGridViewBook.RowCount = len;
                    for (int i = 0; i < len; i++)
                    {
                        DataGridViewTextBoxCell titleCell = (DataGridViewTextBoxCell)dataGridViewBook.Rows[i].Cells[1];
                        titleCell.Value = listBooks[i].title;
                        DataGridViewTextBoxCell urlCell = (DataGridViewTextBoxCell)dataGridViewBook.Rows[i].Cells[2];
                        urlCell.Value = listBooks[i].url;
                        DataGridViewTextBoxCell dateCell = (DataGridViewTextBoxCell)dataGridViewBook.Rows[i].Cells[3];
                        dateCell.Value = (string.Format("{0:D}", listBooks[i].create_date));                }
                }
            }