ComboBox remove from index problem
-
Gents, I'm experimenting with indexing and I came across the following problem that I cannot fully grasp. I have a combobox filled with stuff ( the collection items ). I want to remove it all at once so that the collection is empty again. So after some thinking I though I try the follwing :
private void ClearComboBoxes() { for (int x = 0; x < cmbtrackWhere.Items.Count; x++) { cmbtrackWhere.Items.RemoveAt[x]; } }
It gives me the error : " Cannot apply indexing [] to an expression of type 'method group' I assign the value zero to the integerx
, count the items in the collection and add 1 tox
if necessary untilx
is equal to the number of items in the collection. Then I remove each item on index[x]
from the collection. Am I thinking in the right direction ? .. can anyone briefly explain to me what I'm doing wrong and why it fails ? kind regards, -
Gents, I'm experimenting with indexing and I came across the following problem that I cannot fully grasp. I have a combobox filled with stuff ( the collection items ). I want to remove it all at once so that the collection is empty again. So after some thinking I though I try the follwing :
private void ClearComboBoxes() { for (int x = 0; x < cmbtrackWhere.Items.Count; x++) { cmbtrackWhere.Items.RemoveAt[x]; } }
It gives me the error : " Cannot apply indexing [] to an expression of type 'method group' I assign the value zero to the integerx
, count the items in the collection and add 1 tox
if necessary untilx
is equal to the number of items in the collection. Then I remove each item on index[x]
from the collection. Am I thinking in the right direction ? .. can anyone briefly explain to me what I'm doing wrong and why it fails ? kind regards,If you want to remove all of the items in a combobox, you should use:
cmbtrackWhere.Items.Clear();
This will remove them all at once. If you are trying to remove an item at a specific index, i think your code should be something along the lines of:// Use parentheses instead of brackets cmbtrackWhere.Items.RemoveAt(x);
-
Gents, I'm experimenting with indexing and I came across the following problem that I cannot fully grasp. I have a combobox filled with stuff ( the collection items ). I want to remove it all at once so that the collection is empty again. So after some thinking I though I try the follwing :
private void ClearComboBoxes() { for (int x = 0; x < cmbtrackWhere.Items.Count; x++) { cmbtrackWhere.Items.RemoveAt[x]; } }
It gives me the error : " Cannot apply indexing [] to an expression of type 'method group' I assign the value zero to the integerx
, count the items in the collection and add 1 tox
if necessary untilx
is equal to the number of items in the collection. Then I remove each item on index[x]
from the collection. Am I thinking in the right direction ? .. can anyone briefly explain to me what I'm doing wrong and why it fails ? kind regards,So the mistake was to use square brackects; RemoveAt is a method, it needs parentheses. Furthermore, as soon as you remove an item, all the higher-numbered items get moved down one positition, so if you need to remove al but the first N items the correct code would be:
for (int x = N; x < cmbtrackWhere.Items.Count; x++) {
cmbtrackWhere.Items.RemoveAt(N); // yes, always the first you dont want any more!
}This loop continues to remove the first unwanted item, until there are no more unwanted items left. There is of course the alternative to remove them in reverse order:
for (int x = cmbtrackWhere.Items.Count-1; x>=N; x--) {
cmbtrackWhere.Items.RemoveAt(x);
}Of course, if you want to remove everything there is the Clear() method to do that. :)
Luc Pattyn [My Articles] [Forum Guidelines]