[2005] Expression is a value and therefore cannot be the target of an...
I cannot get this working, I want on form keypress, if they hit w, to move the location of label1's y location -10.
VB Code:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "w" Then
Label1.Location.Y = Label1.Location.Y - 10
End If
End Sub
Thanks for any help!
Re: [2005] Expression is a value and therefore cannot be the target of an...
From MSDN:
Quote:
Because the Point class is a value type (Structure in Visual Basic, struct in 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 object 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 object.
Basically, you can't change label.location.y, but you can change label.left, which would do what you want.
Re: [2005] Expression is a value and therefore cannot be the target of an...
The short of it is that you cannot set a property of a structure directly through another property. You would either need to set the Top property of the Label:or else save the current Location to a variable and then assign it back again:
VB Code:
Dim loc As Point = Label1.Location
loc.Y -= 10
Label1.Location = loc
or else create a new Point object and assign that:
VB Code:
Label1.Location = New Point(Label1.Location.X, Label1.Location.y - 10)
You may not understand this fully but I'll give you the long(er) explanation anyway. Because Location is type Point and is a value type, accessing the Location property returns a COPY of the actual Point object stored in the Label. Setting the Y property of that copy has no affect whatsoever on the original, thus it could not affect the Label. For that reason the compiler will not allow you to even try. When you access the property directly like that the copy only exists temporarily. If you assign the copy to a variable, as I did in the second example above, it then becomes more permanent. You can then edit the copy and assign it back to the original property. That will overwrite the original value stored in the Label, thus the Label is affected.
Re: [2005] Expression is a value and therefore cannot be the target of an...
Dagnabit! What Refresh button? ;)
Re: [2005] Expression is a value and therefore cannot be the target of an...
Yes, but...
VB Code:
If e.KeyChar = "w" Then
Label1.Top = Label1.Location.Y - 3
End If
If e.KeyChar = "a" Then
Label1.Left = Label1.Location.X - 3
End If
Those work, but right and bottom don't...they say they are readonly.
Re: [2005] Expression is a value and therefore cannot be the target of an...
That's because you're just GETTING the value of Label1.Location.Y and not SETTING it. Once the calculation on the right is performed you're assigning the result to a property of Label1, not to a property of a property of Label1.
As I said, you weren't likely to understand that explanation properly because it requires a relatively good understanding of the difference between value types and reference types and how they are manipulated. Understanding what the stack and the heap are is a help too, although not essential. I would recommend to everyone who doesn't know the difference to search MSDN and do some readin on the topic.
Re: [2005] Expression is a value and therefore cannot be the target of an...
Quote:
Originally Posted by Copernicus
Yes, but...
VB Code:
If e.KeyChar = "w" Then
Label1.Top = Label1.Location.Y - 3
End If
If e.KeyChar = "a" Then
Label1.Left = Label1.Location.X - 3
End If
Those work, but right and bottom don't...they say they are readonly.
Hmmm... I read this post again.
A control's Location is determined by the coordinates of its upper, left corner, which correspond to its Top and its Left. Bottom and Right are ReadOnly because they are calculated. Bottom is equal to Top plus Height, while Right is equal to Left plus Width. If you change the Top or Height property then the Bottom property will change accordingly, but you cannot set it directly. It exists for convenience, so that you don't have to calculate it yourself if you need to position other controls relative to the bottom of this one.