Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
This is my code

C#
if (tineline.Location == new Point(newlabel.Location.X, newlabel.Location.Y))
     {
         MessageBox.Show("HI");
     }


and my error is that the message box doesn't pop-up

I already have

Label newlabel = new Label();Label newlabel2 = new Label();


so when I put a label down like doesn't show a messagebox. this is my timer1 code

C#
int linenumber = 0;
        private void timer1_Tick(object sender, EventArgs e)
{
    linenumber++;
            if (tineline.Location.X >= 369)
            {
                tineline.Location = new Point(133, 16);
                pb1.Location = new Point(pb1.Location.X, pb1.Location.Y + 28);
                label5.Text = "" + linenumber;
            }

            if (tineline.Location == new Point(newlabel.Location.X, newlabel.Location.Y))
            {
                MessageBox.Show("HI");
            }


            if (trackBar1.Value == 1)
            {
                tineline.Location = new Point(tineline.Location.X + 1, tineline.Location.Y);
            }
            if (trackBar1.Value == 2)
            {
                tineline.Location = new Point(tineline.Location.X + 5, tineline.Location.Y);
            }
            if (trackBar1.Value == 3)
            {
                tineline.Location = new Point(tineline.Location.X + 10, tineline.Location.Y);
            }
        }

(labels are inside a panel)
Can some one help me, im trying to make it so when one of the locations X or Y is the same as the label it will show the message, can anyone plz help?
Posted

As griff said, that won't work because your comparing the object's pointers - not the actual contents.

You could write an extension method to abstract away the comparison from the programmer:

C#
public static class ExtensionMethods
{
    public static bool MatchesLocation(this Point thisPt, Point thatPt)
    {
        return (thisPt.X == thatPt.X && thisPt.Y == thatPt.Y);
    }
}


and then you could call it like this:

C#
if (tineline.Location.MatchesLocation(newlabel.Location))
{
    MessageBox.Show("HI");
}
 
Share this answer
 
Comments
OriginalGriff 5-Apr-11 5:59am    
Likewise!
That won't work. They two points are not the same, and never will be. They may contain the same information, but that doesn't mean they are the same point.

Instead, try this:
if (tineline.Location.X == newlabel.Location.X && tineline.Location.Y == newlabel.Location.Y)
     {
         MessageBox.Show("HI");
     }


When you do an "==" check, it checks the references, not the contents, except when such behaviour is specifically overridden, as it is for strings. There are exceptions to this for simple types, but most complex types work this way.
 
Share this answer
 
Comments
#realJSOP 5-Apr-11 5:37am    
Looks like someone isn't happy with our answers. I 5'd you to compensate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900