Skip to content

Commit

Permalink
Merge pull request #2024 from cwensley/curtis/radiobuttonlist-selecte…
Browse files Browse the repository at this point in the history
…dindex-unselect

Allow RadioButtonList.SelectedIndex to be set to -1
  • Loading branch information
cwensley committed Aug 30, 2021
2 parents a85aaf4 + fbe9234 commit 0b47ca7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Eto/Forms/Controls/RadioButtonList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public int SelectedIndex
set
{
EnsureButtons();
SetSelected(buttons[value]);
SetSelected(value >= 0 && value < buttons.Count ? buttons[value] : null);
}
}

Expand Down
15 changes: 12 additions & 3 deletions test/Eto.Test/Sections/Controls/RadioButtonListSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ Control Default()

var layout = new DynamicLayout { DefaultSpacing = new Size(5, 5) };
layout.Add(TableLayout.AutoSized(control));
layout.BeginVertical();
layout.AddRow(null, AddRowsButton(control), RemoveRowsButton(control), ClearButton(control), OrientationDropDown(control), TextColorControl(control), null);
layout.EndVertical();
layout.AddSeparateRow(null, AddRowsButton(control), RemoveRowsButton(control), ClearButton(control), OrientationDropDown(control), TextColorControl(control), null);
layout.AddSeparateRow(null, UnselectButton(control), null);

return layout;
}

Control UnselectButton(RadioButtonList list)
{
var control = new Button { Text = "Set SelectedIndex=-1" };
control.Click += delegate
{
list.SelectedIndex = -1;
};
return control;
}

Control AddRowsButton(RadioButtonList list)
{
var control = new Button { Text = "Add" };
Expand Down
37 changes: 37 additions & 0 deletions test/Eto.Test/UnitTests/Forms/Controls/RadioButtonListTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Eto.Forms;
using NUnit.Framework;

namespace Eto.Test.UnitTests.Forms.Controls
{
[TestFixture]
public class RadioButtonListTests : TestBase
{
[Test]
public void ShouldBeAbleToUnselectAll()
{
Shown(form => {
var rbl = new RadioButtonList
{
Items = { "Item 1", "Item 2", "Item 3" },
SelectedIndex = 1
};
Assert.AreEqual(1, rbl.SelectedIndex, "#1.1");
return rbl;
}, rbl => {
Assert.AreEqual(1, rbl.SelectedIndex, "#2.1");
rbl.SelectedIndex = -1;
Assert.AreEqual(-1, rbl.SelectedIndex, "#3.1");
rbl.SelectedKey = "Item 3";
Assert.AreEqual(2, rbl.SelectedIndex, "#3.1");
rbl.SelectedKey = null;
Assert.AreEqual(-1, rbl.SelectedIndex, "#3.1");
});
}

}
}

0 comments on commit 0b47ca7

Please sign in to comment.