title: Why are mouse events not raised by my custom user control?
body: <p>I want to create a composite custom control with trackbar and others that will be used by some client forms. I need to expose some events like <code>MouseDown</code> and <code>MouseUp</code> to detect start of drag and end of drag. Weirdly, <code>MouseDown</code> is OK but not <code>MouseUp</code>. The code below demonstrates it. Is this a bug in the .NET Framework or of the trackbar? If not, how should I do it instead?</p>
  
  <pre><code>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;
          }
      }
  }
  </code></pre>
  
  <p>Update: For people who want to check <code>InitializeComponent</code> even though I didn't modify it:</p>
  
  <p>For custom control:</p>
  
  <pre><code>namespace MyTrackbar
  {
      partial class UserControl1
      {
          /// &lt;summary&gt;
          /// Required designer variable.
          /// &lt;/summary&gt;
          private System.ComponentModel.IContainer components = null;
  
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
          protected override void Dispose(bool disposing)
          {
              if (disposing &amp;&amp; (components != null))
              {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
  
          #region Component Designer generated code
  
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify 
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          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;
      }
  }
  </code></pre>
  
  <p>For WinForm client:</p>
  
  <pre><code>namespace TestMyTrackBar
  {
      partial class Form1
      {
          /// &lt;summary&gt;
          /// Required designer variable.
          /// &lt;/summary&gt;
          private System.ComponentModel.IContainer components = null;
  
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
          protected override void Dispose(bool disposing)
          {
              if (disposing &amp;&amp; (components != null))
              {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
  
          #region Windows Form Designer generated code
  
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          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;
      }
  }
  </code></pre>