0

I've come across a weird bug I can't solve. I have a list of Controls that are in a panel, and each control has an up arrow (move up) button. This will swap the control's location with the one above it with the following code:

curControl = parent.dataControls[EntryIDNum];       //is 1
swapControl = parent.dataControls[EntryIDNum - 1];  //is 0
tempID = curControl.EntryIDNum;
tempPnt = curControl.Location;

curControl.Location = swapControl.Location;
curControl.EntryIDNum = swapControl.EntryIDNum;     //is now 0
swapControl.Location = tempPnt;
swapControl.EntryIDNum = tempID;                   //is now 1

Say my list has 5 controls. If I move control 4 up one, it swaps with control 3 no problem. However, if I move the last control (number 5) up, the curControl.Location = swapControl.Location happens backwards. For example:

//curControl.Location = (5, 333)
//swapControl.Location = (5, 157)

curControl.Location = swapControl.Location;

//curControl.Location = (5, 333)
//swapControl.Location = (5, 333)

This is IF AND ONLY IF I'm moving up the last control in my list. All other controls swap with the other control with no problem.

Let me know if there is anymore info needed.

Ttbot
  • 1
  • 1
  • Possible duplicate of http://stackoverflow.com/questions/4347902/when-is-a-c-sharp-value-object-copied-and-when-is-its-reference-copied – Prajwal Feb 09 '17 at 12:48
  • 1
    We ideally need a [mcve] to be able to help here. My suspicion is that you're misdiagnosing something here. My first guess would be that `Location` and `EntryIDNum` are unrelated to the order in which elements in `dataControls` are indexed, – Damien_The_Unbeliever Feb 09 '17 at 13:03
  • You are updating the control's EntryIdNum property but you are not updating the parent.dataControls[] array. So the array no longer indexes the controls correctly anymore after one swap. What happens next is unlikely to be pretty. – Hans Passant Feb 09 '17 at 14:59

0 Answers0