Code Snippets

  

Visual Basic Source Code


Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 117,281 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 2,109 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




How to move and Picture box with ASC and Arrow keys

This allows you to move and image box either by ascii or the arrow keys.

Submitted By: sam_benne
Actions:
Rating:
Views: 665

Language: Visual Basic

Last Modified: May 7, 2008
Instructions: Create and picture box (pic1) on a standard exe.

Snippet


  1. Private Sub Form_Load()
  2.     KeyPreview = True
  3.     MsgBox ("Use the 2, 4, 6, 8 keys or the arrow keys to move the picture box")
  4. End Sub
  5.  
  6. Private Sub Form_KeyPress(KeyAscii As Integer) ' to handle the number keys
  7. Form1.Caption = "You pressed key: " & KeyAscii
  8. 'W = 119, D = 100, A = 97, S = 119
  9.    
  10.     Select Case KeyAscii
  11.         Case 115 ' down
  12.             pic1.Top = pic1.Top + 100
  13.         Case 97 ' left
  14.             pic1.Left = pic1.Left - 100
  15.         Case 100 ' Right.
  16.             pic1.Left = pic1.Left + 100
  17.         Case 119 ' Up
  18.             pic1.Top = pic1.Top - 100
  19.     End Select
  20.  
  21. End Sub
  22.  
  23. Private Sub Pic1_KeyDown(KeyCode As Integer, Shift As Integer)
  24.     ' to handle the non ASCII keys, arrow keys, use the KeyDown event
  25.     Select Case KeyCode
  26.     Case vbKeyDown
  27.             pic1.Top = pic1.Top + 100
  28.     Case vbKeyLeft
  29.             pic1.Left = pic1.Left - 100
  30.     Case vbKeyRight
  31.             pic1.Left = pic1.Left + 100
  32.     Case vbKeyUp
  33.             pic1.Top = pic1.Top - 100
  34.     End Select
  35.    
  36. End Sub

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month