Results 1 to 7 of 7

Thread: [2005] Expression is a value and therefore cannot be the target of an...

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    [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:
    1. Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    2.         If e.KeyChar = "w" Then
    3.             Label1.Location.Y = Label1.Location.Y - 10
    4.         End If
    5.     End Sub

    Thanks for any help!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Expression is a value and therefore cannot be the target of an...

    From MSDN:

    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.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    VB Code:
    1. Label1.Top -= 10
    or else save the current Location to a variable and then assign it back again:
    VB Code:
    1. Dim loc As Point = Label1.Location
    2.  
    3. loc.Y -= 10
    4. Label1.Location = loc
    or else create a new Point object and assign that:
    VB Code:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Expression is a value and therefore cannot be the target of an...

    Dagnabit! What Refresh button?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    Re: [2005] Expression is a value and therefore cannot be the target of an...

    Yes, but...

    VB Code:
    1. If e.KeyChar = "w" Then
    2.             Label1.Top = Label1.Location.Y - 3
    3.         End If
    4.         If e.KeyChar = "a" Then
    5.             Label1.Left = Label1.Location.X - 3
    6.         End If

    Those work, but right and bottom don't...they say they are readonly.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Expression is a value and therefore cannot be the target of an...

    Quote Originally Posted by Copernicus
    Yes, but...

    VB Code:
    1. If e.KeyChar = "w" Then
    2.             Label1.Top = Label1.Location.Y - 3
    3.         End If
    4.         If e.KeyChar = "a" Then
    5.             Label1.Left = Label1.Location.X - 3
    6.         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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width