How To Create And Add A Radio List Button Into Window Applications in C#

As per R&D and in namespace System.Windows.Forms, there are a number of Radio list button controls. Currently, Windows form only has RadioButton control available. Another way is to create a group and list of radio buttons, using two Windows form containers like GroupBox and Panel. All radio buttons, which we have can be added inside the group in Windows forms.

We can create our own custom radio list button, using the steps given below.

  1. Create Windows Application.

    C#

  2. Update Form name and the text, as per your understanding.

    C#

  3. Right click on the project and go to Add. Click Component after which, there appears a Window

    C#

  4. In this Window, select Component and write your component/control name. Click Add button.

    C#

  5. After creating component display, a Window of the component with the option link Toolbox appears, and view the code. For creating methods and events for our component, we need to click Switch to code view. Here, we are able to right code, as per the requirement.

    C#

    We are creating a component for RadioListButton. For it, we need to update .cs file with namespace and import the required namespaces.

    1. Class
      RadioListButton.CS

    2. Namespace
      By default, the project name is a namespace and namespace RadioListButton {}.

    3. By default, class name and inherit class public partial class RadioListButton: Component{}.

      1. Change the namespace. For it, namespace RadioListButton and namespace System.Windows.Forms b) Remove Component class as an inherit and add/inherit ListBox

      2. public partial class RadioListButton: Component => public partial class RadioListButton: ListBox. Afterwards, copy and paste all the methods and the events code for RadioListButton.

  6. Build your project.

  7. Afterwards, build your project and you can see your component inside the toolbox under your project name with the component. For example, RadioListButton Components.

    C#

    C#

  8. This component can be used in our project in any form, wherever we require it by using drag & drop. Afterwards, we can bind the data with these controls.

    C#

Code - frmRadioListButton.CS [Form] 

  1. frmRadioListButton.CS [Form]  
  2. namespace RadioListButton  
  3. {  
  4.     public partial class frmRadioListButton : Form  
  5.     {  
  6.         public frmRadioListButton()  
  7.         {  
  8.             InitializeComponent();  
  9.   
  10.             var myCountryList = new[] { new { Id = 1, Name = "India" }, new { Id = 2, Name = "Canada" }, new { Id = 2, Name = "USA" }, new { Id = 2, Name = "Singapore" } }.ToList();  
  11.   
  12.             RadioListButton.DataSource = myCountryList;  
  13.             RadioListButton.DisplayMember = "Name";  
  14.             RadioListButton.ValueMember = "Id";  
  15.         }  
  16.     }  
  17. }   

RadioListButton.CS [Component]

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Diagnostics;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms.VisualStyles;  
  10.   
  11. namespace System.Windows.Forms  
  12. {  
  13.     public partial class RadioListButton : ListBox  
  14.     {  
  15.         Size s;  
  16.         public RadioListButton()  
  17.         {  
  18.             this.DrawMode = DrawMode.OwnerDrawFixed;  
  19.             using (var g = Graphics.FromHwnd(IntPtr.Zero))  
  20.                 s = RadioButtonRenderer.GetGlyphSize(  
  21.                     Graphics.FromHwnd(IntPtr.Zero), RadioButtonState.CheckedNormal);  
  22.         }  
  23.   
  24.         protected override void OnDrawItem(DrawItemEventArgs e)  
  25.         {  
  26.   
  27.             var text = (Items.Count > 0) ? GetItemText(Items[e.Index]) : Name;  
  28.             Rectangle r = e.Bounds; Point p;  
  29.             var flags = TextFormatFlags.Default | TextFormatFlags.NoPrefix;  
  30.             var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;  
  31.             var state = selected ?  
  32.                 (Enabled ? RadioButtonState.CheckedNormal :  
  33.                            RadioButtonState.CheckedDisabled) :  
  34.                 (Enabled ? RadioButtonState.UncheckedNormal :  
  35.                            RadioButtonState.UncheckedDisabled);  
  36.             if (RightToLeft == System.Windows.Forms.RightToLeft.Yes)  
  37.             {  
  38.                 p = new Point(r.Right - r.Height + (ItemHeight - s.Width) / 2,  
  39.                     r.Top + (ItemHeight - s.Height) / 2);  
  40.                 r = new Rectangle(r.Left, r.Top, r.Width - r.Height, r.Height);  
  41.                 flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;  
  42.             }  
  43.             else  
  44.             {  
  45.                 p = new Point(r.Left + (ItemHeight - s.Width) / 2,  
  46.                 r.Top + (ItemHeight - s.Height) / 2);  
  47.                 r = new Rectangle(r.Left + r.Height, r.Top, r.Width - r.Height, r.Height);  
  48.             }  
  49.             var bc = selected ? (Enabled ? SystemColors.Highlight :  
  50.                 SystemColors.InactiveBorder) : BackColor;  
  51.             var fc = selected ? (Enabled ? SystemColors.HighlightText :  
  52.                 SystemColors.GrayText) : ForeColor;  
  53.             using (var b = new SolidBrush(bc))  
  54.                 e.Graphics.FillRectangle(b, e.Bounds);  
  55.             RadioButtonRenderer.DrawRadioButton(e.Graphics, p, state);  
  56.             TextRenderer.DrawText(e.Graphics, text, Font, r, fc, bc, flags);  
  57.             e.DrawFocusRectangle();  
  58.             base.OnDrawItem(e);  
  59.         }  
  60.   
  61.   
  62.         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never),  
  63.         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]  
  64.         public override SelectionMode SelectionMode  
  65.         {  
  66.             get { return System.Windows.Forms.SelectionMode.One; }  
  67.             set { }  
  68.         }  
  69.   
  70.         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never),  
  71.         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]  
  72.         public override int ItemHeight  
  73.         {  
  74.             get { return (this.Font.Height + 2); }  
  75.             set { }  
  76.         }  
  77.   
  78.         [EditorBrowsable(EditorBrowsableState.Never), Browsable(false),  
  79.         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]  
  80.         public override DrawMode DrawMode  
  81.         {  
  82.             get { return base.DrawMode; }  
  83.             set { base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; }  
  84.         }  
  85.     }  

Up Next
    Ebook Download
    View all
    Learn
    View all