4 Replies - 3635 Views - Last Post: 15 March 2011 - 10:51 PM Rate Topic: -----

#1 Madamayo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 10-February 09

System.Drawing.Point Non-existent in VB 2010?

Posted 15 March 2011 - 09:21 PM

Im getting an error right now for this code, and I'm wondering if it's me, or VB2010 has changed?

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Left Then

            If Player.Location.X >= 10 Then

                Player.Location = New System.Drawing.Point (Player.Location.X += 5, Player.Location.Y)

            End If

        Else

        End If

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: System.Drawing.Point Non-existent in VB 2010?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4300
  • View blog
  • Posts: 12,064
  • Joined: 18-April 07

Re: System.Drawing.Point Non-existent in VB 2010?

Posted 15 March 2011 - 10:10 PM

You can't do Player.Location.X += 5 in the constructor of Point. Just like you can't do

Dim i as Integer = Player.Location.X += 5



Now you can do...

Player.Location = New System.Drawing.Point(Player.Location.X + 5, Player.Location.Y)



Which makes more sense anyways right? You are modifying Player's location so take its current X value, add 5 and use it to set the location to a new point based of this modified x value.

Or better yet...

Player.Location.X += 5



Right? We are saying adjust the X value by adding 5 to it. No need to create a new point with a modified X just to reload it back into the location. Modify the location point itself.

:)
Was This Post Helpful? 1
  • +
  • -

#3 Madamayo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 10-February 09

Re: System.Drawing.Point Non-existent in VB 2010?

Posted 15 March 2011 - 10:25 PM

Hahaha, so it was just the extra = that was throwing it off....

Thank you very much, that was incredibly helpful.
Was This Post Helpful? 0
  • +
  • -

#4 Madamayo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 10-February 09

Re: System.Drawing.Point Non-existent in VB 2010?

Posted 15 March 2011 - 10:32 PM

Okay... So I'm trying to get my Player (a picturebox) to move left or right depending on which key is pressed. I'm currently using If/ElseIf loops to do it, and they are not working. I've been considering using select/case to do this, but am wondering if i am even capturing the keyinput anyways? Would I need to load the following code into the main form when it loads? I can't seem to find my mistake. (It's been a while since I've programmed and I'm trying to get back into it, please bear with me)


    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Left Then

            If Player.Location.X >= 10 Then

                Player.Location = New System.Drawing.Point(Player.Location.X + 2, Player.Location.Y)

            End If

        ElseIf e.KeyCode = Keys.Right Then

            If Player.Location.X <= 570 Then

                Player.Location = New System.Drawing.Point(Player.Location.X - 2, Player.Location.Y)

            End If

        End If

    End Sub



I'm thinking it is my private sub that is the error. (Compiles and runs, just doesn't function like I want it to)

This post has been edited by Madamayo: 15 March 2011 - 10:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 Madamayo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 10-February 09

Re: System.Drawing.Point Non-existent in VB 2010?

Posted 15 March 2011 - 10:51 PM

Problem solved. Needed to set KeyPreview to True on my form.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1