Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
116 views

Custom Radio Button - Custom Controls WinForm C # - RJ Code Advance

Uploaded by

Toan Nguyen Manh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Custom Radio Button - Custom Controls WinForm C # - RJ Code Advance

Uploaded by

Toan Nguyen Manh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

7/22/2021 Custom Radio Button - Custom controls WinForm C # - RJ Code Advance

Custom Radio Button - Custom


controls WinForm C #
by 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.

It's very easy to do so, so let's start with the tutorial.

1.- Create class


First  we will add a class  for the Custom Option Button in our Windows Form project, You
can put the name you want, in my case I will name it  RJRadioButton .

2.- Import Windows.Form and Drawing library

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;

3.- Inherit the RadioButton class


As usual, we will  inherit an existing control from the Windows Form library  and thus
extend its functionality and modify its appearance, in this case, inheriting from the
RadioButton class .

public class RJRadioButton: RadioButton


{
}

4.- Declare fields


In the class created above, we will declare fields for the  appearance and assign their
default values,  for example: The color for the selected and deselected state of the
RadioButton.

// Fields
private Color checkedColor = Color. MediumSlateBlue ;
private Color unCheckedColor = Color. Gray ;

5.- Generate properties


We generate properties to expose the fields defined above.

// Properties
public Color CheckedColor
{
get { return checkedColor; }
set
{
checkedColor = value ;
this . Invalidate () ;
}
}

public Color UnCheckedColor


{
get { return unCheckedColor; }
set
{
https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 2/8
7/22/2021 Custom Radio Button - Custom controls WinForm C # - RJ Code Advance

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 ) ;
}

7.- Cancel the Painting event


You need to override the OnPaint event method to draw and paint the new appearance of the
control, and override the Resize event method to set the appropriate width for the control
each time it resizes.

// 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 . 

VIEW FULL CODE (GITHUB)

Watch video tutorial

Custom RadioButton - WinForm C#

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

Post the comment

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

Software Patterns (Course)


OOP (Course)
Desk
GUI
Software Patterns
OOP
Uncategorized
Web

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

Terms and Conditions Privacy Policy Cookies policy




© 2021 RJ Code Advance - All Rights Reserved

https://rjcodeadvance.com/custom-radio-button-custom-controls-winform-c/ 8/8

You might also like