Click here to Skip to main content
15,882,017 members
Articles / Programming Languages / C#
Article

Adding an LED to your radiobuttons

Rate me:
Please Sign up or sign in to vote.
3.90/5 (6 votes)
2 Jul 20023 min read 144.8K   1.2K   62   16
Enhance the visual impact of your radio buttons

Introduction

After writing a software front end for my Kenwood R-5000 shortwave receiver, I was unhappy with the look of the white circle/black dot standard radio buttons. After all, my Kenwood has these very cool LEDs in the top corner of each button, to show which is currently active. How to simulate this in C# to enhance my project? After tinkering with Alexandr Khilov's very nice cute button, I hit on it. Subclass it as a RadioButton instead of a Button,draw a triangle in the top left corner, set it to a color, then add a little code to tie the whole thing together! I find the end results to be very attractive visually, and hopefully you'll find uses for it I've never considered. Here's a snapshot of the demo project to show you what's possible. Not necessarily tasteful you understand, but possible.

More about the control

I've added a couple of additional touches that are absent from Alex's project, the main one being an attribute header for each property, so a little help is available in design mode:

C#
[ 
Bindable(true),
Category("Appearance"),
Description("Sets the width of the LED in % of control's width")
]
public int LEDWidth{
    get {return m_width;}
    set {m_width = value; Invalidate();
}

And of course, several new elements had to be added, here's a quick rundown:

  • LEDColor: The color of the LED in OFF Mode. My favorites being DarkRed, Green and DarkGoldenRod (for a yellow LED).

  • LEDWidth: an integer that represents the percentage of the control's width you want the LED to cover.

  • LEDHeight: an integer that represents the percentage of the control's height you want the LED to cover. You can make some VERY strange looking LED's by playing with these numbers. I find, about a 10% width and 40% height looks good for a rectangular control.

  • LEDOffset: The number of pixels between the edge of the control and the LED.

I also changed a couple of Alex's identifiers, since I prefer the button to gradient from top to bottom instead of left to right:

  • topColor: sets the color for the top of the control, that will eventually gradient into the color set by the property bottomColor. Transparency values are similarly named.

I added a boolean variable onOff in order to pass the button's state to the onPaint event handler, and added some extra code to Alex's already excellent work:

C#
if(onOff == true){
    c3 = System.Windows.Forms.ControlPaint.LightLight(m_ledcolor);
}
else{
    c3 = System.Windows.Forms.ControlPaint.Dark(m_ledcolor);
}
SolidBrush sb = new SolidBrush(c3);
Pen blackPen = new Pen(Color.Black , 2);

//set the drawing points for the triangle
Point point1 = new Point( m_offset, m_offset);
Point point2 = new Point(m_offset, triHeight());
Point point3 = new Point(triWidth(),m_offset);
Point point4 = new Point(m_offset,m_offset);

//put them all in an array of Points
Point[] triPoints = {
point1,
point2,
point3,
point4,
};
pe.Graphics.DrawPolygon(blackPen, triPoints);

//thanks to Anonymous for this suggestion!
pe.Graphics.SmoothingMode=System.Drawing.Drawing2D.
    SmoothingMode.HighQuality;
    
pe.Graphics.FillPolygon(sb,triPoints,
    System.Drawing.Drawing2D.FillMode.Winding);

Of particular interest would probably be the ControlPaint.LightLight and ControlPaint.Dark methods. No need to mess with bit masking or luminosity values to make the color changes, these functions do all the dirty work! The triHeight() and triWidth() functions, simply calculate the number of pixels needed for the correct size of the triangle.

C#
virtual protected int triHeight(){
    float x;
    x = this.Height * ((float)m_height/100);
    return (int)x;
}
virtual protected int triWidth(){
    float x;
    x = this.Width * ((float)m_width/100);
    return (int)x;
}

Thanks to DeepStack for pointing out how to force the Button's Appearance property to Button instead of Default.

Possible enhancements would include:

  • Allowing the designer to select which corner to place the LED. I'm happy with the upper left-hand corner, so I left that as a future project.

  • Changing the shape of the LED to a circle or a square or a diamond or whatever. Again, I was going for a triangular shape to simulate a real-life radio button, so I left it as is.

About Duane Lunday

I'm a very experienced programmer who recently signed a long term non-compensation agreement with my previous employer of 10 years. I've been using my downtime to catch up on C# and the .NET framework in general. If you're interested in shortwave radio software and spy stations in particular, download my SpyCatcher software written in C# at http://www.codedragon.net.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAppearance Pin
SLareau27-Apr-09 11:20
SLareau27-Apr-09 11:20 
GeneralAppearence is read-only Pin
Corey W28-Feb-07 12:12
Corey W28-Feb-07 12:12 
GeneralWeb Version Pin
NestorLeone3-Dec-03 5:17
NestorLeone3-Dec-03 5:17 
GeneralRe: Web Version Pin
vbcv17-Jun-04 19:14
vbcv17-Jun-04 19:14 
QuestionError in source code? Pin
Lasse Johansen31-Jan-03 14:09
Lasse Johansen31-Jan-03 14:09 
AnswerRe: Error in source code? Pin
vbcv17-Jun-04 19:03
vbcv17-Jun-04 19:03 
AnswerRe: Error in source code? Pin
romias9-Feb-05 2:43
romias9-Feb-05 2:43 
GeneralAppearance property Pin
James T. Johnson21-Jun-02 14:27
James T. Johnson21-Jun-02 14:27 
GeneralRe: Appearance property Pin
Duane Lunday22-Jun-02 18:29
Duane Lunday22-Jun-02 18:29 
GeneralRe: Appearance property Pin
DeepStack26-Jun-02 0:55
DeepStack26-Jun-02 0:55 
GeneralRe: Appearance property Pin
Duane Lunday3-Jul-02 5:29
Duane Lunday3-Jul-02 5:29 
GeneralAntialiasing :) Pin
20-Jun-02 21:44
suss20-Jun-02 21:44 
GeneralRe: Antialiasing :) Pin
Duane Lunday21-Jun-02 6:00
Duane Lunday21-Jun-02 6:00 
QuestionWhat is a 'long term non-compensation agreement' ? Pin
Christian Graus18-Jun-02 16:33
protectorChristian Graus18-Jun-02 16:33 
AnswerRe: What is a 'long term non-compensation agreement' ? Pin
Duane Lunday18-Jun-02 16:43
Duane Lunday18-Jun-02 16:43 
GeneralRe: What is a 'long term non-compensation agreement' ? Pin
Terence Wallace7-Jul-06 12:26
Terence Wallace7-Jul-06 12:26 
Now thats funny! Laugh | :laugh:

TL Wallace
http://www.arkitechebc.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.