locked
PictureBox Positioning RRS feed

  • Question

  • I need some help with positioning a picture box. You see, I'm making an RTS with Vb .NET using windows forms. I know this seems like a REALLY bad idea but I'm going to try it anyway. But what I need help with is this:

     

    I want to be able to place a picture box with a certain radius of its previous location, as in

     

     

    /move here\           /move here\

     

       /move here\    PictureBox1 - is here    /move here\

                                 

    /move here\           /move here

     

     

     

    And I don't want any responses with no help but just saying "you should try a different way", because I DO know I should be doing it a different way BUT I'M NOT!

     

    So if you could please help me, that would be GREAT!

     

     

    ~Chris

    Saturday, September 15, 2007 7:32 PM

Answers

  • You should try a different way... Hehe, just kidding. If it helps you learn and you have fun with it: good for you!

    Basically, if you just want to move the Picturebox all you need to know is the current location and it's new location. If that is all you want for now, the code below perhaps helps a bit:

    Code Snippet

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MovePicture(PictureBox1, MoveDirection.Right)
        End Sub


        ''' <summary>
        ''' Represents the directions where the picturebox can move to.
        ''' </summary>
        ''' <remarks></remarks>
        Public Enum MoveDirection
            Down
            Left
            Right
            Up
        End Enum

        ''' <summary>
        ''' Moves a specific PictureBox, in a specific direction, with a specific increment/decrement value.
        ''' </summary>
        ''' <param name="Container">Name of the PictureBox container</param>
        ''' <param name="Direction">Direction to move to</param>
        ''' <remarks></remarks>
        Public Sub MovePicture(ByVal Container As PictureBox, ByVal Direction As MoveDirection)

            Dim Increment As Integer = 5
            Dim newLocation As Point = New Point(Container.Location)

            'Determine the new location, i.e. Point(), based on the direction to move to:
            Select Case Direction
                Case MoveDirection.Down
                    newLocation.Y += Increment
                Case MoveDirection.Left
                    newLocation.X -= Increment
                Case MoveDirection.Right
                    newLocation.X += Increment
                Case MoveDirection.Up
                    newLocation.Y -= Increment
            End Select

            'Move the PictureBox:
            Container.Location = newLocation

        End Sub



    This allows you to move a specified PictureBox to one of four defined locations with one line of code. Ofcourse you can expand this example to as many axis changes you want.

    It's very basic, bet let us know if this helps...


    Sunday, September 16, 2007 11:43 AM
  • it does help but after I had posted this, I was considering an easier way to move the sprites around rather then a left or right button but rather, clicking on the picture and being able to move it in a radius.

     

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    'Select the unit you want to move.

    PictureBox1.Select()

    PictureBox1Selected = True

    unitpan.Visible = True

    End Sub

     

     

    Private Sub testmap_Klik(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

    If PictureBox1Selected = True Then

    'Put unit where the cursor is.

    Dim LocalMousePosition As Point

    LocalMousePosition = MyBase.PointToClient(Windows.Forms.Cursor.Position)

    PictureBox1.Location = LocalMousePosition

    End If

    End Sub

     

     

    Private Sub testmap_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel

    If PictureBox1Selected = True Then

    'When yo scroll, it deselects the unit.

    PictureBox1Selected = False

    unitpan.Visible = False

    End If

    End Sub

     

     

     

    So when you click on the picturebox, rather then moving it with buttons, you have to place it within a certain radius.

     

    ~Chris

     

     

     

    EDIT: "/bump!"

    "/bump again!"

    Sunday, September 16, 2007 12:28 PM

All replies

  • You should try a different way... Hehe, just kidding. If it helps you learn and you have fun with it: good for you!

    Basically, if you just want to move the Picturebox all you need to know is the current location and it's new location. If that is all you want for now, the code below perhaps helps a bit:

    Code Snippet

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MovePicture(PictureBox1, MoveDirection.Right)
        End Sub


        ''' <summary>
        ''' Represents the directions where the picturebox can move to.
        ''' </summary>
        ''' <remarks></remarks>
        Public Enum MoveDirection
            Down
            Left
            Right
            Up
        End Enum

        ''' <summary>
        ''' Moves a specific PictureBox, in a specific direction, with a specific increment/decrement value.
        ''' </summary>
        ''' <param name="Container">Name of the PictureBox container</param>
        ''' <param name="Direction">Direction to move to</param>
        ''' <remarks></remarks>
        Public Sub MovePicture(ByVal Container As PictureBox, ByVal Direction As MoveDirection)

            Dim Increment As Integer = 5
            Dim newLocation As Point = New Point(Container.Location)

            'Determine the new location, i.e. Point(), based on the direction to move to:
            Select Case Direction
                Case MoveDirection.Down
                    newLocation.Y += Increment
                Case MoveDirection.Left
                    newLocation.X -= Increment
                Case MoveDirection.Right
                    newLocation.X += Increment
                Case MoveDirection.Up
                    newLocation.Y -= Increment
            End Select

            'Move the PictureBox:
            Container.Location = newLocation

        End Sub



    This allows you to move a specified PictureBox to one of four defined locations with one line of code. Ofcourse you can expand this example to as many axis changes you want.

    It's very basic, bet let us know if this helps...


    Sunday, September 16, 2007 11:43 AM
  • it does help but after I had posted this, I was considering an easier way to move the sprites around rather then a left or right button but rather, clicking on the picture and being able to move it in a radius.

     

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    'Select the unit you want to move.

    PictureBox1.Select()

    PictureBox1Selected = True

    unitpan.Visible = True

    End Sub

     

     

    Private Sub testmap_Klik(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

    If PictureBox1Selected = True Then

    'Put unit where the cursor is.

    Dim LocalMousePosition As Point

    LocalMousePosition = MyBase.PointToClient(Windows.Forms.Cursor.Position)

    PictureBox1.Location = LocalMousePosition

    End If

    End Sub

     

     

    Private Sub testmap_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel

    If PictureBox1Selected = True Then

    'When yo scroll, it deselects the unit.

    PictureBox1Selected = False

    unitpan.Visible = False

    End If

    End Sub

     

     

     

    So when you click on the picturebox, rather then moving it with buttons, you have to place it within a certain radius.

     

    ~Chris

     

     

     

    EDIT: "/bump!"

    "/bump again!"

    Sunday, September 16, 2007 12:28 PM