C#位置面板代码问题 [英] C# Problem with location panel code

查看:85
本文介绍了C#位置面板代码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

This is my code

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();



因此,当我放下标签时,例如不会显示消息框.这是我的timer1代码



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

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?

推荐答案

正如格里夫所说,这是行不通的,因为您在比较对象的指针-而不是实际内容.

您可以编写一个扩展方法来从程序员那里抽象出比较结果:

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:

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:

if (tineline.Location.MatchesLocation(newlabel.Location))
{
    MessageBox.Show("HI");
}


那是行不通的.他们的两点不一样,而且永远不会.它们可能包含相同的信息,但这并不意味着它们是同一点.

相反,请尝试以下操作:
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.


这篇关于C#位置面板代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆