遇见一个问题,在构造函数最后一行为控件注册一个drag事件居然无效,放到form_load里才有效.why?

解决方案 »

  1.   

    构造函数先于form_load,那时控件都还没有加载,怎么可能会被注册事件
      

  2.   

    因为Winform用了Windows操作系统的Window。
    注册一个drag要给出目标窗口, 当form_load的时候,窗口句柄已经创建了,这时注册drag有效。
      

  3.   

    那为什么,在InitializeComponent()中系统自动生成的代码注册的事件就OK呢?
      

  4.   


    不懂你玩winform不?,你创建一个事例,然后分步运行就很容易理解了他先是进入一个构造方法         public MainForm()
            {
                InitializeComponent();       
            }        private void InitializeComponent()
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
                this.bnt_4 = new System.Windows.Forms.Button();
                this.pictureBox1 = new System.Windows.Forms.PictureBox();
                this.bnt_1 = new System.Windows.Forms.Button();
                this.bnt_2 = new System.Windows.Forms.Button();
                this.bnt_5 = new System.Windows.Forms.Button();
                this.bnt_3 = new System.Windows.Forms.Button();
                this.pb_min = new System.Windows.Forms.PictureBox();
                this.pb_exit = new System.Windows.Forms.PictureBox();
                this.label1 = new System.Windows.Forms.Label();
                this.bnt_6 = new System.Windows.Forms.Button();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.pb_min)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.pb_exit)).BeginInit();
                this.SuspendLayout();
               ........
             }
    你会发现他都是先定义了就去加绑事件
    如果你在构造方法里写,那么就相当于在InitializeComponent()之前去绑定了,都时都还没有初使化
      

  5.   


    你根本没有看清楼主的描述!楼主说的那句话是放在 initializecomponent 方法后面的,也就是说那个时候控件都已经创建完成,在那个时候,你绑定其它事件都是没有问题的,之所以有问题,你根本没有解释清楚,#2 说的很清楚,是因为 window handler 的问题,而不是加载不加载的问题。
      

  6.   

    如果放在initializecomponent 之后是没有问题,看看代码有没有其它方面的问题。