Radio Button Example for WinForms

Nevron NOV UserInterface for WinForms ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WinForms UserInterface examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Radio Button Example for WinForms Description

This example demonstrates how to create and use radio buttons and radio button groups.

A radio button (NRadioButton) is a kind of a toggle button, which is intended to be placed inside the sub-hierarchy of a radio button group (NRadioButtonGroup). When there are multiple radio buttons inside the radio button group sub-hierarchy, only one can be checked at a time. Checking a different radio button will automatically uncheck the previously checked one.

The radio button group (NRadioButtonGroup) is a content container, thus it is up to the developer to choose the style in which the radio buttons are arranged. In this example we have created two radio groups, each of which holding a stack panel (horizontal or vertical) that contain radio buttons. It is however possible to arrange the radio buttons in any way that you see fit.


Radio Button Example

C# Radio Button Example Code for Nevron UserInterface for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Editors;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NRadioButtonExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NTab tab = new NTab();

			// create a tab page with vertically arranged radio buttons
			NTabPage verticalTabPage = new NTabPage("Vertical Radio Group");
			tab.TabPages.Add(verticalTabPage);

			NRadioButtonGroup verticalRadioGroup = new NRadioButtonGroup();
			verticalRadioGroup.HorizontalPlacement = ENHorizontalPlacement.Left;
			verticalRadioGroup.VerticalPlacement = ENVerticalPlacement.Top;
			verticalTabPage.Content = verticalRadioGroup;
			
			NStackPanel verticalStack = new NStackPanel();
			verticalRadioGroup.Content = verticalStack;

			for (int i = 0; i < 5; i++)
			{
				NRadioButton radioButton = new NRadioButton("Item " + i.ToString());
				verticalStack.Add(radioButton);
			}

			NRadioButton disabledRadioButton1 = new NRadioButton("Disabled");
			disabledRadioButton1.Enabled = false;
			verticalStack.Add(disabledRadioButton1);
			verticalRadioGroup.SelectedIndexChanged += OnVerticalRadioGroupSelectedIndexChanged;

			// create a tab page with horizontally arranged radio buttons
			NTabPage horizontalTabPage = new NTabPage("Horizontal Radio Group");
			tab.TabPages.Add(horizontalTabPage);

			NRadioButtonGroup horizontalRadioGroup = new NRadioButtonGroup();
			horizontalRadioGroup.VerticalPlacement = ENVerticalPlacement.Top;
			horizontalRadioGroup.HorizontalPlacement = ENHorizontalPlacement.Left;
			horizontalTabPage.Content = horizontalRadioGroup;

			NStackPanel horizontalStack = new NStackPanel();
			horizontalStack.Direction = ENHVDirection.LeftToRight;
			horizontalRadioGroup.Content = horizontalStack;

            for (int i = 0; i < 5; i++)
			{
				NRadioButton radioButton = new NRadioButton("Item " + i.ToString());
				horizontalStack.Add(radioButton);
			}

			NRadioButton disabledRadioButton2 = new NRadioButton("Disabled");
			disabledRadioButton2.Enabled = false;
			horizontalStack.Add(disabledRadioButton2);
			horizontalRadioGroup.SelectedIndexChanged += OnHorizontalRadioGroupSelectedIndexChanged;
			return tab;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			stack.FillMode = ENStackFillMode.Last;
			stack.FitMode = ENStackFitMode.Last;

			// create the events list box
			m_EventsLog = new NExampleEventsLog();
			stack.Add(m_EventsLog);

			return stack;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to create and use radio buttons and radio button groups.

A radio button (NRadioButton) is a kind of a toggle button, which is intended to be placed inside the sub-hierarchy of a radio button group (NRadioButtonGroup). When there are multiple radio buttons inside the radio button group sub-hierarchy, only one can be checked at a time. Checking a different radio button will automatically uncheck the previously checked one.

The radio button group (NRadioButtonGroup) is a content container, thus it is up to the developer to choose the style in which the radio buttons are arranged. In this example we have created two radio groups, each of which holding a stack panel (horizontal or vertical) that contain radio buttons. It is however possible to arrange the radio buttons in any way that you see fit.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Toggle Buttons", "https://www.nevron.com/docs/openvision/ui-toggle-buttons.html"), ("Buttons Overview", "https://www.nevron.com/docs/openvision/ui-buttons-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnHorizontalRadioGroupSelectedIndexChanged(NValueChangeEventArgs args) { m_EventsLog.LogEvent("Horizontal Radio Selected: " + (int)args.NewValue); } private void OnVerticalRadioGroupSelectedIndexChanged(NValueChangeEventArgs args) { m_EventsLog.LogEvent("Vertical Radio Selected: " + (int)args.NewValue); } #endregion #region Fields NExampleEventsLog m_EventsLog; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Radio Button Example for WinForms Solution

VB.NET Radio Button Example Code for Nevron UserInterface for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NRadioButtonExample"

        ' Create the NOV example and its controls
        Dim exampleContent As NWidget = CreateExampleContent()
        Dim exampleControls As NWidget = CreateExampleControls()
        If Not exampleControls Is Nothing Then
            exampleControls.PreferredWidth = 300
        End If

        ' Place the example and its controls in a pair box
        Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
        pairBox.FillMode = ENStackFillMode.First
        pairBox.FitMode = ENStackFitMode.First
        pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

        ' Host the example in the form
        Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
        host.Dock = DockStyle.Fill
        Controls.Add(host)
    End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            Dim tab As NTab = New NTab()

            ' create a tab page with vertically arranged radio buttons
            Dim verticalTabPage As NTabPage = New NTabPage("Vertical Radio Group")
            tab.TabPages.Add(verticalTabPage)

            Dim verticalRadioGroup As NRadioButtonGroup = New NRadioButtonGroup()
            verticalRadioGroup.HorizontalPlacement = ENHorizontalPlacement.Left
            verticalRadioGroup.VerticalPlacement = ENVerticalPlacement.Top
            verticalTabPage.Content = verticalRadioGroup

            Dim verticalStack As NStackPanel = New NStackPanel()
            verticalRadioGroup.Content = verticalStack

            For i = 0 To 4
                Dim radioButton As NRadioButton = New NRadioButton("Item " & i.ToString())
                verticalStack.Add(radioButton)
            Next

            Dim disabledRadioButton1 As NRadioButton = New NRadioButton("Disabled")
            disabledRadioButton1.Enabled = False
            verticalStack.Add(disabledRadioButton1)
            verticalRadioGroup.SelectedIndexChanged += AddressOf OnVerticalRadioGroupSelectedIndexChanged

            ' create a tab page with horizontally arranged radio buttons
            Dim horizontalTabPage As NTabPage = New NTabPage("Horizontal Radio Group")
            tab.TabPages.Add(horizontalTabPage)

            Dim horizontalRadioGroup As NRadioButtonGroup = New NRadioButtonGroup()
            horizontalRadioGroup.VerticalPlacement = ENVerticalPlacement.Top
            horizontalRadioGroup.HorizontalPlacement = ENHorizontalPlacement.Left
            horizontalTabPage.Content = horizontalRadioGroup

            Dim horizontalStack As NStackPanel = New NStackPanel()
            horizontalStack.Direction = ENHVDirection.LeftToRight
            horizontalRadioGroup.Content = horizontalStack

            For i = 0 To 4
                Dim radioButton As NRadioButton = New NRadioButton("Item " & i.ToString())
                horizontalStack.Add(radioButton)
            Next

            Dim disabledRadioButton2 As NRadioButton = New NRadioButton("Disabled")
            disabledRadioButton2.Enabled = False
            horizontalStack.Add(disabledRadioButton2)
            horizontalRadioGroup.SelectedIndexChanged += AddressOf OnHorizontalRadioGroupSelectedIndexChanged
            Return tab
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()
            stack.FillMode = ENStackFillMode.Last
            stack.FitMode = ENStackFitMode.Last

            ' create the events list box
            m_EventsLog = New NExampleEventsLog()
            stack.Add(m_EventsLog)

            Return stack
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to create and use radio buttons and radio button groups.

A radio button (NRadioButton) is a kind of a toggle button, which is intended to be placed inside the sub-hierarchy of a radio button group (NRadioButtonGroup). When there are multiple radio buttons inside the radio button group sub-hierarchy, only one can be checked at a time. Checking a different radio button will automatically uncheck the previously checked one.

The radio button group (NRadioButtonGroup) is a content container, thus it is up to the developer to choose the style in which the radio buttons are arranged. In this example we have created two radio groups, each of which holding a stack panel (horizontal or vertical) that contain radio buttons. It is however possible to arrange the radio buttons in any way that you see fit.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Toggle Buttons", "https://www.nevron.com/docs/openvision/ui-toggle-buttons.html"), ("Buttons Overview", "https://www.nevron.com/docs/openvision/ui-buttons-overview.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnHorizontalRadioGroupSelectedIndexChanged(args As NValueChangeEventArgs) m_EventsLog.LogEvent("Horizontal Radio Selected: " & CInt(args.NewValue).ToString()) End Sub Private Sub OnVerticalRadioGroupSelectedIndexChanged(args As NValueChangeEventArgs) m_EventsLog.LogEvent("Vertical Radio Selected: " & CInt(args.NewValue).ToString()) End Sub #End Region #Region "Fields" Private m_EventsLog As NExampleEventsLog #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Radio Button Example for WinForms Solution

Related Documentation Links