0

I have a list view that has two columns: one for the prefix and one for the connection string.

Example:

Prefix| Connection string
--------------------------
OR1_  | blablabla
--------------------------
OR2_  | blebleble
--------------------------
OR3_  | blublublu
--------------------------

(Sorry for the makeshift table, I think it will be easier to understand)

So now whenever I delete an item from the listView I need to rename all the prefixes so imagine if I delete the item number 2, I will end up with:

Prefix| Connection string
--------------------------
OR1_  | blablabla
--------------------------
OR3_  | blublublu
--------------------------

But I actually need it to be OR1_ and OR2_

so I made this code that does that when I delete an item:

 private void buttonDel_Click(object sender, EventArgs e)
        {
            if (listViewConnectionStrings.Items.Count == 0) return;
            else
            {
                //Delete the items
                foreach (ListViewItem eachItem in listViewConnectionStrings.SelectedItems)
                {
                    Connections.Remove(eachItem.Text);
                    listViewConnectionStrings.Items.Remove(eachItem);
                }
                if(listViewConnectionStrings.Items.Count == 0) return;

                //Rename the prefixes
                int cntItems = listViewConnectionStrings.Items.Count;
                for (int i = 0; i <= cntItems; i++)
                {
                    ListViewItem newItem = new ListViewItem();
                    newItem = listViewConnectionStrings.Items[0];
                    int newPrefix = i++;
                    newItem.Text = @"OR" + newPrefix.ToString() + @"_";
                }

            }
        }

The last for is working fine when I delete the first item on the list, it renames all the others properly, but If I delete one in the middle or at the bottom, well one that's not the first all the others get the same name.

What am I missing, what's wrong?

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • Are you deleting more than one item at time or just one item is selected? IE The MultiSelect property is set to true or false? – Steve Jul 02 '16 at 22:33

1 Answers1

0

The first thing I see in your code, is that in the for loop, you are incrementing i two times, first at the beggining of the loop and second, in the second block when you assign it to the newPrefix and this could be a problem:

            int cntItems = listViewConnectionStrings.Items.Count;
            for (int i = 0; i <= cntItems; i++)//here you do i++
            {
                ListViewItem newItem = new ListViewItem();
                newItem = listViewConnectionStrings.Items[0];
                int newPrefix = i++;//here you increment i again
                newItem.Text = @"OR" + newPrefix.ToString() + @"_";
            }

what should be used, not to insert undesired behavior is when you want to assign i to newPrefix is use i+1 instead of i++.

And the second thing that makes me think is how you use your listViewConnectionStrings.Items because you never change the index of your Item. It's only used Item[0]. You can try again with both modifications as follows:

            int cntItems = listViewConnectionStrings.Items.Count;
            for (int i = 0; i <= cntItems; i++)//here you do i++
            {
                ListViewItem newItem = new ListViewItem();
                newItem = listViewConnectionStrings.Items[i];
                int newPrefix = i+1;//here you increment i again
                newItem.Text = @"OR" + newPrefix.ToString() + @"_";
            }
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • I've tried and there is no difference, plus i++ would be the correct way to do it because it returns i + 1 but keeps i with the same value as it was – LikeIfYouCaredAboutMyName Jul 03 '16 at 02:04
  • 1
    @LikeIfYouCaredAboutMyName , please note that if you do i++, it will always change the value of i, but after the end of the assignation in your case. Please try again, I have updated my answer. Cheers! – meJustAndrew Jul 03 '16 at 05:18
  • ok that worked thank you. It renames de items properly but gives an exception saying that the index is not valid, so I think that I need to increment i but keep it with the value that he had. is that possible? – LikeIfYouCaredAboutMyName Jul 03 '16 at 14:47