Eitan Yona
Eitan Yona

Reputation: 75

Replace buttons location

l have problem with my c# winform project.

In my project I have a function that switches the location of buttons to their old location if they are in the same area.

private void myText_MouseUp(object sender, MouseEventArgs e) {

Point q = new Point(0, 0);
        Point q2 = new Point(0, 0);
        bool flag = false;
        int r = 0;
        foreach (Control p in this.Controls)
        {
            for (int i = 0; i < counter; i++)
            {
                if (flag)
                {
                    if (p.Location.X == locationx[i] && p.Location.Y == locationy[i])
                    {
                        oldx = e.X;
                        oldy = e.Y;
                        flag = true;
                        r = i;
                    }
                }
            }
        }
        foreach (Control p in this.Controls)
        {
            for (int j = 0; j < counter; j++)
            {
                if ((locationx[j] == p.Location.X) && (locationy[j] == p.Location.Y))
                {
                    Point arrr = new Point(oldx, oldy);
                    buttons[j].Location = arrr;
                    buttons[r].Location = new Point(locationx[j], locationy[j]);
                }
            }
        }
}
   The problem with this code is that if they are in the same area, the buttons do not switch their locations.  Instead they goes to the last button location.

If someone could help me that will help me alot :)

Upvotes: 2

Views: 2314

Answers (1)

David Heffernan
David Heffernan

Reputation: 613592

The if statement always evaluates to true. This means that the final j loop will do this:

// last time round the i loop, i == counter-1
// and q == new Point(locationx[counter-1], locationy[counter-1])
for (int j = 0; j < counter; j++)
{
    Point q2 = new Point(locationx[j], locationy[j]);
    buttons[i].Location = q2;
    buttons[j].Location = q;
}

The net result of this is that every button's Location is set to q, which is

new Point(locationx[counter-1], locationy[counter-1])

Why does the if statement always evaluate to true. Well, first of all let's look at a couple of the or clauses in the if statement:

|| ((q.Y >= q2.Y) && (q.X <= q2.X))
|| ((q.Y >= q2.Y) && (q.X == q2.X))

This is equivalent to

|| ((q.Y >= q2.Y) && (q.X <= q2.X))

The line containing the == test has absolutely no impact on the final result of the condition. In fact all the lines containing == can be similarly treated. This leaves:

|| ((q.Y >= q2.Y) && (q.X <= q2.X))
|| ((q.Y >= q2.Y) && (q.X >= q2.X))
|| ((q.Y <= q2.Y) && (q.X >= q2.X))
|| ((q.Y <= q2.Y) && (q.X <= q2.X))

We can condense

|| ((q.Y >= q2.Y) && (q.X <= q2.X))
|| ((q.Y >= q2.Y) && (q.X >= q2.X))

into

|| ((q.Y >= q2.Y)

and similarly

|| ((q.Y <= q2.Y) && (q.X >= q2.X))
|| ((q.Y <= q2.Y) && (q.X <= q2.X))

is the same as

|| ((q.Y <= q2.Y)

Combine

|| ((q.Y >= q2.Y)
|| ((q.Y <= q2.Y)

and you can see that the if condition always evaluates to true.

Upvotes: 2

Related Questions