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 🙂
The
ifstatement always evaluates to true. This means that the finaljloop will do this:The net result of this is that every button’s
Locationis set toq, which isWhy does the
ifstatement always evaluate totrue. Well, first of all let’s look at a couple of theorclauses in theifstatement:This is equivalent to
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:We can condense
into
and similarly
is the same as
Combine
and you can see that the
ifcondition always evaluates totrue.