【发布时间】:2010-12-10 16:44:08
【问题描述】:
我想创建一个带有轨迹栏和其他一些客户端表单将使用的复合自定义控件。我需要公开一些事件,如MouseDown 和MouseUp 来检测拖动开始和拖动结束。奇怪的是,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;
}
}
【问题讨论】:
-
您实际上是在使用
TrackBar控件,还是构建了自己的自定义滑块? -
你说得对,我只是意识到当我将跟踪栏嵌入到自定义控件中时它不起作用。
-
@user310291 它不可能是一个框架错误,如果它不能像你期望的那样工作。如果是这样,.NET 框架的错误将超过其 SLOC 数量!
标签: c# .net winforms user-controls custom-controls