Hey guys I was just wondering wat is wrong with this code
PictureBox2.Location.X = pbX
Printable View
Hey guys I was just wondering wat is wrong with this code
PictureBox2.Location.X = pbX
You can not set the X coordinate that way, you have to set the Left property or assign a new Point structure to the Location property.
The reason why that code is not working is because Location returns a Point structure, and since structures are value-types (the returned Point structure doesnt really have anything to do with the PictureBox' location), you can not assign anything to it. Even if you could, it would be pointless.
How do I assign a left property.
I'm sorry but have you even tried yourself?
yes I have i cant figure it out
vb Code:
PictureBox2.Left = 5
When I set a picturebox.right it says it is a read only what do i do to fix this.
Where did you see me say anything about the Right property? If I havent been drugged the last 5 minutes, I believe I mentioned the Left property.
The Right property is a Read-Only property (as you could've easily found out by looking at Intellisense or MSDN) and simply returns the X coordinate for the controls right edge. If you want to change that coordinate, you need to change the Left and/or the Width property.
Alright I have fixed that problem. I just looked in some of my older code when i had created a game called pong now here is my code.
Public Class Form1
Dim pbX As Integer
Dim pby As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim objpoint As New System.Drawing.Point(PictureBox2.Location.X, PictureBox2.Location.Y)
PictureBox2.Location = objpoint
End Sub
Private Sub PictureBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles PictureBox2.KeyPress
If e.KeyChar = "a" Or e.keychar = "A" Then
Dim objpoint As New System.Drawing.Point(PictureBox2.Location.X - 14, PictureBox2.Location.Y)
PictureBox2.Location = objpoint
End If
End Sub
End Class
The only problem is the picture box isnt moving
please try this
PictureBox2.left += 100
did it move?
Here's what MSDN has to say about the PictureBox.KeyPress event:
This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
It worked in my old pong game
Wouldnt you be better of using the forms KeyDown event instead?
how do i
What happened when you tried my suggestion?:wave:Quote:
Originally Posted by tarik666
You are already handling the PictureBox' KeyPress event. So obviously you know how to handle an event. Where's the problem?
I dont understand what I am meant to do with the keydown event
The same thing that you are currently trying to do in the PictureBox' KeyPress event.
so do i do like Private Sub PictureBox2_KeyDown or something I dont know
CheersCode:Public Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Left Then
MyPictureBox.Location = New Point(MyPictureBox.Location.X-1, MyPictureBox.Location.Y)
If MyPictureBox.Location.X < 0 Then
MyPictureBox.Location = New Point(0, MyPictureBox.Location.Y)
End If
ElseIf e.KeyCode = Keys.Right Then
MyPictureBox.Location = New Point(MyPictureBox.Location.X+1, MyPictureBox.Location.Y)
If MyPictureBox.Location.X+MyPictureBox.Width > Form1.Width Then
MyPictureBox.Location = New Point(0, MyPictureBox.Location.Y)
End If
End If
End Sub
thanks Icyclur it works fine.
No problem at all [Removed by Mod]
Cheers
I cant their is no edit button
Asking someone to read a signature which says to give reps is against the AUP ;) Please edit it or just remove it altogether.
If someone wants to give you reps, then its up to them to decide to or not to or even give a negative.