|
-
Apr 1st, 2006, 09:06 AM
#1
Thread Starter
Fanatic Member
[Resolved] [2005] moving a picturebox
I want to change the value of the place where the picturebox goes, so i put:
VB Code:
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
'677,562
pbCar.Location.X(677)
pbCar.Location.Y(562)
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
-
Apr 1st, 2006, 09:09 AM
#2
Re: [2005] moving a picturebox
shouldnt it be:
VB Code:
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
'677,562
pbCar.Location.X = 677
pbCar.Location.Y = 562
End Sub
-
Apr 1st, 2006, 02:30 PM
#3
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:
PictureBox1.Location = New Point(50, 50)
-
Apr 2nd, 2006, 01:35 PM
#4
Thread Starter
Fanatic Member
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:
Private Sub frmCar_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Chr(Keys.Left) Then
increasespeed()
End If
End Sub
Public Sub increasespeed()
'oddly enough is just this:
ispeed = ispeed + 5
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???
-
Apr 2nd, 2006, 02:08 PM
#5
Re: [2005] moving a picturebox
Perhaps if you used KeyDown instead of KeyPress?
just a guess..
-
Apr 2nd, 2006, 09:26 PM
#6
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.
-
Apr 2nd, 2006, 09:34 PM
#7
Re: [2005] moving a picturebox
 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:
Dim pt As Point = Me.pbCar.Location
pt.X = 677
pt.Y = 562
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.
-
Apr 2nd, 2006, 11:44 PM
#8
Lively Member
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:
Dim picX As Integer = PictureBox1.Location.X
Dim picY As Integer = PictureBox1.Location.Y
If e.KeyValue = "37" Then
PictureBox1.Location = New Point(picX - 1, picY)
ElseIf e.KeyValue = "39" Then
PictureBox1.Location = New Point(picX + 1, picY)
End If
-
Apr 2nd, 2006, 11:55 PM
#9
Re: [2005] moving a picturebox
 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:
Dim picX As Integer = PictureBox1.Location.X
Dim picY As Integer = PictureBox1.Location.Y
If e.KeyValue = "37" Then
PictureBox1.Location = New Point(picX - 1, picY)
ElseIf e.KeyValue = "39" Then
PictureBox1.Location = New Point(picX + 1, picY)
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.
-
Apr 3rd, 2006, 12:29 AM
#10
Re: [2005] moving a picturebox
 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:
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Left Then
Me.TextBox1.Location = New Point(TextBox1.Location.X - 1, TextBox1.Location.Y)
End If
End Sub
Bill
-
Apr 3rd, 2006, 12:52 AM
#11
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.
-
Apr 3rd, 2006, 01:20 AM
#12
Re: [2005] moving a picturebox
Figured it out
You can override the ProcessCmdKey function, and still handle the arrow keys.
VB Code:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Left Then
Me.TextBox1.Location = New Point(TextBox1.Location.X - 1, TextBox1.Location.Y)
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
It still processes the focus changing though, but if that's a concern, it can be fixed with other methods.
Bill
-
Apr 3rd, 2006, 01:28 AM
#13
Re: [2005] moving a picturebox
Nice one Bill.
-
Apr 3rd, 2006, 03:07 PM
#14
Thread Starter
Fanatic Member
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.
-
Apr 13th, 2006, 03:54 PM
#15
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|