1

I have combobox which is correctly populated with some field ID when button is clicked.

private void Button_Click(object sender, RoutedEventArgs e)
{         
    results.Items.Add(ID);          
}

Now I want when I change some value to delete previous value (or values in case I have multiple values in combobox) but I am always getting exception (if some value is already selected in Combo Box) I tried to add in that method on the top this:

results.Items.Clear();

and I tried this:

for (int i = 0; i < results.Items.Count; i++)
{
    results.Items.RemoveAt(i);
    i--;
}

But always getting exception:

System.ArgumentException: Value does not fall within the expected range. at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData) at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData) at MS.Internal.XcpImports.Collection_Add[T](PresentationFrameworkCollection'1 collection, Object value) at System.Windows.PresentationFrameworkCollection'1.AddImpl(Object value) at System.Windows.Controls.ItemCollection.AddImpl(Object value) at System.Windows.Controls.ItemCollection.AddInternal(Object value) at System.Windows.PresentationFrameworkCollection'1.Add(T value) at SXPCreateIncident3.SilverlightControl1.results_SelectionChanged(Object sender, SelectionChangedEventArgs e) at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArg

If I don't have this part with Clear (Remove) then combobox has more elements on every button Click but I need to clear previous content when button is clicked.

Veljko
  • 1,708
  • 12
  • 40
  • 80

2 Answers2

2

Did you try unselecting all items before deleting:

results.SelectedIndex = -1;
results.Items.Clear();

And in case Clear would still cause some trouble, shouldn't your second method be:

for (int i = results.Items.Count - 1; i >= 0; i--)
{
    results.Items.RemoveAt(i);
}
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146
0

I'm not entirely sure how result.Items is bound to you Combobox but you might try to replace the unwanted item with a new one:

private void Button_Click(object sender, RoutedEventArgs e)
{
    // itemToRemove should already be set    
    var index = result.Items.IndexOf(itemToRemove);     
    results.Items[index ] = ID;          
}

To remove multiple items, do not use an iterator. Removing things from a collection while using an iterator messes the iterator up. You could however do this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    for(var i = 0; i < result.Items.Count; i++)
    {
        // itemsToRemove should be populated with the IDs you want to remove.
        if(itemsToRemove.Contains(result.Items[i])
        {
            result.RemoveAt(i);
        }
    }
    result.Items.Add(ID);
}

This loop won't get messed up because every time the expression i < result.Items.Count is evaluated and Count be one less then the previous Count when an ID has been removed.

EDIT To clear the combobox and populate it with new items you'll have to provide a new ItemsSource for the combobox:

private void Button_Click(object sender, RoutedEventArgs e)
{
    results.ItemsSource = null;
    results.ItemsSource = new List<SomeType>(); // replace SomeType with the type of ID.
    results.Items.Add(ID);          
}
venerik
  • 5,766
  • 2
  • 33
  • 43
  • Hi Venerik thanks but what is itemsToRemove??? I want to remove all. How should I then set this itemsToRemove? – Veljko Mar 12 '16 at 20:59
  • You wrote "Now I want when I change some value to delete previous value (or values in case I have multiple values in combobox)". So I assumed you somehow keep track of the values you want to remove. That are the values you should put in the `itemsToRemove`. – venerik Mar 12 '16 at 21:09
  • Hi venerik I want to remove all values in combox in case I have selected some value in combobox because I want on button click to populate combobox once again from scratch. Is the itemsToRemove some kind of ListBox that I should populate? Or can you please assist me how to populate it before I remove all from combobox? – Veljko Mar 12 '16 at 21:14
  • Hi unforunately again the same issue: Value does not fall within the expected range. First value is populated correctly but immediately if I select it and click again the button it throws error. ID is value which I retrieve as a string in field ID. results.ItemsSource = null; results.ItemsSource = new List(); results.Items.Add(ticketID); – Veljko Mar 12 '16 at 23:04