├── LICENSE ├── README.md └── RJRadioButton.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 | # Custom-RadioButton 2 | Custom Radio Button Windows Form C# 3 |

VIDEO:

4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /RJRadioButton.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 | 11 | namespace CustomControls.RJControls 12 | { 13 | class RJRadioButton : RadioButton 14 | { 15 | //Fields 16 | private Color checkedColor = Color.MediumSlateBlue; 17 | private Color unCheckedColor = Color.Gray; 18 | 19 | //Properties 20 | public Color CheckedColor 21 | { 22 | get 23 | { 24 | return checkedColor; 25 | } 26 | 27 | set 28 | { 29 | checkedColor = value; 30 | this.Invalidate(); 31 | } 32 | } 33 | 34 | public Color UnCheckedColor 35 | { 36 | get 37 | { 38 | return unCheckedColor; 39 | } 40 | 41 | set 42 | { 43 | unCheckedColor = value; 44 | this.Invalidate(); 45 | } 46 | } 47 | 48 | //Constructor 49 | public RJRadioButton() 50 | { 51 | this.MinimumSize = new Size(0, 21); 52 | //Add a padding of 10 to the left to have a considerable distance between the text and the RadioButton. 53 | this.Padding = new Padding(10,0,0,0); 54 | } 55 | 56 | //Overridden methods 57 | protected override void OnPaint(PaintEventArgs pevent) 58 | { 59 | //Fields 60 | Graphics graphics = pevent.Graphics; 61 | graphics.SmoothingMode = SmoothingMode.AntiAlias; 62 | float rbBorderSize = 18F; 63 | float rbCheckSize = 12F; 64 | RectangleF rectRbBorder = new RectangleF() 65 | { 66 | X = 0.5F, 67 | Y = (this.Height - rbBorderSize) / 2, //Center 68 | Width = rbBorderSize, 69 | Height = rbBorderSize 70 | }; 71 | RectangleF rectRbCheck = new RectangleF() 72 | { 73 | X = rectRbBorder.X + ((rectRbBorder.Width - rbCheckSize) / 2), //Center 74 | Y = (this.Height - rbCheckSize) / 2, //Center 75 | Width = rbCheckSize, 76 | Height = rbCheckSize 77 | }; 78 | 79 | //Drawing 80 | using (Pen penBorder = new Pen(checkedColor, 1.6F)) 81 | using (SolidBrush brushRbCheck = new SolidBrush(checkedColor)) 82 | using (SolidBrush brushText = new SolidBrush(this.ForeColor)) 83 | { 84 | //Draw surface 85 | graphics.Clear(this.BackColor); 86 | //Draw Radio Button 87 | if (this.Checked) 88 | { 89 | graphics.DrawEllipse(penBorder, rectRbBorder);//Circle border 90 | graphics.FillEllipse(brushRbCheck, rectRbCheck); //Circle Radio Check 91 | } 92 | else 93 | { 94 | penBorder.Color = unCheckedColor; 95 | graphics.DrawEllipse(penBorder, rectRbBorder); //Circle border 96 | } 97 | //Draw text 98 | graphics.DrawString(this.Text, this.Font, brushText, 99 | rbBorderSize + 8, (this.Height - TextRenderer.MeasureText(this.Text, this.Font).Height) / 2);//Y=Center 100 | } 101 | } 102 | 103 | //X-> Obsolete code, this was replaced by the Padding property in the constructor 104 | //(this.Padding = new Padding(10,0,0,0);) 105 | //protected override void OnResize(EventArgs e) 106 | //{ 107 | // base.OnResize(e); 108 | // this.Width = TextRenderer.MeasureText(this.Text, this.Font).Width + 30; 109 | //} 110 | } 111 | } 112 | --------------------------------------------------------------------------------