Results 1 to 15 of 15

Thread: [Resolved] [2005] moving a picturebox

  1. #1

    Thread Starter
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Resolved [Resolved] [2005] moving a picturebox

    I want to change the value of the place where the picturebox goes, so i put:

    VB Code:
    1. Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    2.         '677,562
    3.         pbCar.Location.X(677)
    4.         pbCar.Location.Y(562)
    5.     End Sub

    Thinking it would cope, it says "Property access must assign to the property or use its value"...

    Can anybody help me with what the hell its going on about?
    Last edited by kregg; Apr 3rd, 2006 at 03:12 PM. Reason: Resolved

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] moving a picturebox

    shouldnt it be:

    VB Code:
    1. Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    2.         '677,562
    3.         pbCar.Location.X = 677
    4.         pbCar.Location.Y = 562
    5.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] moving a picturebox

    The Location.X and Location.Y properties are readonly, thus you cant set the values. They just display the x and y coordinates of the Location property. However, the Location property is not readonly, and is a point type, and you can set a new location like below:
    VB Code:
    1. PictureBox1.Location = New Point(50, 50)

  4. #4

    Thread Starter
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Re: [2005] moving a picturebox

    Thanks gigemboy and atheist for your help. It now moves the picture. My problem now is that I want the user to press the left key to move it left, press right to move it right etc. My problem is getting it to actually do it! I tried making it when the user presses a key on the form, but it doesn't work. heres the code:

    VB Code:
    1. Private Sub frmCar_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    2.         If e.KeyChar = Chr(Keys.Left) Then
    3.             increasespeed()
    4.         End If
    5.     End Sub
    6.  
    7.     Public Sub increasespeed()
    8.         'oddly enough is just this:
    9.         ispeed = ispeed + 5
    10.     End Sub

    I'm guessing I've got the focus wrong or something like that. I tried it on a button(hence the stupid subroutine increasespeed); no difference. Anyone up for ideas...

    Pretty please???

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] moving a picturebox

    Perhaps if you used KeyDown instead of KeyPress?

    just a guess..
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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

    Re: [2005] moving a picturebox

    It's not really possible to do what you're asking. A forum search will turn up someone else who was trying to do the same thing and was unable to because of the way that controls react or not to the arrow keys. I'd say that the only way would be to use API calls.
    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,222

    Re: [2005] moving a picturebox

    Quote Originally Posted by gigemboy
    The Location.X and Location.Y properties are readonly, thus you cant set the values.
    That's actually not true. Location is a Point object and the X and Y properties of the Point type are not ReadOnly. The reason that it doesn't work is because Point is a value type. You cannot set properties of a value type member directly through a property because the property returns a temporary copy of the object, not a reference to the object itself. Setting properties of that temporary copy is pointless as it would have no effect on the original object and the copy is immediately discarded, so the compiler won't even let you try. You could do this, which also proves that X and Y are not ReadOnly:
    VB Code:
    1. Dim pt As Point = Me.pbCar.Location
    2.  
    3.         pt.X = 677
    4.         pt.Y = 562
    5.  
    6.         Me.pbCar.Location = pt
    This offers no advantage over creating a new Point object though, so you should do that. It comes down to the difference between reference types and value types, although it's not immediately obvious. Understand that difference and you understand this behaviour.
    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

  8. #8
    Lively Member
    Join Date
    Jun 2005
    Posts
    74

    Re: [2005] moving a picturebox

    I hadn't seen a solution so I'll post one. Just paste the code into a form's KeyDown event. (Of course, you should have a picture box control on the form!)

    VB Code:
    1. Dim picX As Integer = PictureBox1.Location.X
    2. Dim picY As Integer = PictureBox1.Location.Y
    3. If e.KeyValue = "37" Then
    4.             PictureBox1.Location = New Point(picX - 1, picY)
    5. ElseIf e.KeyValue = "39" Then
    6.             PictureBox1.Location = New Point(picX + 1, picY)
    7. End If

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

    Re: [2005] moving a picturebox

    Quote Originally Posted by AlphaScorpious
    I hadn't seen a solution so I'll post one. Just paste the code into a form's KeyDown event. (Of course, you should have a picture box control on the form!)

    VB Code:
    1. Dim picX As Integer = PictureBox1.Location.X
    2. Dim picY As Integer = PictureBox1.Location.Y
    3. If e.KeyValue = "37" Then
    4.             PictureBox1.Location = New Point(picX - 1, picY)
    5. ElseIf e.KeyValue = "39" Then
    6.             PictureBox1.Location = New Point(picX + 1, picY)
    7. End If
    That solution will only work if the form does not contain a single control that can accept focus. As soon as you add a TextBox, a Button or any other control whose CanFocus property returns True, the arrow keys take on specific functionality that will render that code useless. If your form has not controls that can accept focus then it's fine, otherwise no dice.

    Also, KeyValue is type Integer so you shouldn't be comparing it to a String. If you do use code like that you should either be using the numeric values 37 and 39 or, preferably use the KeyCode property instead and compare to Keys.Left and Keys.Right. That way your code is self documenting whereas 37 and 39 would require commenting or their meaning is not apparent.
    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

  10. #10
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [2005] moving a picturebox

    Quote Originally Posted by jmcilhinney
    That solution will only work if the form does not contain a single control that can accept focus. As soon as you add a TextBox, a Button or any other control whose CanFocus property returns True, the arrow keys take on specific functionality that will render that code useless. If your form has not controls that can accept focus then it's fine, otherwise no dice.
    Are you sure? If you set the form's Keypreview property to true, this will work:

    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.         If e.KeyCode = Keys.Left Then
    3.             Me.TextBox1.Location = New Point(TextBox1.Location.X - 1, TextBox1.Location.Y)
    4.         End If
    5.     End Sub

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

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

    Re: [2005] moving a picturebox

    Hmmm... thought I'd tested that more fully when the last person that I mentioned asked the question. It looks like it will work with certain combinations of controls and not others. If one or more Buttons are the only controls on the form that can accept focus then it will not work. If there are other controls that can receive focus then it will work under some, and maybe all, circumstances. If you have a Button and a PictureBox then you cannot move the PictureBox with the arrows. If you have a TextBox then you can, but if a Button has focus then the first arrow press will shift the focus to the TextBox and then subsequent arrow presses will move the PictureBox. Note that they will also move the caret within TextBox if the is text present. Doing both would probably be undesirable. It seems that the Button behaviour is a function of the ButtonBase class, so that means that RadioButtons and CheckBoxes will cause the same issue. The arrow keys are used a a means of navigation between ButtonBase objects.
    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

  12. #12
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [2005] moving a picturebox

    Figured it out

    You can override the ProcessCmdKey function, and still handle the arrow keys.
    VB Code:
    1. Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
    2.         If keyData = Keys.Left Then
    3.             Me.TextBox1.Location = New Point(TextBox1.Location.X - 1, TextBox1.Location.Y)
    4.         End If
    5.         Return MyBase.ProcessCmdKey(msg, keyData)
    6.     End Function

    It still processes the focus changing though, but if that's a concern, it can be fixed with other methods.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

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

    Re: [2005] moving a picturebox

    Nice one Bill.
    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

  14. #14

    Thread Starter
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Re: [2005] moving a picturebox

    Thanks to everyone who helped with this thread!

    And special thanks to Bill, who solved my problem!

    Everyones help was and is appreciated.

  15. #15
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: [Resolved] [2005] moving a picturebox

    but how do you move picture up and down without it automaticaly moveing to the top and corner????

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