|
-
Apr 3rd, 2008, 02:05 PM
#1
Thread Starter
Hyperactive Member
Move
Hey guys I was just wondering wat is wrong with this code
PictureBox2.Location.X = pbX
-
Apr 3rd, 2008, 02:10 PM
#2
Re: Move
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.
-
Apr 3rd, 2008, 02:13 PM
#3
Thread Starter
Hyperactive Member
Re: Move
How do I assign a left property.
-
Apr 3rd, 2008, 02:14 PM
#4
Re: Move
I'm sorry but have you even tried yourself?
-
Apr 3rd, 2008, 02:14 PM
#5
Thread Starter
Hyperactive Member
Re: Move
yes I have i cant figure it out
-
Apr 3rd, 2008, 02:16 PM
#6
-
Apr 3rd, 2008, 02:19 PM
#7
Thread Starter
Hyperactive Member
Re: Move
When I set a picturebox.right it says it is a read only what do i do to fix this.
-
Apr 3rd, 2008, 02:21 PM
#8
Re: Move
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.
-
Apr 3rd, 2008, 02:41 PM
#9
Thread Starter
Hyperactive Member
Re: Move
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
-
Apr 3rd, 2008, 02:45 PM
#10
Re: Move
please try this
PictureBox2.left += 100
did it move?
-
Apr 3rd, 2008, 02:50 PM
#11
Re: 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.
-
Apr 3rd, 2008, 02:52 PM
#12
Thread Starter
Hyperactive Member
Re: Move
It worked in my old pong game
-
Apr 3rd, 2008, 02:54 PM
#13
Re: Move
Wouldnt you be better of using the forms KeyDown event instead?
-
Apr 3rd, 2008, 02:54 PM
#14
Thread Starter
Hyperactive Member
-
Apr 3rd, 2008, 02:55 PM
#15
Re: Move
 Originally Posted by tarik666
It worked in my old pong game
What happened when you tried my suggestion?
-
Apr 3rd, 2008, 02:56 PM
#16
Re: Move
You are already handling the PictureBox' KeyPress event. So obviously you know how to handle an event. Where's the problem?
-
Apr 3rd, 2008, 03:03 PM
#17
Thread Starter
Hyperactive Member
Re: Move
I dont understand what I am meant to do with the keydown event
-
Apr 3rd, 2008, 03:12 PM
#18
Re: Move
The same thing that you are currently trying to do in the PictureBox' KeyPress event.
-
Apr 3rd, 2008, 03:13 PM
#19
Thread Starter
Hyperactive Member
Re: Move
so do i do like Private Sub PictureBox2_KeyDown or something I dont know
-
Apr 3rd, 2008, 07:30 PM
#20
Frenzied Member
Re: Move
Code:
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
Cheers
-
Apr 4th, 2008, 12:41 AM
#21
Thread Starter
Hyperactive Member
Re: Move
thanks Icyclur it works fine.
-
Apr 4th, 2008, 12:46 AM
#22
Frenzied Member
Re: Move
No problem at all [Removed by Mod]
Cheers
Last edited by RobDog888; Apr 4th, 2008 at 12:02 PM.
-
Apr 4th, 2008, 10:39 AM
#23
Thread Starter
Hyperactive Member
Re: Move
I cant their is no edit button
-
Apr 4th, 2008, 12:00 PM
#24
Re: Move
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
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
|