Custom Radio Button - Custom Controls WinForm C # - RJ Code Advance
Custom Radio Button - Custom Controls WinForm C # - RJ Code Advance
Hello, this time we will make a custom Radio Button. Like other Windows Form controls, this
control does not have many appearance customization options. Therefore, we will add some
appearance properties so that we can change the style color of the existing Windows Form
Radio Button.
https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 1/8
7/22/2021 Custom Radio Button - Custom controls WinForm C # - RJ Code Advance
To make any custom control, it is necessary to import the Windows Forms library and the
drawing library ( Drawing ).
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
// Fields
private Color checkedColor = Color. MediumSlateBlue ;
private Color unCheckedColor = Color. Gray ;
// Properties
public Color CheckedColor
{
get { return checkedColor; }
set
{
checkedColor = value ;
this . Invalidate () ;
}
}
unCheckedColor = value ;
this . Invalidate () ;
}
}
6.- Builder
In the constructor, we simply set the minimum size of the control, the minimum height
must be 21. This is very important to avoid problems with the size of the Radio Button
figure and the font size.
//Builder
public RJRadioButton ()
{
this . MinimumSize = new Size ( 0 , 21 ) ;
// Add a padding of 10 to the left to have a considerable distance be
this . Padding = new Padding ( 10 , 0 , 0 , 0 ) ;
}
// Overridden methods
protected override void OnPaint ( PaintEventArgs pevent )
{
// Fields
Graphics graphics = pevent. Graphics ;
graphics. SmoothingMode = SmoothingMode. AntiAlias ;
float rbBorderSize = 18F;
float rbCheckSize = 12F;
RectangleF rectRbBorder = new RectangleF ()
{
X = 0.5 F,
Y = ( this . Height - rbBorderSize ) / 2 , // Center
Width = rbBorderSize,
Height = rbBorderSize
} ;
RectangleF rectRbCheck = new RectangleF ()
{
X = rectRbBorder. X + (( rectRbBorder. Width - rbCheckSize ) / 2
Y = ( this . Height - rbCheckSize ) / 2 , // Center
Width = rbCheckSize,
Height = rbCheckSize
} ;
https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 3/8
7/22/2021 Custom Radio Button - Custom controls WinForm C # - RJ Code Advance
// Drawing
using ( Pen penBorder = new Pen ( checkedColor, 1.6 F ))
using ( SolidBrush brushRbCheck = new SolidBrush ( checkedColor ))
using ( SolidBrush brushText = new SolidBrush ( this . ForeColor ))
{
// Draw surface
graphics. Clear ( this . BackColor ) ;
// Draw Radio Button
if ( this . Checked )
{
graphics. DrawEllipse ( penBorder, rectRbBorder ) ; // Circle
graphics. FillEllipse ( brushRbCheck, rectRbCheck ) ; // Circ
}
else
{
penBorder. Color = unCheckedColor;
graphics. DrawEllipse ( penBorder, rectRbBorder ) ; // Circle
}
// Draw text
graphics. DrawString ( this . Text , this . Font , brushText,
rbBorderSize + 8 , ( this . Height - TextRenderer. MeasureTex
}
}
// X-> Obsolete code, this was replaced by the Padding property in th
//(this.Padding = new Padding (10,0,0,0);)
// protected override void OnResize (EventArgs e)
// {
// base.OnResize (e);
// this.Width = TextRenderer.MeasureText (this.Text, this.Font) .Widt
//}
Well that's it, you can now use the new custom RadioButton from the toolbox, do n't forget
to compile the project to save the changes .
https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 4/8
7/22/2021 Custom Radio Button - Custom controls WinForm C # - RJ Code Advance
Leave a reply
Your email address will not be published. Required fields are marked with *
Commentary
Name *
E-mail *
Web
https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 5/8
7/22/2021 Custom Radio Button - Custom controls WinForm C # - RJ Code Advance
Welcome to blog
Look for …
Follow me
Category:
.NET
ASP .NET
C#
Mistakes
F#
Visual basic
Windows Forms
Layered Architecture
Database
MySQL
SQL Server
Custom controls
courses
.NET (Course)
Create .Net Installer
Full Login C #, VB, SQL Server
https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 6/8
7/22/2021 Custom Radio Button - Custom controls WinForm C # - RJ Code Advance
Recent logins
Circular Picture Box - Border Gradient color + Styles - C # & WinForms
Custom ProgressBar - WinForms & C #
Custom TextBox Full - Rounded & Placeholder - WinForm, C #
Custom ComboBox - Icon, Back, Text & Border Color - WinForm C #
Custom Text Box - Custom controls WinForm C #
Recent comments
gustavo on Custom Button - Custom controls WinForm C #
_Nooker in Modern Form + Font Awesome Icons, WinForm, C # - VB.NET
Impekly in Full Login + CRUD - C #, SQL Server- Advanced Level
Willgt27 in Login Cap 2- Start Session, Close Session and Show user data with C #, VB.Net,
Sql Server, WinForm- POO, Layered Architecture- Intermediate Level
https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 7/8
7/22/2021 Custom Radio Button - Custom controls WinForm C # - RJ Code Advance
Juan Vega in Chapter 4 / Create Installation Package - Application with Local Network
Database (LAN-MAN) - SQL Server, Visual Studio, WinForm, Layers
https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 8/8