using System.Drawing; using System.Windows.Forms; using System.ComponentModel; using System; namespace Switch_Toolbox.Library.Forms { /// /// CheckBox created with code & design skills. /// public class STCheckBox : CheckBox { /// /// The color of the CheckBox background rectangle /// [Category("Options"), Description("The color of the background."), Browsable(true)] private Color BoxColor { get; set; } /// /// Ticked icon /// private Image TickTock => Properties.Resources.CheckMark; public STCheckBox() { this.BoxColor = FormThemes.BaseTheme.CheckBoxBackColor; this.Paint += OnPaint; this.CheckedChanged += new EventHandler(CheckedChangedEvent); } bool value { get; set; } /// /// Binds a property from the given object to the checkbox /// /// /// /// public void Bind(object Object, string PropertyName, bool ResetBindings = true) { if (ResetBindings) DataBindings.Clear(); DataBindings.Add("Checked", Object, PropertyName); } /// /// Draws the ticked icon if the PsCheckBox is checked and its background color /// /// /// private void OnPaint(object sender, PaintEventArgs e) { e.Graphics.FillRectangle(new SolidBrush(this.BoxColor), new Rectangle(0, 0, 15, 15)); if (this.Checked) { e.Graphics.DrawImage(this.TickTock, 2, 2, 12, 12); } } /// /// Sets the size of the layout to 16x;16x /// /// private void Set16(PictureBox pb) { pb.Size = new Size(16, 16); } /// /// Sets the size of the layout to 18x;18x /// /// private void Set18(PictureBox pb) { pb.Size = new Size(18, 18); } /// /// Changes thee way our layout behaves /// private void CheckedChangedEvent(object sender, EventArgs args) { value = Checked; if (Checked) { BoxColor = FormThemes.BaseTheme.CheckBoxEnabledBackColor; } else { BoxColor = FormThemes.BaseTheme.CheckBoxBackColor; } this.Invalidate(); foreach (Binding data in DataBindings) { data.WriteValue(); } } } }