11 Replies - 691 Views - Last Post: 20 February 2011 - 07:39 PM Rate Topic: -----

#1 coastal5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

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'".
 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);
        }



Is This A Good Question/Topic? 0
  • +

Replies To: ListBox help

#2 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

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.
Was This Post Helpful? 0
  • +
  • -

#3 coastal5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

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.
Was This Post Helpful? 0
  • +
  • -

#4 jayfella  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 07-March 09

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:

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

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,666
  • Joined: 02-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#6 coastal5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

Re: ListBox help

Posted 20 February 2011 - 03:24 PM

jayfella, I still get the same errors
Was This Post Helpful? 0
  • +
  • -

#7 coastal5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

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?

View PosttlhIn`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.

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,666
  • Joined: 02-June 10

Re: ListBox help

Posted 20 February 2011 - 04:09 PM

View Postcoastal5, 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;
       }



Was This Post Helpful? 0
  • +
  • -

#9 coastal5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

Re: ListBox help

Posted 20 February 2011 - 04:59 PM

doesn"t seem to work either?
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,666
  • Joined: 02-June 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.
Was This Post Helpful? 0
  • +
  • -

#11 coastal5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

Re: ListBox help

Posted 20 February 2011 - 07:32 PM

View PosttlhIn`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?
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,666
  • Joined: 02-June 10

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.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1