如何给自定义控件加ValueChanged事件

云海玉弓缘 2009-06-16 02:08:28
从网上找了一个竖形的进度条控件, 但是这个控件没有ValueChanged事件,如何自定义这个事件啊?

附上竖形进度条控件源码:


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace VerticalProgressBar
{
public enum Styles
{
Classic, // same as ProgressBar
Solid
}

public enum BorderStyles
{
Classic, // same as ProgressBar
None
}
/// <summary>
/// Vertical Progress Bar
/// </summary>
[Description("Vertical Progress Bar")]
[ToolboxBitmap(typeof(ProgressBar))]
[Browsable(false)]
public sealed class VerticalProgressBar : System.Windows.Forms.UserControl
{

private System.ComponentModel.Container components = null;

private int m_Value = 50;
private int m_Minimum = 0;
private int m_Maximum = 100;
private int m_Step = 10;

private Styles m_Style = Styles.Classic; //Bar Style
private BorderStyles m_BorderStyle = BorderStyles.Classic;
private Color m_Color = Color.Blue; //Bar color

public VerticalProgressBar()
{
InitializeComponent();

// ***** avoid flickering
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);

this.Name = "VerticalProgressBar";
this.Size = new Size(10, 120);
}

[Description( "VerticalProgressBar Maximum Value")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public int Maximum
{
get
{
return m_Maximum;
}
set
{
m_Maximum = value;
if(m_Maximum < m_Minimum)
m_Minimum=m_Maximum;
if(m_Maximum < m_Value)
m_Value = m_Maximum;
Invalidate();
}
}
[Description( "VerticalProgressBar Minimum Value")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public int Minimum
{
get
{
return m_Minimum;
}
set
{
m_Minimum = value;
if(m_Minimum > m_Maximum)
m_Maximum = m_Minimum;
if(m_Minimum > m_Value)
m_Value = m_Minimum;
Invalidate();
}
}
[Description( "VerticalProgressBar Step")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public int Step
{
get
{
return m_Step;
}
set
{
m_Step = value;
}
}
[Description( "VerticalProgressBar Current Value")]
[Category( "VerticalProgressBar" )]
public int Value
{
get
{
return m_Value;
}
set
{
m_Value = value;
if(m_Value > m_Maximum)
m_Value = m_Maximum;
if(m_Value < m_Minimum)
m_Value = m_Minimum;
Invalidate();
}
}
[Description( "VerticalProgressBar Color")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public System.Drawing.Color Color
{
get
{
return m_Color;
}
set
{
m_Color = value;
Invalidate();
}
}
[Description( "VerticalProgressBar Border Style")]
[Category( "VerticalProgressBar" )]
public new BorderStyles BorderStyle
{
get
{
return m_BorderStyle;
}
set
{
m_BorderStyle = value;
Invalidate();
}
}
[Description( "VerticalProgressBar Style")]
[Category( "VerticalProgressBar" )]
public Styles Style
{
get
{
return m_Style;
}
set
{
m_Style = value;
Invalidate();
}
}

public void PerformStep()
{
m_Value+=m_Step;

if(m_Value > m_Maximum)
m_Value = m_Maximum;
if(m_Value < m_Minimum)
m_Value = m_Minimum;

Invalidate();
return;
}

public void Increment(int value)
{
m_Value+=value;

if(m_Value > m_Maximum)
m_Value = m_Maximum;
if(m_Value < m_Minimum)
m_Value = m_Minimum;

Invalidate();
return;
}


private void drawBorder(Graphics dc)
{
if(m_BorderStyle == BorderStyles.Classic)
{
Color darkColor = ControlPaint.Dark(this.BackColor);
Color brightColor = ControlPaint.Dark(this.BackColor);
Pen p = new Pen(darkColor, 1);
dc.DrawLine(p, this.Width, 0, 0, 0);
dc.DrawLine(p, 0,0, 0, this.Height);
p = new Pen(brightColor,1);
dc.DrawLine(p, 0, this.Height, this.Width,this.Height);
dc.DrawLine(p, this.Width,this.Height, this.Width,0);
}
}

private void drawBar(Graphics dc)
{
if(m_Minimum == m_Maximum || (m_Value - m_Minimum) == 0)
return;

int width; // the bar width
int height; // the bar height
int x; // the bottom-left x pos of the bar
int y; // the bottom-left y pos of the bar

if(m_BorderStyle == BorderStyles.None)
{
width = this.Width;
x = 0;
y = this.Height;
}
else
{
if(this.Width > 4 || this.Height > 2)
{
width = this.Width - 4;
x = 2;
y = this.Height - 1;
}
else
return; // Cannot draw
}

height = (m_Value - m_Minimum) * this.Height / (m_Maximum - m_Minimum); // the bar height

if(m_Style == Styles.Solid)
{
drawSolidBar(dc, x, y, width, height);
}
if(m_Style == Styles.Classic)
{
drawClassicBar(dc, x, y, width, height);
}
}
private void drawSolidBar(Graphics dc, int x, int y, int width, int height)
{
dc.FillRectangle(new SolidBrush(m_Color), x, y-height, width, height);
}
private void drawClassicBar(Graphics dc, int x, int y, int width, int height)
{
int valuepos_y = y - height; // The pos y of value

int blockheight = width * 3 / 4; // The height of the block

if (blockheight <= -1) return; // make sure blockheight is larger than -1 in order not to have the infinite loop.

for(int currentpos = y; currentpos > valuepos_y; currentpos -= blockheight+1)
{
dc.FillRectangle(new SolidBrush(m_Color), x, currentpos - blockheight, width, blockheight);
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics dc = e.Graphics;

//Draw Bar
drawBar(dc);

//Draw Border
drawBorder(dc);

base.OnPaint(e);
}

protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged (e);
Invalidate();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

...全文
751 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jest850615 2009-06-16
  • 打赏
  • 举报
回复
up6楼
gomoku 2009-06-16
  • 打赏
  • 举报
回复

[Description("...")]
[Category("VerticalProgressBar")]
public event EventHandler ValueChanged; //<---

[Description("VerticalProgressBar Current Value")]
[Category("VerticalProgressBar")]
public int Value
{
get
{
return m_Value;
}
set
{
if (m_Value != value) //<---
{
m_Value = Math.Max(this.m_Minimum, Math.Min(this.m_Maximum, value));
if (ValueChanged != null)
{
ValueChanged(this, EventArgs.Empty); //<---
}
Invalidate();
}
}
}

public void PerformStep()
{
this.Value += m_Step; //<---
}

public void Increment(int value)
{
this.Value += value;
}

可能你还要稍微修改一下Maximum和Minimum属性。
rightyeah 2009-06-16
  • 打赏
  • 举报
回复
up
云海玉弓缘 2009-06-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qq2766 的回复:]
C# code

private EventHandler<ValueChangedEventArgs> _ValueChanged;
public event EventHandler<ValueChangedEventArgs> ValueChanged
{
add { _ValueChanged += value; }
remove { _ValueChanged -= value; }
}
public class ValueChangedEventArgs : EventArgs
{
private int _Value;

public int Value

[/Quote]

老兄,你的代码我编译的时候提示:
错误 1 VerticalProgressBar.VerticalProgressBar.OnValueChanged是密封类“VerticalProgressBar.VerticalProgressBar”中新的虚拟成员

搜索了下这个错误有篇文章http://topic.csdn.net/t/20040419/15/2983558.html

我去掉了virtual关键字,但是还有警告:
警告 1 VerticalProgressBar.VerticalProgressBar.ValueChangedEventArgs : 在密封类中声明了新的保护成员

不知道是怎么回事...
NealXX 2009-06-16
  • 打赏
  • 举报
回复


private EventHandler<ValueChangedEventArgs> _ValueChanged;
public event EventHandler<ValueChangedEventArgs> ValueChanged
{
add { _ValueChanged += value; }
remove { _ValueChanged -= value; }
}
public class ValueChangedEventArgs : EventArgs
{
private int _Value;

public int Value
{
get { return _Value; }
set { _Value = value; }
}

public ValueChangedEventArgs(int value)
{
this._Value = value;
}
}
protected virtual void OnValueChanged(ValueChangedEventArgs e)
{
if (_ValueChanged != null)
{
_ValueChanged(this, e);
}
}

cpio 2009-06-16
  • 打赏
  • 举报
回复
在类里面声明事件:
public event EventHandler ValueChanged;


引发事件
public void PerformStep()
{
m_Value+=m_Step;
if (ValueChanged != null)
{
ValueChanged(this, new EventArgs());
}
li_dao_hang_1989 2009-06-16
  • 打赏
  • 举报
回复
呵呵……
来接分的!

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧