如何给自定义控件加ValueChanged事件
从网上找了一个竖形的进度条控件, 但是这个控件没有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
}
}
...全文
相关推荐
swift-加减号控件Swift
总结来说,Swift中的加减号控件是通过自定义UIView实现的,包含加号、减号按钮和一个显示数值的标签。通过监听按钮点击事件,我们可以更新数值并确保其在预设范围内。
WPF调色器&屏幕取色器
在WPF中,我们可以使用Slider控件来控制RGB或HSV的值,通过事件处理程序更新颜色显示。例如,可以为每个颜色通道创建一个Slider,然后在ValueChanged事件中更新颜色值。
qt滑动选择年份SliderTime.7z
Qt的样式表(QSS)系统允许开发者自定义控件的外观,以适应不同的界面设计。
C#自定义控件加滑动条的简单实现例子
```为了使滑动条的改变能够影响到自定义控件的行为,我们需要监听滑动条的`ValueChanged`事件。
WPF自定义的NumericUpDown控件
**事件处理**:控件提供了各种事件,如ValueChanged事件,当数值改变时触发,开发者可以在此事件中执行相应的逻辑。现在,让我们关注这个自定义的控件:1.


