InitializeComponent();
}
}
}
Application2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public enum RGBSelection
{
R, G, B
}
public partial class UserControl1 : UserControl
{
public event EventHandler<EventArgs> ValueChanged;
private System.Windows.Forms.Label labelLabel;
private System.Windows.Forms.NumericUpDown numericUpDownValue;
private System.Windows.Forms.TrackBar trackBarValue;
public UserControl1()
{
InitializeComponent();
numericUpDownValue.Minimum = 0;
numericUpDownValue.Maximum = 255;
trackBarValue.Minimum = 0;
trackBarValue.Maximum = 255;
trackBarValue.TickFrequency = 10;
numericUpDownValue.ValueChanged += numericUpDownValue_ValueChanged;
trackBarValue.ValueChanged += trackBarValue_ValueChanged;
}
public RGBSelection ColorPart { get; set; }
public string Label
{
get { return labelLabel.Text; }
set { labelLabel.Text = value; }
}
// Значение элемента управления
public int Value
{
get { return (int)numericUpDownValue.Value; }
set { numericUpDownValue.Value = (int)value; }
}
// Если либо у ползунка, либо у числового поля изменилось значение, мы
должны
// обновить другой элемент, чтобы сохранить их взаимное соответствие
void trackBarValue_ValueChanged(object sender, EventArgs e)
{
if (sender != this)
{
numericUpDownValue.Value = trackBarValue.Value;
OnValueChanged();
}
}
void numericUpDownValue_ValueChanged(object sender, EventArgs e)
{
if (sender != this)
{
trackBarValue.Value = (int)numericUpDownValue.Value;
OnValueChanged();
}
}
protected void OnValueChanged()
{
Refresh();
if (ValueChanged != null) { ValueChanged(this, EventArgs.Empty); }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Эллипс нужного цвета — единственное, что мы должны
// нарисовать сами в этом элементе управления
Rectangle rect = new Rectangle(numericUpDownValue.Left, 5,
numericUpDownValue.Width, numericUpDownValue.Bounds.Top - 10);
int r = 0, g = 0, b = 0;
switch (ColorPart)
{
case RGBSelection.R: r = Value; break;
case RGBSelection.G: g = Value; break;
case RGBSelection.B: b = Value; break;
}
Color с = Color.FromArgb(r, g, b);
using (Brush brush = new SolidBrush(с))
{e.Graphics.FillEllipse(brush, rect); }
}
}
}