private void Team1_SelectedIndexChanged(object sender, EventArgs e)
{
Team2.SelectedIndex = Team1.SelectedIndex;
}
private void Team2_SelectedIndexChanged(object sender, EventArgs e)
{
Team1.SelectedIndex = Team2.SelectedIndex;
}
private void btnRemove_Click(object sender, EventArgs e)
{
Team1.Items.Remove(Team2.SelectedItem);
Team2.Items.Remove(Team1.SelectedItem);
}
ListBox help
Page 1 of 111 Replies - 510 Views - Last Post: 20 February 2011 - 07:39 PM
#1
ListBox help
Posted 20 February 2011 - 01:33 PM
Hello i'm new to windows form programming in c#. Here's my problem: I have 2 listbox that are associated and I want to remove elements from both listbox with the remove button. When i try to remove elements from both listbox i get "InvalidArgument=Value of '2' is not valid for 'SelectedIndex'".
Replies To: ListBox help
#2
Re: ListBox help
Posted 20 February 2011 - 02:03 PM
Which bit of code are you getting the error on?
For the first part of your code you should check the value of the SelectedIndex first as it will change once when it leaves the current item and again when it selects the new item.
For the first part of your code you should check the value of the SelectedIndex first as it will change once when it leaves the current item and again when it selects the new item.
#3
Re: ListBox help
Posted 20 February 2011 - 02:07 PM
The error is coming from the SelectedIndexChanged event. I'm not sure how to correct it.
#4
Re: ListBox help
Posted 20 February 2011 - 03:06 PM
at a glance i think its because when you remove the item, the index changes and fires the IndexChanged event. Try:
which is short-hand for:
private void Team2_SelectedIndexChanged(object sender, EventArgs e)
{
Team1.SelectedIndex = (Team1.Items.Count >= Team2.SelectedIndex) ? Team2.SelectedIndex : -1;
}
which is short-hand for:
if (Team1.Items.Count >= Team2.SelectedIndex)
Team1.SelectedIndex = Team2.SelectedIndex;
else
Team1.SelectedIndex = -1;
This post has been edited by jayfella: 20 February 2011 - 03:08 PM
#5
Re: ListBox help
Posted 20 February 2011 - 03:10 PM
Nearly all collections in .NET are zero indexed.
If you have two items they are indexes 0 and 1.
If you have five items they are indexes 0, 1, 2, 3, 4
Also take a look at line 15
As soon as you remove the selected item it means that some other item is therefore suddenly selected, triggering your .SelectedItemChanged event handler. Once that selection happens you no longer have that many items in both listboxes because you've deleted one.
In other words, as soon as line 15 executes BUT BEFORE LINE 16 EXECUTES the selected index changed handler gets triggered. So between lines 15 and 16 your two listboxes are not the same.
Breakpoints. Breakpoints are your friend. If you were to walk through this and look at the listbox.items collection you would see this happen.
If you have two items they are indexes 0 and 1.
If you have five items they are indexes 0, 1, 2, 3, 4
Also take a look at line 15
As soon as you remove the selected item it means that some other item is therefore suddenly selected, triggering your .SelectedItemChanged event handler. Once that selection happens you no longer have that many items in both listboxes because you've deleted one.
In other words, as soon as line 15 executes BUT BEFORE LINE 16 EXECUTES the selected index changed handler gets triggered. So between lines 15 and 16 your two listboxes are not the same.
Breakpoints. Breakpoints are your friend. If you were to walk through this and look at the listbox.items collection you would see this happen.
#6
Re: ListBox help
Posted 20 February 2011 - 03:24 PM
jayfella, I still get the same errors
#7
Re: ListBox help
Posted 20 February 2011 - 03:31 PM
I understand whats going on but I'm not to sure on how to fix it?
tlhIn`toq, on 20 February 2011 - 03:10 PM, said:
Nearly all collections in .NET are zero indexed.
If you have two items they are indexes 0 and 1.
If you have five items they are indexes 0, 1, 2, 3, 4
Also take a look at line 15
As soon as you remove the selected item it means that some other item is therefore suddenly selected, triggering your .SelectedItemChanged event handler. Once that selection happens you no longer have that many items in both listboxes because you've deleted one.
In other words, as soon as line 15 executes BUT BEFORE LINE 16 EXECUTES the selected index changed handler gets triggered. So between lines 15 and 16 your two listboxes are not the same.
Breakpoints. Breakpoints are your friend. If you were to walk through this and look at the listbox.items collection you would see this happen.
If you have two items they are indexes 0 and 1.
If you have five items they are indexes 0, 1, 2, 3, 4
Also take a look at line 15
As soon as you remove the selected item it means that some other item is therefore suddenly selected, triggering your .SelectedItemChanged event handler. Once that selection happens you no longer have that many items in both listboxes because you've deleted one.
In other words, as soon as line 15 executes BUT BEFORE LINE 16 EXECUTES the selected index changed handler gets triggered. So between lines 15 and 16 your two listboxes are not the same.
Breakpoints. Breakpoints are your friend. If you were to walk through this and look at the listbox.items collection you would see this happen.
#8
Re: ListBox help
Posted 20 February 2011 - 04:09 PM
coastal5, on 20 February 2011 - 04:31 PM, said:
I understand whats going on but I'm not to sure on how to fix it?
Very straightforward logic here:
If the problem is that you are reacting to the SelectedIndexChanged event while you are still deleting items...
then stop listening for the SelectedIndedChanged event while you are deleting items.
- At the start of your delete method unsubscribe from the SelectedIndexChanged events.
- Do your deletes
- Re-subscribe to the events.
private void Team1_SelectedIndexChanged(object sender, EventArgs e)
{
Team2.SelectedIndex = Team1.SelectedIndex;
}
private void Team2_SelectedIndexChanged(object sender, EventArgs e)
{
Team1.SelectedIndex = Team2.SelectedIndex;
}
private void btnRemove_Click(object sender, EventArgs e)
{
Team1.SelectedIndexChanged -= SelectedIndexChanged;
Team2.SelectedIndexChanged -= SelectedIndexChanged;
Team1.Items.Remove(Team2.SelectedItem);
Team2.Items.Remove(Team1.SelectedItem);
Team1.SelectedIndexChanged += SelectedIndex;
Team2.SelectedIndexChanged += SelectedIndexChanged;
}
#10
Re: ListBox help
Posted 20 February 2011 - 07:17 PM
Quote
Breakpoints. Breakpoints are your friend. If you were to walk through this and look at the listbox.items collection you would see this happen.
Have you put breakpoints in the deletion method?
When you look at the items collection of each listbox what are the contents?
Write it out on paper if you need to.
Make a list of Team1.Items
Make a list of Team2.Items
Breakpoint at line 15 and and walk through it. Look at the collections to see what is happening.
Feel free to post screen shots along the way if you need to so we can see what you see and help guide you to it. But we don't have your project so we can't exactly duplicate your results.
#11
Re: ListBox help
Posted 20 February 2011 - 07:32 PM
tlhIn`toq, on 20 February 2011 - 07:17 PM, said:
Quote
Breakpoints. Breakpoints are your friend. If you were to walk through this and look at the listbox.items collection you would see this happen.
Have you put breakpoints in the deletion method?
When you look at the items collection of each listbox what are the contents?
Write it out on paper if you need to.
Make a list of Team1.Items
Make a list of Team2.Items
Breakpoint at line 15 and and walk through it. Look at the collections to see what is happening.
Feel free to post screen shots along the way if you need to so we can see what you see and help guide you to it. But we don't have your project so we can't exactly duplicate your results.
I just have 2 listbox with 4 random numbers inside each listbox and a remove button. For some reason i cannot remove both selected numbers from each listbox?
#12
Re: ListBox help
Posted 20 February 2011 - 07:39 PM
I'll say it again, but more directly this time.
- Put a breakpoint at line 15.
- Open the Locals and Autos pallet.
- Have them set so you can see all the values of each listbox.list collection.
- Take a screen shot at the start of each line from 15 to the end of the method. That should amount to about 5 screen shots.
- Post them to this thread so we can see what you see.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|