Position Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to code an application that will move a image around the screen
depending on what arrow keys the user presses. If the user presses the up
key, the graphic will move up a few positions, and so on. I was wondering
how I would move the image though, it seems that when I try to change the X
and Y values in the position property, nothing happenes. I must be changing
the wrong property, but I don't know where to find the one that needs to
change. Thanks
 
Jay said:
I am trying to code an application that will move a image around the screen
depending on what arrow keys the user presses. If the user presses the up
key, the graphic will move up a few positions, and so on. I was wondering
how I would move the image though, it seems that when I try to change the
X
and Y values in the position property, nothing happenes. I must be
changing
the wrong property, but I don't know where to find the one that needs to
change.

If you want to move a form around, check its 'Left', 'Top', and 'Location'
properties.
 
I wasn't really looking to move the form though, I just wanted to move the
image that is located on the form, not the entire form itself. Thanks
 
Jay said:
I wasn't really looking to move the form though, I just wanted to move the
image that is located on the form, not the entire form itself.

Even controls have these properties.
 
Jay,
Image.Position is a structure you need to change the entire structure.

Something like:

Protected Overrides Function IsInputKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Left, Keys.Right, Keys.Up, Keys.Down
Return False
Case Else
Return MyBase.IsInputKey(keyData)
End Select
End Function

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean
Dim offset As Point
Select Case keyData
Case Keys.Left
offset = New Point(-1, 0)
Case Keys.Right
offset = New Point(1, 0)
Case Keys.Up
offset = New Point(0, -1)
Case Keys.Down
offset = New Point(0, 1)
Case Else
Return MyBase.ProcessDialogKey(keyData)
End Select
Me.PictureBox1.Location = New Point(Me.PictureBox1.Location.X +
offset.X, Me.PictureBox1.Location.Y + offset.Y)
End Function

The above was tested in a Form with KeyPreview=True. I would also expect the
above to work within a custom control itself.

NOTE: Left, Right, Up, & Down are normally not input keys, so the IsInputKey
override is not specifically needed...

Hope this helps
Jay

|I am trying to code an application that will move a image around the screen
| depending on what arrow keys the user presses. If the user presses the up
| key, the graphic will move up a few positions, and so on. I was wondering
| how I would move the image though, it seems that when I try to change the
X
| and Y values in the position property, nothing happenes. I must be
changing
| the wrong property, but I don't know where to find the one that needs to
| change. Thanks
 
Doh!
ProcessDialogKey should return a True if it processed the key.

| Protected Overrides Function ProcessDialogKey(ByVal keyData As
| System.Windows.Forms.Keys) As Boolean
| Dim offset As Point
| Select Case keyData
| Case Keys.Left
| offset = New Point(-1, 0)
| Case Keys.Right
| offset = New Point(1, 0)
| Case Keys.Up
| offset = New Point(0, -1)
| Case Keys.Down
| offset = New Point(0, 1)
| Case Else
| Return MyBase.ProcessDialogKey(keyData)
| End Select
| Me.PictureBox1.Location = New Point(Me.PictureBox1.Location.X +
| offset.X, Me.PictureBox1.Location.Y + offset.Y)

Return True

| End Function

Jay

| Jay,
| Image.Position is a structure you need to change the entire structure.
|
| Something like:
|
| Protected Overrides Function IsInputKey(ByVal keyData As
| System.Windows.Forms.Keys) As Boolean
| Select Case keyData
| Case Keys.Left, Keys.Right, Keys.Up, Keys.Down
| Return False
| Case Else
| Return MyBase.IsInputKey(keyData)
| End Select
| End Function
|
| Protected Overrides Function ProcessDialogKey(ByVal keyData As
| System.Windows.Forms.Keys) As Boolean
| Dim offset As Point
| Select Case keyData
| Case Keys.Left
| offset = New Point(-1, 0)
| Case Keys.Right
| offset = New Point(1, 0)
| Case Keys.Up
| offset = New Point(0, -1)
| Case Keys.Down
| offset = New Point(0, 1)
| Case Else
| Return MyBase.ProcessDialogKey(keyData)
| End Select
| Me.PictureBox1.Location = New Point(Me.PictureBox1.Location.X +
| offset.X, Me.PictureBox1.Location.Y + offset.Y)
| End Function
|
| The above was tested in a Form with KeyPreview=True. I would also expect
the
| above to work within a custom control itself.
|
| NOTE: Left, Right, Up, & Down are normally not input keys, so the
IsInputKey
| override is not specifically needed...
|
| Hope this helps
| Jay
|
| ||I am trying to code an application that will move a image around the
screen
|| depending on what arrow keys the user presses. If the user presses the
up
|| key, the graphic will move up a few positions, and so on. I was
wondering
|| how I would move the image though, it seems that when I try to change the
| X
|| and Y values in the position property, nothing happenes. I must be
| changing
|| the wrong property, but I don't know where to find the one that needs to
|| change. Thanks
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top