将最小最大值添加到自定义控件范围栏
本文关键字:自定义控件 范围 添加 最大值 | 更新日期: 2023-09-27 18:10:22
我用c#编写了一个自定义范围滑块。到目前为止,我有3个属性"最小,最大和值"。根据建议,我需要做以下工作:1. 调整Value属性以检查它是否保持在最小值和最大值之间。2.更改百分比变量的名称,因为它不再存储百分比。3.在任何地方使用浮点数或整型值,不要混合使用它们,因为可能会失去精度。4.创建一个方法,通过鼠标更新该值,并从事件处理程序调用该方法。
我不确定实现这些变化的最佳方式。我希望得到一些帮助。
namespace jmRangeSlider
{
public partial class rangeSlider : UserControl
{
public rangeSlider()
{
InitializeComponent();
label1.ForeColor = Color.Black;
this.ForeColor = SystemColors.Highlight; // set the default color the RangeSlider
}
protected float percent = 0.0f; // Protected because we don't want this to be accessed from the outside
// Create a Value property for the RangeSlider
public float Value
{
get
{
return percent;
}
set
{
// Maintain the value between 0 and 100
if (value < 0) value = 0;
else if (value > 100) value = 100;
percent = value;
label1.Text = value.ToString();
//redraw the RangeSlider every time the value changes
this.Invalidate();
}
}
int minValue = 0;
public int MinValue
{
get
{
return this.minValue;
}
set
{
this.minValue = value;
}
}
int maxValue = 100;
public int MaxValue
{
get
{
return this.maxValue;
}
set
{
this.maxValue = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Brush b = new SolidBrush(this.ForeColor); //create brush that will draw the background of the range bar
// create a linear gradient that will be drawn over the background. FromArgb means you can use the Alpha value which is the transparency
LinearGradientBrush lb = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), Color.FromArgb(50, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Vertical);
// calculate how much has the RangeSlider to be filled for 'x' %
int width = (int)((percent / 100) * this.Width);
e.Graphics.FillRectangle(b, 0, 0, width, this.Height);
e.Graphics.FillRectangle(lb, 0, 0, width, this.Height);
b.Dispose(); lb.Dispose();
}
private void rangeSlider_SizeChanged(object sender, EventArgs e)
{
// maintain the label in the center of the RangeSlider
label1.Location = new Point(this.Width / 2 - 21 / 2 - 4, this.Height / 2 - 15 / 2);
}
protected override void OnMouseClick(MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
int val = (x * 100) / this.Width; //when click get value within progress bar
int screenX = Cursor.Position.X;
label1.Text = screenX.ToString();
this.Value = val;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//this.Value += 1;
int x = e.X;
int val = (x * 100) / this.Width; //when click get value within progress bar
label1.Text = val.ToString();
this.Value = val;
}
}
}
}
添加&设置最小和最大。您可以添加检查以确保Min
public float Min { get; set; }
public float Max { get; set; }
然后,调整Value属性以检查其保持在最小和最大之间。然后像这样改变绘制方法:
int width = (int)((Value - Min) * this.Width / (Max - Min));
将设置代码改为:
Value = Min + (e.X / this.Width * (Max - Min));
进一步建议:- 更改
percent
变量的名称,因为它不再存储百分比,例如称其为value
。 - 使用浮点数或整型值,不要混合使用,因为这样会失去精度。
- 创建一个方法来通过鼠标更新该值,并从事件处理程序中调用该方法,如下所示。
protected override void OnMouseClick(MouseEventArgs e)
{
UpdateValue(e)
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) UpdateValue(e);
}
private void UpdateValue(MouseEventArgs e) {
Value = Min + (e.X / this.Width * (Max - Min));
}