View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Dean[_2_] Dean[_2_] is offline
external usenet poster
 
Posts: 16
Default use of arrow keys in Excel

On Jan 12, 8:28*pm, "Rick Rothstein"
wrote:
It just occurred to me that, using the code I provided, it would be possible
to move the image out of view (off, way off, the edges of the UserForm).
Here is a modified subroutine that will stop the movement when the Image
control reaches the edges of the UserForm...

Sub MoveImage1(ByVal Direction As Long)
* Const MoveAmount As Long = 2
* With Image1
* * Select Case Direction
* * * Case vbKeyLeft
* * * * If .Left MoveAmount Then .Left = .Left - MoveAmount
* * * Case vbKeyRight
* * * * If .Left < Me.InsideWidth - .Width - MoveAmount _
* * * * * * * * * * * * Then .Left = .Left + MoveAmount
* * * Case vbKeyUp
* * * * If .Top MoveAmount Then .Top = .Top - MoveAmount
* * * Case vbKeyDown
* * * * If .Top < Me.InsideHeight - .Height - MoveAmount _
* * * * * * * * * * * * * *Then .Top = .Top + MoveAmount
* * End Select
* End With
End Sub

--
Rick (MVP - Excel)

"Rick Rothstein" wrote in message

...



But that code is for a CommandButton... you said you wanted to move an
Image control around. The reason I said I think it may be hard to do that
is because the Image control cannot take focus and it does not have a
KeyDown (nor a KeyUp or KeyPress) event. You might be able to put code in
every other control's KeyDown event procedure if you don't have too many
other controls on the UserForm and handle the problem that way. If you
want to do it that way, copy/paste this subroutine into the UserForm's
code window...


Sub MoveImage1(ByVal Direction As Long)
*Const MoveAmount As Long = 2
*Select Case Direction
* *Case vbKeyLeft
* * *Image1.Left = Image1.Left - MoveAmount
* *Case vbKeyRight
* * *Image1.Left = Image1.Left + MoveAmount
* *Case vbKeyUp
* * *Image1.Top = Image1.Top - MoveAmount
* *Case vbKeyDown
* * *Image1.Top = Image1.Top + MoveAmount
*End Select
End Sub


and, for **every** control on the UserForm that has a KeyDown event
procedure available, put this single line in that KeyDown event...


MoveImage1 KeyCode


--
Rick (MVP - Excel)


"Dean" wrote in message
....
On Jan 12, 1:17 pm, Dean wrote:
Hey thanks alot, even though that wasn't quite what I was looking for,
I may use it on later projects,


hello, yea I found it out from an old example I got from the Internet,
the code it
Private Sub CommandButton1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 1 Then
MsgBox hello world
End If


End Sub


but the only problem is I don't know what key cone is which, I think
the left arrow is 37, but how could I find these no's out???- Hide quoted text -


- Show quoted text -


Yea thanks for your reply, I knew you could use the call function for
Commandbutton_keypress, but didn't mention it,
I found out the key code's from a MSDN form, but can't rember the
link,
Sorry for complicating thing with using the command button, and not
using the command button,
Thanks for all you replies