dotnet / dotnet/winforms

Multiple RadioButton in GroupBox do not DataBind to checked correctly

Open
#5,072 3 comments 1 reaction 0 assignees View on GitHub
:beetle: bug help wanted
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

**.NET Core Version:**

SDK: 5.0.203 (383637d63f)
Runtime: 5.0.6 (478b2f8c0e)

**Problem description:**

When a property is bound to the checked property of a RadioButton and that RadioButton is in a group with other RadioButtons its checked property does not propagate back to the bound property when another RadioButton in the group is checked.

**Minimal repro:**
- Create new .NET-Core 5.0 WinForms app
- Open the forms designer for Form1
- Add GroupBox
- Add 3 RadioButtons to the GroupBox
- Add a Button
- Replace Form1.cs with the following:
```cs
using System;
using System.Windows.Forms;

namespace RadioButtonDataBindingTest
{
public partial class Form1 : Form
{
private readonly RadioSelectionGroup radioSelectionGroup = new RadioSelectionGroup()
{
RadioButton1Selected = false,
RadioButton2Selected = false,
RadioButton3Selected = true
};

public Form1()
{
this.InitializeComponent();

this.radioButton1.DataBindings.Add(nameof(this.radioButton1.Checked), radioSelectionGroup, nameof(radioSelectionGroup.RadioButton1Selected));
this.radioButton2.DataBindings.Add(nameof(this.radioButton2.Checked), radioSelectionGroup, nameof(radioSelectionGroup.RadioButton2Selected));
this.radioButton3.DataBindings.Add(nameof(this.radioButton3.Checked), radioSelectionGroup, nameof(radioSelectionGroup.RadioButton3Selected));
}

private void button1_Click(object sender, EventArgs e)
{
string message1 = $"radioButton1.Checked = {this.radioButton1.Checked} <=> RadioButton1Selected = {this.radioSelectionGroup.RadioButton1Selected}";
string message2 = $"radioButton2.Checked = {this.radioButton2.Checked} <=> RadioButton2Selected = {this.radioSelectionGroup.RadioButton2Selected}";
string message3 = $"radioButton3.Checked = {this.radioButton3.Checked} <=> RadioButton3Selected = {this.radioSelectionGroup.RadioButton3Selected}";

MessageBox.Show(message1 + Environment.NewLine + message2 + Environment.NewLine + message3);
}

private class RadioSelectionGroup
{
public bool RadioButton1Selected { get; set; }
public bool RadioButton2Selected { get; set; }
public bool RadioButton3Selected { get; set; }
}
}
}
```

The sample starts with RadioButton#3 as selected, now when e.g. RadioButton#2 gets selected, one would expect that RadioButton2Selected of the RadioSelectionGroup becomes true AND RadioButton3Selected becomes false.

The screenshot below shows that actually RadioButton3Selected stays true.

![image](https://user-images.githubusercontent.com/80055480/121498288-91bcf500-c9dc-11eb-8fc2-c4e715c4f359.png)

The complete sample is attached here:
[RadioButtonDataBindingTest.zip](https://github.com/dotnet/winforms/files/6629967/RadioButtonDataBindingTest.zip)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.