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.
- Create Windows Application.
- Update Form name and the text, as per your understanding.
- Right click on the project and go to Add. Click Component after which, there appears a Window
- In this Window, select Component and write your component/control name. Click Add button.
- 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.
We are creating a component for RadioListButton. For it, we need to update .cs file with namespace and import the required namespaces.
- Class
RadioListButton.CS
- Namespace
By default, the project name is a namespace and namespace RadioListButton {}.
- By default, class name and inherit class public partial class RadioListButton: Component{}.
- Change the namespace. For it, namespace RadioListButton and namespace System.Windows.Forms b) Remove Component class as an inherit and add/inherit ListBox
- public partial class RadioListButton: Component => public partial class RadioListButton: ListBox. Afterwards, copy and paste all the methods and the events code for RadioListButton.
- Build your project.
- Afterwards, build your project and you can see your component inside the toolbox under your project name with the component. For example, RadioListButton Components.
- 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.
Code - frmRadioListButton.CS [Form]
- frmRadioListButton.CS [Form]
- namespace RadioListButton
- {
- public partial class frmRadioListButton : Form
- {
- public frmRadioListButton()
- {
- InitializeComponent();
-
- var myCountryList = new[] { new { Id = 1, Name = "India" }, new { Id = 2, Name = "Canada" }, new { Id = 2, Name = "USA" }, new { Id = 2, Name = "Singapore" } }.ToList();
-
- RadioListButton.DataSource = myCountryList;
- RadioListButton.DisplayMember = "Name";
- RadioListButton.ValueMember = "Id";
- }
- }
- }
RadioListButton.CS [Component]
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms.VisualStyles;
-
- namespace System.Windows.Forms
- {
- public partial class RadioListButton : ListBox
- {
- Size s;
- public RadioListButton()
- {
- this.DrawMode = DrawMode.OwnerDrawFixed;
- using (var g = Graphics.FromHwnd(IntPtr.Zero))
- s = RadioButtonRenderer.GetGlyphSize(
- Graphics.FromHwnd(IntPtr.Zero), RadioButtonState.CheckedNormal);
- }
-
- protected override void OnDrawItem(DrawItemEventArgs e)
- {
-
- var text = (Items.Count > 0) ? GetItemText(Items[e.Index]) : Name;
- Rectangle r = e.Bounds; Point p;
- var flags = TextFormatFlags.Default | TextFormatFlags.NoPrefix;
- var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
- var state = selected ?
- (Enabled ? RadioButtonState.CheckedNormal :
- RadioButtonState.CheckedDisabled) :
- (Enabled ? RadioButtonState.UncheckedNormal :
- RadioButtonState.UncheckedDisabled);
- if (RightToLeft == System.Windows.Forms.RightToLeft.Yes)
- {
- p = new Point(r.Right - r.Height + (ItemHeight - s.Width) / 2,
- r.Top + (ItemHeight - s.Height) / 2);
- r = new Rectangle(r.Left, r.Top, r.Width - r.Height, r.Height);
- flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
- }
- else
- {
- p = new Point(r.Left + (ItemHeight - s.Width) / 2,
- r.Top + (ItemHeight - s.Height) / 2);
- r = new Rectangle(r.Left + r.Height, r.Top, r.Width - r.Height, r.Height);
- }
- var bc = selected ? (Enabled ? SystemColors.Highlight :
- SystemColors.InactiveBorder) : BackColor;
- var fc = selected ? (Enabled ? SystemColors.HighlightText :
- SystemColors.GrayText) : ForeColor;
- using (var b = new SolidBrush(bc))
- e.Graphics.FillRectangle(b, e.Bounds);
- RadioButtonRenderer.DrawRadioButton(e.Graphics, p, state);
- TextRenderer.DrawText(e.Graphics, text, Font, r, fc, bc, flags);
- e.DrawFocusRectangle();
- base.OnDrawItem(e);
- }
-
-
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never),
- DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public override SelectionMode SelectionMode
- {
- get { return System.Windows.Forms.SelectionMode.One; }
- set { }
- }
-
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never),
- DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public override int ItemHeight
- {
- get { return (this.Font.Height + 2); }
- set { }
- }
-
- [EditorBrowsable(EditorBrowsableState.Never), Browsable(false),
- DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public override DrawMode DrawMode
- {
- get { return base.DrawMode; }
- set { base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; }
- }
- }
- }