├── LICENSE ├── README.md └── RJToggleButton.cs /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Toggle-Button-WinForm 2 | Windows Form C# 3 |

VIDEO:

4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /RJToggleButton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using System.Windows.Forms; 8 | using System.Drawing; 9 | using System.Drawing.Drawing2D; 10 | using System.ComponentModel; 11 | 12 | namespace CustomControls.RJControls 13 | { 14 | public class RJToggleButton : CheckBox 15 | { 16 | //Fields 17 | private Color onBackColor = Color.MediumSlateBlue; 18 | private Color onToggleColor = Color.WhiteSmoke; 19 | private Color offBackColor = Color.Gray; 20 | private Color offToggleColor = Color.Gainsboro; 21 | private bool solidStyle = true; 22 | 23 | //Properties 24 | [Category("RJ Code Advance")] 25 | public Color OnBackColor 26 | { 27 | get 28 | { 29 | return onBackColor; 30 | } 31 | 32 | set 33 | { 34 | onBackColor = value; 35 | this.Invalidate(); 36 | } 37 | } 38 | 39 | [Category("RJ Code Advance")] 40 | public Color OnToggleColor 41 | { 42 | get 43 | { 44 | return onToggleColor; 45 | } 46 | 47 | set 48 | { 49 | onToggleColor = value; 50 | this.Invalidate(); 51 | } 52 | } 53 | 54 | [Category("RJ Code Advance")] 55 | public Color OffBackColor 56 | { 57 | get 58 | { 59 | return offBackColor; 60 | } 61 | 62 | set 63 | { 64 | offBackColor = value; 65 | this.Invalidate(); 66 | } 67 | } 68 | 69 | [Category("RJ Code Advance")] 70 | public Color OffToggleColor 71 | { 72 | get 73 | { 74 | return offToggleColor; 75 | } 76 | 77 | set 78 | { 79 | offToggleColor = value; 80 | this.Invalidate(); 81 | } 82 | } 83 | 84 | [Browsable(false)] 85 | public override string Text 86 | { 87 | get 88 | { 89 | return base.Text; 90 | } 91 | 92 | set 93 | { 94 | 95 | } 96 | } 97 | 98 | [Category("RJ Code Advance")] 99 | [DefaultValue(true)] 100 | public bool SolidStyle 101 | { 102 | get 103 | { 104 | return solidStyle; 105 | } 106 | 107 | set 108 | { 109 | solidStyle = value; 110 | this.Invalidate(); 111 | } 112 | } 113 | 114 | //Constructor 115 | public RJToggleButton() 116 | { 117 | this.MinimumSize = new Size(45, 22); 118 | } 119 | 120 | //Methods 121 | private GraphicsPath GetFigurePath() 122 | { 123 | int arcSize = this.Height - 1; 124 | Rectangle leftArc = new Rectangle(0, 0, arcSize, arcSize); 125 | Rectangle rightArc = new Rectangle(this.Width - arcSize - 2, 0, arcSize, arcSize); 126 | 127 | GraphicsPath path = new GraphicsPath(); 128 | path.StartFigure(); 129 | path.AddArc(leftArc, 90, 180); 130 | path.AddArc(rightArc, 270, 180); 131 | path.CloseFigure(); 132 | 133 | return path; 134 | } 135 | 136 | protected override void OnPaint(PaintEventArgs pevent) 137 | { 138 | int toggleSize = this.Height - 5; 139 | pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 140 | pevent.Graphics.Clear(this.Parent.BackColor); 141 | 142 | if (this.Checked) //ON 143 | { 144 | //Draw the control surface 145 | if (solidStyle) 146 | pevent.Graphics.FillPath(new SolidBrush(onBackColor), GetFigurePath()); 147 | else pevent.Graphics.DrawPath(new Pen(onBackColor,2), GetFigurePath()); 148 | //Draw the toggle 149 | pevent.Graphics.FillEllipse(new SolidBrush(onToggleColor), 150 | new Rectangle(this.Width - this.Height + 1, 2, toggleSize, toggleSize)); 151 | } 152 | else //OFF 153 | { 154 | //Draw the control surface 155 | if(solidStyle) 156 | pevent.Graphics.FillPath(new SolidBrush(offBackColor), GetFigurePath()); 157 | else pevent.Graphics.DrawPath(new Pen(offBackColor, 2), GetFigurePath()); 158 | //Draw the toggle 159 | pevent.Graphics.FillEllipse(new SolidBrush(offToggleColor), 160 | new Rectangle(2, 2, toggleSize, toggleSize)); 161 | } 162 | } 163 | } 164 | } 165 | --------------------------------------------------------------------------------