Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to know how to make different pic appear each time u press a key

Status
Not open for further replies.

Oolong

Programmer
Apr 10, 2002
13
0
0
US
I need to know how to make a different image appear every time you press a key, like an arrow key or a space bar. If someone can post the code, that would be very helpful to me. I already know how to make one image appear when i press a certain key, but i want to know how to make another image appear if i press it again, and then another appear if I press it again.
 
just an example...
you could also use an ImageList control or a PictureClip control or you could use whatever you wanted

Code:
Option Explicit

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  ' assuming zero-based array of pictures
  Const ITEMCOUNT = 4& ' set this to the number of pictures
  Static i As Long
  
  If KeyCode = vbKeyLeft Then
    i = (ITEMCOUNT + (i - 1)) Mod ITEMCOUNT
  ElseIf KeyCode = vbKeyRight Then
    i = (i + 1) Mod ITEMCOUNT
  End If
  
  ' in this example I have an array of Image controls (indices 0, 1, 2, 3)
  Picture = Image1(i).Picture ' set form's picture property
End Sub
 
I get a runtime error of 13. It says type mismatch. I may have done something wrong though. I set itemcount as 3&, because i have 3 pics. I took out the else if vbkeyright, because i just want it for vbkeyleft and nothing else. Then for the Picture = image1(i).picture, i put in the name of the image in the "image1" spot. How does this code recognize the other pictures that i want to use and what order to put them in?
 
In my example, I had a control array of image controls

Image1(0)
Image1(1)
Image1(2)
etc.

I set each Image control's picture property to a different picture in design time

or you could set each at run time in the Form_Load() event
ex. Image1(0).Picture=LoadPicture("C:\windows\bubbles.bmp")

 
Specify an Image Folder in the code and each time you press a key it will load a new image in a picturebox.
This way you can add or remove files for display even
after the program is compiled.

'-------------------------------------------------------
'Throw a Picturebox on Form
'Name PictureBox = Picture1
'Check Microsoft Scripting Runtime under Project References Menu
'Paste code into module and try

Dim FSO As New FileSystemObject
Dim PicArray()
Dim X As Integer

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
NextPic
End Sub

Private Sub NextPic()
On Error GoTo ErrorHandler
If X > UBound(PicArray) Then X = 0
Picture1.Picture = LoadPicture(PicArray(X))
X = X + 1
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Is = 481
If X + 1 > UBound(PicArray) Then
X = 0
Else
X = X + 1
End If
Resume
Case Else
MsgBox Err.Number & Err.Description
Resume Next
End Select
End Sub

Private Sub Form_Load()
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim strPath As String
Dim fo 'Folder Object
Dim fc 'Folder Collection
Dim F_item 'Dual Purpose - File and then SubFolder
Dim fls 'File Collection
Dim i As Integer
X = 0
i = 0
'Set Path to a Folder where you store your image files
strPath = "C:\Documents and Settings\Kevin Clark\My Documents\My Pictures\"
Set fo = FSO.GetFolder(strPath)
Set fc = fo.SubFolders
Set fls = fo.Files
For Each F_item In fls
If F_item.Type Like "*Image" Then
ReDim Preserve PicArray(i)
PicArray(i) = F_item
i = i + 1
End If
Next F_item
Me.KeyPreview = True
NextPic
End Sub
'-------------------------------------------------------
 
Still doesnt seem to work. Here is a rundown of what i have.
I have 3 images on my form. I made a control array on each of them. They are all named image1. I set image1(2) to be in the same position as image1(0). I did the same with image1(1). Do i have to declare one of the images as visible = true. Tell me what im doing wrong please.

Option Explicit

Private Sub Form_Load()
image1(2).Left = image1(0).Left
image1(1).Left = image1(0).Left

End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Const ITEMCOUNT = 3&
Static i As Long

If KeyCode = vbKeyLeft Then
i = (ITEMCOUNT + (i - 1)) Mod ITEMCOUNT
End If

Picture = image1(i).Picture
End Sub

 
anyone have any other better and short answers, or can help fix my code.
 
What exactly is the problem ? Are you getting an error, is it just not displaying images or not recognizing keydown ?
 
It just seems to work once. It displays one of the pictures, and then it doesnt display the next one when i hit it again.
 
could it be that all your image controls have the save value in their .Picture properties
 
Look at the form properties and make sure you have the KeyPreview set to true


or add this line

Me.KeyPreview = true


Then change your
Picture = image1(i).Picture

to

Image1(0).Picture = Image1(i).Picture
or
Me.Picture = Image1(i).picture
 
Get a Image Control over your form and then try the following



Dim c, ans, pos
Dim valid As Boolean


Private Sub Form_Activate()
On Error Resume Next
CommonDialog1.ShowOpen
If CommonDialog1.FileName = "" Then

Unload Me
Exit Sub

End If
Image1.Width = Me.Width
Image1.Height = Me.Height
If Not Err.Number = 481 Then
Image1.Picture = LoadPicture(CommonDialog1.FileName)
Label1.Caption = Dir(CommonDialog1.FileName)
Else
valid = False
End If
If Not Left(CommonDialog1.FileName, 1) = "\" Then
ans = CurDir(CommonDialog1.FileName)
Else
ans = Dir(CommonDialog1.FileName)
ans = Mid(CommonDialog1.FileName, 1, Len(CommonDialog1.FileName) - Len(ans))
End If


ShowFileList (ans)

End Sub



Sub ShowFileList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 In fc
If LCase(Right(f1.name, 3)) = "jpg" Or LCase(Right(f1.name, 3)) = "bmp" Or LCase(Right(f1.name, 3)) = "gif" Or LCase(Right(f1.name, 3)) = "wmf" Then
List1.AddItem (f1.name)
End If
Next
For i = 0 To List1.ListCount - 1
If List1.List(i) = Dir(CommonDialog1.FileName) Then
c = i + 1
Exit For
End If
Next
If valid = False Then
Image1_Click
valid = True
End If
End Sub



Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error Resume Next
If KeyCode = vbKeySpace Or KeyCode = vbKeyRight Then
c = c + 1
If c >= List1.ListCount Then
c = 0
End If
If Not Err.Number = 481 Then
Image1.Picture = LoadPicture(ans & "\" & List1.List(c))
Label1.Caption = List1.List(c)
Else
Image1_Click
End If
ElseIf KeyCode = vbKeyLeft Then
c = c - 1
If c <= 0 Then
c = List1.ListCount - 1
End If
If Not Err.Number = 481 Then
Image1.Picture = LoadPicture(ans & &quot;\&quot; & List1.List(c))
Label1.Caption = List1.List(c)
Else
Image1_Click
End If
ElseIf KeyCode = vbKeyEscape Then
Unload Me
End If

End Sub



Private Sub Image1_Click()
On Error Resume Next

c = c + 1
If c >= List1.ListCount Then
c = 0
End If
If Not Err.Number = 481 Then
Image1.Picture = LoadPicture(ans & &quot;\&quot; & List1.List(c))
Label1.Caption = List1.List(c)
Else
Image1_Click
End If

End Sub



satya
 
Sent him something like that already Silver but dosen't seem to want to use it (too long)
Would be dynamic though and keep the end program small.
 
Its not that it is too long, i just dont understand it. Im still in my early stages of programming in vb, and i am making a game. I want my character to be able to walk. So i thought that if i drew three pics, (one of him standing, one of him stepping left and one of him stepping right) then all i needed would be some code that made a pic show up after i press up, so it would look like he is walking. Thanx for helping me. I will have to ask my teacher on Monday since I have spring break right now.
 
Instead of using 3 image controls you could use one and load the pictures into a collection, or into an array defined as a StdPicture (dim myPicture(3) as StdPicture; Set myPicture(1) = XX; Set Image1.Picture = myPicture(1)), and/or use a resource file if you do not want to have the pictures stored in a normal file.

Using a variable defined as StdPicture (uses Set) is an easy way of assigning many objects the same picture which are stored in memory.
 
No problem Oolong. Besidea if all you need is to display the three images as you described then the other methods could be an overkill anyway.

KeyPreview works when Form has the focus. Try Modifying your program to use something other then the Left key. I think it may be that when you press the left key button that you shift the focus from your form to another control in your form.
 
Ignore my last comment regarding Form and Focus. It has nothing to do with it (Must be late - brain wandering again). Anyway try this and see if it works and then maybe add your leftbutton condition if it does.

On your form:
Insert 1 image control (use default name image1)
Insert 3 image controls (image1(Indexed 0 to 2)
Try this code to see if it works




Dim i As Integer

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Image1.Picture = image2(i).Picture
i = i + 1
If i > 2 Then i = 0
End Sub

Private Sub Form_Load()
i = 0
Me.KeyPreview = true
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top