C#无法理解为什么此代码无法正常工作 [英] C# not understanding why this code isn't working

查看:70
本文介绍了C#无法理解为什么此代码无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

This is my code

private void timer1_Tick(object sender, EventArgs e)
     {
pictureBox1.Location = new Point(pictureBox1.Location.X += 1, pictureBox1.Location.Y);
     }



这是我的错误



and this is my error

Error   1   Cannot modify the return value of 'System.Windows.Forms.Control.Location' because it is not a variable  Z:\Longitudinal Studio\Longitudinal Studio\Form1.cs 73  47  Longitudinal Studio

推荐答案

"Because the Point class is a value type (Structure in Visual Basic, struct in Visual C#), it is returned by value, meaning accessing the property returns a copy of the upper-left point of the control. So, adjusting the X or Y properties of the Point returned from this property will not affect the Left, Right, Top, or Bottom property values of the control. To adjust these properties set each property value individually, or set the Location property with a new Point."


资料来源:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.location.aspx [ ^ ]

这是麻烦声明


Source :
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.location.aspx[^]

So this is trouble statement

pictureBox1.Location.X += 1


Lakamraju Raghuram 给出的解决方案1是很好的答案,直指重点.
我想补充一点,即使可以修改pictureBox1.Location.X,在下面的语句中也是多余的,因为再次将相同的值作为参数传递给构造函数的参数,该值将再次在构造函数中设置.
The solution 1 given by Lakamraju Raghuram is very good answer and straight to the point.
I want to add that, even if pictureBox1.Location.X can be modified, it is redundant in the following statement as the same value is again passed as an argument to the parameter of the constructor, which will be set in the constructor again.
pictureBox1.Location = new Point(pictureBox1.Location.X += 1, pictureBox1.Location.Y);


所以,
pictureBox1.Location.X + 1将足以代替
pictureBox1.Location.X += 1


So,
pictureBox1.Location.X + 1 will be sufficient instead of
pictureBox1.Location.X += 1


这篇关于C#无法理解为什么此代码无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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