Determining if the mouse is on top of a box I have 4 picture boxes and a collision detection to see if my mouse is on top of any of them, but I have to run the same code 4 different times, but just changing the name. Also, the else if statements can be regular if statements.

PictureBox display = sender as PictureBox;
Point drop = PointToClient(Cursor.Position);

if (((displayCollision(drop, pbDisplay1)) == true) &amp;&amp; (display != pbDisplay1))
{
    display.Location = pbDisplay1.Location;
    display.Size = pbDisplay1.Size;

    pbDisplay1.Location = defaultLocation;
    pbDisplay1.Size = defaultSize;
}
else if (((displayCollision(drop, pbDisplay2)) == true) &amp;&amp; (display != pbDisplay2))
{
    display.Location = pbDisplay2.Location;
    display.Size = pbDisplay2.Size;

    pbDisplay2.Location = defaultLocation;
    pbDisplay2.Size = defaultSize;
}
else if (((displayCollision(drop, pbDisplay3)) == true) &amp;&amp; (display != pbDisplay3))
{
    display.Location = pbDisplay3.Location;
    display.Size = pbDisplay3.Size;

    pbDisplay3.Location = defaultLocation;
    pbDisplay3.Size = defaultSize;
}
else if (((displayCollision(drop, pbDisplay4)) == true) &amp;&amp; (display != pbDisplay4))
{ 
    display.Location = pbDisplay4.Location;
    display.Size = pbDisplay4.Size;

    pbDisplay4.Location = defaultLocation;
    pbDisplay4.Size = defaultSize;
}
else 
{
    display.Location = defaultLocation;
}
I don't see a convenient way to do a for loop, but you could extract common functionality into a single function:

bool CheckAndModify(PictureBox pb)
{
    if (displayCollision(drop, pb) &amp;&amp; (display != pb))
    {
        display.Location = pb.Location;
        display.Size = pb.Size;
        pb.Location = defaultLocation;
        pb.Size = defaultSize;
        return true;
    }
    return false;
}


And then:

bool modified = CheckAndModify(pbDisplay1) ||
                CheckAndModify(pbDisplay2) ||
                CheckAndModify(pbDisplay3) ||
                CheckAndMovify(pbDisplay4);


Or, I suppose a loop would be possible:

foreach (var pbDisplay in new[]{pbDisplay1, pbDisplay2, pbDisplay3, pbDisplay4})
{
    if (CheckAndModify(pbDisplay))
    {
        break;
    }
}