Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: mono/mono
base: 68069f0566
...
head fork: mono/mono
compare: da25f624ed
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on May 21, 2012
@ermshiperete ermshiperete Xamarin-4921: Fix Listbox.SelectedItem after adding sorted item
When we add an item to a sorted listbox the indices of the selected
items change and need to be updated.

Change-Id: Ia32849fe10480354835dcd48b37d326d612af04f
759d650
Commits on May 24, 2012
@migueldeicaza migueldeicaza Merge pull request #298 from ermshiperete/4921
Xamarin-4921: Fix Listbox.SelectedItem after adding sorted item
da25f62
View
15 mcs/class/Managed.Windows.Forms/System.Windows.Forms/ListBox.cs
@@ -2531,13 +2531,26 @@ public ObjectCollection (ListBox owner, ObjectCollection value)
public int Add (object item)
{
int idx;
+ object[] selectedItems = null;
+
+ // we need to remember the original selected items so that we can update the indices
+ if (owner.sorted) {
+ selectedItems = new object[owner.SelectedItems.Count];
+ owner.SelectedItems.CopyTo (selectedItems, 0);
+ }
idx = AddItem (item);
owner.CollectionChanged ();
// If we are sorted, the item probably moved indexes, get the real one
- if (owner.sorted)
+ if (owner.sorted) {
+ // update indices of selected items
+ owner.SelectedIndices.Clear ();
+ for (int i = 0; i < selectedItems.Length; i++) {
+ owner.SelectedIndex = this.IndexOf (selectedItems [i]);
+ }
return this.IndexOf (item);
+ }
return idx;
}
View
53 mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ListBoxTest.cs
@@ -854,6 +854,59 @@ void AddItems (bool sorted, object[] items, int[] expectedAddPositions)
}
Assert.AreEqual ((Array)expectedAddPositions, (Array)addedAtList.ToArray (typeof (int)), "addedAtList");
}
+
+ [Test]
+ public void SelectedIndexUpdated () // Xamarin bug 4921
+ {
+ using (Form f = new Form ()) {
+ f.ShowInTaskbar = false;
+
+ ListBox l = new ListBox ();
+ l.Sorted = true;
+ f.Controls.Add (l);
+
+ l.Items.Add ("B");
+ l.SelectedIndex = 0;
+
+ Assert.AreEqual (0, l.SelectedIndex);
+
+ l.Items.Add ("A");
+ Assert.AreEqual (1, l.SelectedIndex);
+ }
+ }
+
+ [Test]
+ public void SelectedIndexUpdated_MultiSelect () // Xamarin bug 4921
+ {
+ using (Form f = new Form ()) {
+ f.ShowInTaskbar = false;
+
+ ListBox l = new ListBox ();
+ l.Sorted = true;
+ l.SelectionMode = SelectionMode.MultiSimple;
+ f.Controls.Add (l);
+
+ l.Items.Add ("B");
+ l.Items.Add ("C");
+ l.SelectedIndex = 0;
+ l.SelectedIndex = 1;
+
+ Assert.AreEqual (2, l.SelectedIndices.Count);
+ Assert.AreEqual (0, l.SelectedIndices [0]);
+ Assert.AreEqual (1, l.SelectedIndices [1]);
+ Assert.AreEqual (2, l.SelectedItems.Count);
+ Assert.AreEqual ("B", l.SelectedItems [0]);
+ Assert.AreEqual ("C", l.SelectedItems [1]);
+
+ l.Items.Add ("A");
+ Assert.AreEqual (2, l.SelectedIndices.Count);
+ Assert.AreEqual (1, l.SelectedIndices[0]);
+ Assert.AreEqual (2, l.SelectedIndices[1]);
+ Assert.AreEqual (2, l.SelectedItems.Count);
+ Assert.AreEqual ("B", l.SelectedItems [0]);
+ Assert.AreEqual ("C", l.SelectedItems [1]);
+ }
+ }
}
[TestFixture]

No commit comments for this range

Something went wrong with that request. Please try again.