为什么我的自定义用户控件不会引发鼠标事件?

时间:2010-12-10 16:44:09

标签: c# .net winforms user-controls custom-controls

我想创建一个带有轨迹栏的复合自定义控件,以及某些客户端表单将使用的其他控件。我需要公开一些事件,例如MouseDownMouseUp,以检测拖动的开始和拖动的结束。奇怪的是,MouseDown可以,但不是MouseUp。下面的代码演示了它。这是.NET Framework或跟踪栏中的错误吗?如果没有,我该怎么办呢?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MyTrackbar
{

    public partial class UserControl1 : UserControl
    {
        public delegate void StartDragHandler();
        public delegate void EndDragHandler();

        [Category("Action")]
        [Description("Fires when user starts to drag.")]
        public event StartDragHandler StartDrag;
        [Category("Action")]
        [Description("Fires when user ends to drag.")]
        public event EndDragHandler EndDrag;

        public UserControl1()
        {
            InitializeComponent();
        }

        private void trackBar1_MouseDown(object sender, MouseEventArgs e)
        {
            textBox1.BackColor = Color.Red;
            if (StartDrag != null) {
                StartDrag();
            }

        }

        private void trackBar1_MouseUp(object sender, MouseEventArgs e)
        {
            textBox1.BackColor = Color.White;
            if (EndDrag != null) {
                EndDrag();
            }
        }
    }
}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestMyTrackBar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void userControl11_StartDrag()
        {
            // Work
            textBox1.BackColor = Color.Red;
        }

        private void userControl11_MouseUp(object sender, MouseEventArgs e)
        {
            // Doesn't work !!!!!!!!!!!!
            textBox1.BackColor = Color.White;
        }
    }
}

更新:对于想要检查InitializeComponent的人,即使我没有修改它:

对于自定义控件:

namespace MyTrackbar
{
    partial class UserControl1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            this.textBox1 = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            this.SuspendLayout();
            // 
            // trackBar1
            // 
            this.trackBar1.Location = new System.Drawing.Point(21, 17);
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(356, 45);
            this.trackBar1.TabIndex = 0;
            this.trackBar1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.trackBar1_MouseDown);
            this.trackBar1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.trackBar1_MouseUp);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(31, 68);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(338, 20);
            this.textBox1.TabIndex = 1;
            // 
            // UserControl1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.trackBar1);
            this.Name = "UserControl1";
            this.Size = new System.Drawing.Size(400, 150);
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TrackBar trackBar1;
        private System.Windows.Forms.TextBox textBox1;
    }
}

对于WinForm客户端:

namespace TestMyTrackBar
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.userControl11 = new MyTrackbar.UserControl1();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // userControl11
            // 
            this.userControl11.Location = new System.Drawing.Point(13, 39);
            this.userControl11.Name = "userControl11";
            this.userControl11.Size = new System.Drawing.Size(400, 114);
            this.userControl11.TabIndex = 0;
            this.userControl11.StartDrag += new MyTrackbar.UserControl1.StartDragHandler(this.userControl11_StartDrag);
            this.userControl11.MouseUp += new System.Windows.Forms.MouseEventHandler(this.userControl11_MouseUp);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(45, 160);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(336, 20);
            this.textBox1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(457, 231);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.userControl11);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private MyTrackbar.UserControl1 userControl11;
        private System.Windows.Forms.TextBox textBox1;
    }
}

3 个答案:

答案 0 :(得分:3)

在开始之前,我想澄清一下,answer provided by Hans Passant已经解释了我将要做的事情。所以,如果你觉得这很有帮助,你应该回答他的答案。

然而,从评论交流中可以看出,你并不完全明白他想说的是什么。也许我可以做一些更好的解释,如果你真正了解你的代码中必须改变的内容,也许你会更容易理解正在发生的事情。

让我们从评估当前情况开始:当用户开始拖动轨迹栏时,属于userControl11的文本框会正确地改变颜色。但是,在完成拖动轨道栏后,属于Form1第二个文本框不会变回白色。

您的用户控件已经引发了两个自定义事件StartDragEndDrag,其名称显然表示当前状态。在Form1中,您正在处理用户控件的StartDrag事件,并将第二个文本框的颜色更改为红色。但是,您不是处理EndDrag事件以将颜色更改回白色,而是处理MouseUp事件。这就是为什么它不起作用,以及Hans的答案试图告诉你的原因。 相反,您的表单代码应如下所示:

public partial class Form1 : Form
{
   public Form1()
   {
      InitializeComponent();

      this.userControl11.EndDrag += new MyTrackbar.UserControl1.EndDragHandler(this.userControl11_EndDrag);
   }

   private void userControl11_StartDrag()
   {
      // Works
      textBox1.BackColor = Color.Red;
   }

   private void userControl11_EndDrag()
   {
      // Now also works!
      textBox1.BackColor = Color.White;
   }
}

您已经明白,您需要处理自己提出的自定义StartDrag事件,因此看起来很简单,您需要处理{{1} } event以达到预期的效果。

但是,您可能会问,“为什么EndDrag事件在用户控件内部工作,但在表单内部工作?!”我认为这是你试图争论的.NET Framework中的一个错误。 (提示:我们任何人发现框架中的真正错误的可能性远小于比我们中的一个人对为什么我们看到的行为感到困惑,或者只是遗漏了一些明显的东西。)

原因是,在用户控件中,您正在处理由跟踪栏控件(MouseUp)引发的MouseUp事件。 但该事件不会向上传播。仅仅因为跟踪栏控件引发trackBar1事件而不是意味着包含它的用户控件会引发同一事件。您似乎已经知道MouseUp事件,MouseDown事件与此完全相同。

无需向微软提交错误报告,也不需要任何人对他们的感情造成伤害。

答案 1 :(得分:2)

    private void userControl11_MouseUp(object sender, MouseEventArgs e)
    {
        // Doesn't work !!!!!!!!!!!!
        textBox1.BackColor = Color.White;
    }

您应该使用EndDrag事件,而不是MouseUp。

答案 2 :(得分:1)

目前尚不清楚真正的问题,因为示例中完全没有 InitializeComponent 实现。

所以,我的回答是基于以下断言:

  • UserControl1.trackBar1 TrackBar ,* trackBar1_MouseDown *是 UserControl1.trackBar1.MouseDown 的子网,* trackBar1_MouseUp *是< EM> UserControl1.trackBar1.MouseUp
  • Form1.userControl11 UserControl1 ,* userControl11_StartDrag()*是 Form1.userControl11.StartDrag 事件的订阅者,* userControl11_MouseUp *是 Form1.userControl11.MouseUp 事件的订阅者

“不起作用”是正确的。可能(并且你应该确认它是否)事件 Form1.userControl11.MouseUp 永远不会被引发。 Infact Form1.userControl11.MouseUp UserControl1.trackBar1.MouseUp 不一样; infact UserControl1 类在其嵌入的Control trackBar1 引发 MouseUp 事件时公开 EndDrag 事件。

因此,解决方案是订阅UserControl1.EndDrag事件,而不是鼠标(如Hans Passant所说),以反映正确的行为(我认为)。