如何在用户自定义的构造函数中传入相应的参数。    public partial class UserControl1 : UserControl
    {
        public UserControl1(bool temp)
        {
            InitializeComponent();
            textBox1.Enabled = temp;
        }
    }
 以上是这个用户自定义的控件的默认构造函数,编译能通过,但是当把这个控件拖放到主窗体时,会出现报错,因为没有找到默认的构造函数,然后我到主窗体的 InitializeComponent()里面加入相应的语句。如下》
{
            //
            // UserControl1
            //
            this.userControl1.Location = new System.Drawing.Point(100,37);
            this.userControl1.Name = "Jacky";
            this.userControl1.Size = new System.Drawing.Size(200, 300);
            this.userControl1.TabIndex = 1;            this.Controls.Add(this.userControl1);}        private UserControl1 userControl1 = new UserControl1(false); //此处进行这个构造函数的调用,
但是回到主窗体的设计界面,报错!
错误提示为:
变量“userControl1”未声明或从未赋值,可是在主窗体的InitializeComponet(){}下面我明明是加入了
private UserControl1 userControl1 = new UserControl1(false); 
请大家发表发表个人看法。
当然用属性是可以实现的,但我不想用属性那么麻烦。
高手顺便讲一讲原理,为什么用户控件的构造函数就不能加上参数呢?而窗体就能加上参数传递。谢谢各位大虾。

解决方案 »

  1.   

    你还没  private UserControl1 userControl1 = new UserControl1(false); 的时候
    主窗体已经 InitializeComponent()
    this.userControl1.Location = new System.Drawing.Point(100,37); 
                this.userControl1.Name = "Jacky"; 
                this.userControl1.Size = new System.Drawing.Size(200, 300); 
                this.userControl1.TabIndex = 1;             this.Controls.Add(this.userControl1); 
    这时候主窗体能认识userControl1这东西吗??
      

  2.   

    主窗口创建的第一步肯定是构造 执行InitializeComponent()
    然后才实例化类成员 
      

  3.   

    呵呵确实如此,你只能使用属性。你运气还不错,还能放上去,在2003里,如果你只定义一个有参数的UserControl构造函数,而没有定义自己的无参构造函数,你根本就放不上窗口,更别提编译了。我想这是整个系统在设计的时候的一个,主要是为了给设计时提供一些帮助。因为你在打开设计器时,IDE无法提供构造函数的参数,即使有可能构造的过程也相当复杂。作为辅助的解决手段,我想只能是你定义两个构造函数,一个是无参数的,而一个是由你自己定义的。并尽可能的减少那个无参构造函数的可见性,比如使用internal,呵呵。
      

  4.   

    楼主也可以看看这个帖子
    http://topic.csdn.net/u/20080828/10/4abc97a2-9519-44f0-8f09-77780a198a1d.html
      

  5.   

    需要引用请公开
     private UserControl1 userControl1 = new UserControl1(false); //此处进行这个构造函数的调用, 这个是你的....你是私有的.....你认为私有的别人能访问吗?UP,拿点小分,呵呵
      

  6.   

    UserControl1:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsControlLibrary1
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1(int x)
            {
                InitializeComponent();
                label1.Text = x.ToString();
            }
        }
    }
    form1:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using WindowsFormsControlLibrary1;namespace WindowsFormsApplication15
    {
        public partial class Form1 : Form
        {
         
            public Form1()
            {
                InitializeComponent();
                UserControl1 c = new UserControl1(1000);
                c.Parent = this; 
            }
        }
    }
    你不能直接拖一个到窗口,会报错,但可以动态生成
      

  7.   

    To:justindreams,不好意思,你来迟了一点