我想創建一個組合自定義控件,其中包含將由某些客戶端表單使用的跟蹤欄和其他控件。我需要公開一些事件,如MouseDown
和MouseUp
以檢測拖動的開始和拖動的結束。奇怪的是,MouseDown
是好的,但不是MouseUp
。下面的代碼演示了它。這是.NET Framework或trackbar中的錯誤嗎?如果不是,我應該怎麼做呢?爲什麼我的自定義用戶控件不會引發鼠標事件?
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;
}
}
你實際上是在使用'TrackBar'控件,還是建立了自己的自定義滑塊? – 2010-12-10 16:58:57
你說得對,我只是意識到,當我將軌跡欄嵌入到自定義控件中時,它不起作用。 – user310291 2010-12-11 10:57:56
@ user310291如果它不起作用,它不能成爲框架錯誤。如果是這樣,.NET框架應該比它的SLOC計數有更多的錯誤! – Luca 2010-12-11 18:08:40