Back to Devexpress

RadioGroup() Constructor

windowsforms-devexpress-dot-xtraeditors-dot-radiogroup-dot-ctor-0f4080ff.md

latest5.3 KB
Original Source

RadioGroup() Constructor

Initializes a new RadioGroup control instance with default settings.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public RadioGroup()
vb
Public Sub New

Remarks

Use this constructor to create a new instance of the RadioGroup control at runtime. The constructor initializes the new radio group with default settings.

You can use the methods and properties of the new radio group to adjust it as required: you can specify its name, caption text, radio group style, look and feel, etc. The new editor needs to be added to the parent container’s Controls collection in order for it to be displayed on the form.

When a RadioGroup control is created, the constructor is automatically invoked to create and initiate a RepositoryItemRadioGroup object, exposed via the control’s RadioGroup.Properties property.

Example

This example demonstrates how to programmatically create a radio group, add two items to the collection, initialize their properties and assign a handler to the RadioGroup.SelectedIndexChanged event. The created radio group will be used to control the visibility of a standard button control. This example assumes that a button has already been placed on the form.

The image below shows the example application.

csharp
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;

// ...
public Form1() {
   InitializeComponent();
   CreateRadioEditors();
}

private void CreateRadioEditors() {
    // Creates and initializes radio group items
    RadioGroupItem item1 = new RadioGroupItem();
    item1.Description = "Hide Button";
    RadioGroupItem item2 = new RadioGroupItem();
    item2.Description = "Show Button";
    // Creates and initializes the radio group
    RadioGroup radioEdit1 = new RadioGroup();
    radioEdit1.Properties.Items.Add(item1);
    radioEdit1.Properties.Items.Add(item2);
    radioEdit1.Name = "radioEdit1";
    radioEdit1.Location = new System.Drawing.Point(button1.Location.X, button1.Location.Y + 40);
    radioEdit1.Width = button1.Width;
    radioEdit1.Height = 100;
    // Sets the editor's selection based on the button's visibility
    if (button1.Visible) radioEdit1.SelectedIndex = 1;
    // Assigns a handler for the SelectedIndexChanged event
    radioEdit1.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
    this.Controls.Add((Control)radioEdit1);
}

private void SelectedIndexChanged(object sender, System.EventArgs e) {
    RadioGroup edit = sender as RadioGroup;
    if (edit.SelectedIndex == 0) button1.Visible = false;
    else button1.Visible = true;
}
vb
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraEditors
Imports DevExpress.Utils

Public Sub New()
   InitializeComponent()
   CreateRadioEditors()
End Sub

Private Sub CreateRadioEditors()
    ' Creates and initializes radio group items
    Dim item1 As New RadioGroupItem()
    item1.Description = "Hide Button"
    Dim item2 As New RadioGroupItem()
    item2.Description = "Show Button"
    ' Creates and initializes the radio group
    Dim radioEdit1 As New RadioGroup()
    radioEdit1.Properties.Items.Add(item1)
    radioEdit1.Properties.Items.Add(item2)
    radioEdit1.Name = "radioEdit1"
    radioEdit1.Location = New System.Drawing.Point(button1.Location.X, button1.Location.Y + 40)
    radioEdit1.Width = button1.Width
    radioEdit1.Height = 100
    ' Sets the editor's selection based on the button's visibility
    If button1.Visible Then
        radioEdit1.SelectedIndex = 1
    End If
    ' Assigns a handler for the SelectedIndexChanged event
    AddHandler radioEdit1.SelectedIndexChanged, AddressOf SelectedIndexChanged
    Me.Controls.Add(CType(radioEdit1, Control))
End Sub

Private Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim edit As RadioGroup = TryCast(sender, RadioGroup)
    If edit.SelectedIndex = 0 Then
        button1.Visible = False
    Else
        button1.Visible = True
    End If
End Sub

See Also

RepositoryItemRadioGroup

RadioGroup Class

RadioGroup Members

DevExpress.XtraEditors Namespace