-
May 4th, 2010, 12:53 PM
#1
Animation with 5 picture boxes using VB.NET
Hello!
I'm trying to create an animation with 5 picture boxes (labeled picturebox0 - picturebox4) but I have a specific order I want them to display. I made 5 picture boxes, added images to them, and stack them on top of each other.
I want the order to show this:
picturebox 0, 1, 4, 3, 2, 4, 2, 3, 1, 3... etc
The code I have using if-else statements which would work if I wanted to repeat the same display pattern but I need to manually right the pattern... How would I do that? I have at timer set to change the pictures every 2.5 seconds.
Code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
System.Threading.Thread.Sleep(1000)
If PictureBox0.Visible = True Then
PictureBox0.Visible = False
PictureBox1.Visible = True
ElseIf PictureBox1.Visible = True Then 'up arrow displayed
PictureBox1.Visible = False
PictureBox4.Visible = True
ElseIf PictureBox4.Visible = True Then 'left arrow displayed
PictureBox4.Visible = False
PictureBox3.Visible = True
ElseIf PictureBox3.Visible = True Then 'down arrow displayed
PictureBox3.Visible = False
PictureBox2.Visible = True
ElseIf PictureBox2.Visible = True Then 'right arrow displayed
PictureBox2.Visible = False
PictureBox4.Visible = True
ElseIf PictureBox4.Visible = True Then 'left
PictureBox4.Visible = False
PictureBox2.Visible = True
ElseIf PictureBox2.Visible = True Then 'right
PictureBox2.Visible = False
PictureBox3.Visible = True
ElseIf PictureBox3.Visible = True Then 'down
PictureBox3.Visible = False
PictureBox2.Visible = True
ElseIf PictureBox1.Visible = True Then 'up
PictureBox1.Visible = False
PictureBox3.Visible = True
ElseIf PictureBox3.Visible = True Then 'down
PictureBox3.Visible = False
PictureBox2.Visible = True
ElseIf PictureBox2.Visible = True Then 'right
PictureBox2.Visible = False
PictureBox4.Visible = True
ElseIf PictureBox4.Visible = True Then '
PictureBox1.Visible = False
PictureBox2.Visible = True
End If
End Sub
-
May 4th, 2010, 02:37 PM
#2
Re: Animation with 5 picture boxes using VB.NET
Sleep() inside of a timer event? Bad idea all around. rethink your idea, and then ask a logical question.
what order do you want? random?
-
May 4th, 2010, 02:46 PM
#3
Re: Animation with 5 picture boxes using VB.NET
Thanks for the fast response dglienna! I took the sleep outside of the timer event!
I would like the order to be the way I listed it in the first post. I have a cue list (a list of time stamps made in a text file) that I would like to follow. I have 5 picture boxes that are stacked on top of each other only one picture box visible at one time.
How would I be able to create a slideshow of pictures in the order I want?
-
May 4th, 2010, 03:21 PM
#4
Re: Animation with 5 picture boxes using VB.NET
I would use a file to hold the configuration of the sequence to be ran then process that file in a select case to light the next one. This would make the code short and sweet and would allow you to have as many different patterns as you wish by simply loading different files.
For example you could have all your items for a given sequence on one line of a file like you show in your example above seperated by commas. read that line into a string and then break that string into an array using the split method. The array would be defined at the top of the form so as to be available to all routines within the form. You would also want an Integer var to hold the current array index.
In your timer code you would want something like.
Code:
PictureBox1.Visible=false
PictureBox2.Visible=false
PictureBox3.Visible=false
PictureBox4.Visible=false
PictureBox5.Visible=false
Select case SequenceArrary(ArrayIndex)
Case 1
PictureBox1.Visible=true
Case 2
PictureBox2.Visible=true
Case 3
Case 4
Case 5
End Select
ArrayIndex += 1
Always use [code][/code] tags when posting code.
-
May 4th, 2010, 03:24 PM
#5
Re: Animation with 5 picture boxes using VB.NET
Of course you would also need to check to be sure you have not exceeded the number of items in the array. This would mean the sequence has ran its course and the user is done. Additional processing would result in an error as there are no more items in the list to process.
Always use [code][/code] tags when posting code.
-
May 4th, 2010, 03:43 PM
#6
Re: Animation with 5 picture boxes using VB.NET
Thanks DataMiser for your help! By file do you mean to write the sequence in a text file? Also how would the sequence be written... by writing PictureBox1.Visible= true, PictureBox2.Visible=true, PictureBox3.Visible = true... etc? Sorry i'm not very knowledge in VB.NET but I truly appreciate your help!
-
May 4th, 2010, 04:08 PM
#7
Re: Animation with 5 picture boxes using VB.NET
Just a text file containing the number of the picture to view in sequence
1,2,3,2,4,5,1,2,3,4,1,2,4,1
Always use [code][/code] tags when posting code.
-
May 4th, 2010, 04:08 PM
#8
Re: Animation with 5 picture boxes using VB.NET
Sorry I didn't get to see your updated post! So I would just write case 1, case 2, case 3, case 1, case 4, etc.. in an array?
-
May 4th, 2010, 04:45 PM
#9
Re: Animation with 5 picture boxes using VB.NET
Just the numbers, Like you showed before but without the spaces.
For example if your file contains
1,2,3,4,3,2,1,5,3,2,4,1
The timer would fire case 1 the first time the timer cycles, then case 2 the next time then 3 then 4 then 3 then 2 then 1 then 5 and so on
The case statement is code that tells the program to do certain things in the case where this is true. It is basically the same as a bunch of if else statements.
Code:
Select Case X
Case 1
case 2
case 3
case else
end select
is the same as
Code:
If X=1 then
ElseIf x=2 then
ElseIf X=3 Then
Else
End If
Always use [code][/code] tags when posting code.
-
May 4th, 2010, 06:48 PM
#10
Re: Animation with 5 picture boxes using VB.NET
Hey DataMiser, Thanks again for your continuous help! I'm sorry I don't think I've fully grasped this code. I wrote a textfile contains just the case numbers:
Code:
1,4,3,2,4,2,3,1,3,2,4,3,1,2,4,3,2,1,4,3,4,1,2,4,3,4,2...etc
Then I used StreamReader to read the text file. But I get an error with using case 1,2,3,4. Do I have to declare the char values or do I have to write case "1"?
The error says:
Code:
Error 'Integer' values cannot be converted to 'Char'. Use 'Microsoft.VisualBasic.ChrW' to interpret a numeric value as a Unicode character or first convert it to 'String' to produce a digit.
The full code is here:
Code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3_PictureBox.Tick
Timer3_PictureBox.Enabled = True
Dim SequenceArray As String
Dim ArrayIndex As String
PictureBox0.Visible = True
PictureBox1.Visible = False
PictureBox2.Visible = False
PictureBox3.Visible = False
PictureBox4.Visible = False
Dim AhaGame As String = "C:\AhaImageText.txt"
Dim objReader1 As New System.IO.StreamReader(AhaGame)
Select Case SequenceArray(ArrayIndex)
Case 1
PictureBox1.Visible = True
Case 2
PictureBox2.Visible = True
Case 3
PictureBox3.Visible = True
Case 4
PictureBox4.Visible = True
End Select
ArrayIndex += 1
objReader1.Close()
End Sub
-
May 4th, 2010, 07:06 PM
#11
Re: Animation with 5 picture boxes using VB.NET
You must define the array and the array index outside the sub routine, at the top of the form code. e.g. Just after Public Class Form1. This allows the variables to retain thier value and also allows them to be accessed from other areas of your code.
Your code to read the file and populat the array would need to be done in a different sub routine and only needs to be done to start the process. Like maybe you would have a start button and when you click that button you would set the arrayindex=0 and read the file into the array.
Your timer routine would have only the code dealing with the picture boxes.
btw the arrayindex is an integer not a string
Always use [code][/code] tags when posting code.
-
May 4th, 2010, 07:35 PM
#12
Re: Animation with 5 picture boxes using VB.NET
What about the errors for the case numbers? Do I have to write Case "1", Case "2", etc ? Or would I have to write case chr(1), case chr(2),... etc? I want to test it but I can't with the errors still there
outside of the main program I placed:
Code:
Dim AllData As String
Dim ArrayIndex As Integer
I added this into my start button sub routine:
Code:
ArrayIndex = 0
Dim AllData() As String = System.IO.File.ReadAllLines("C:\AhaImageText.txt")
and my timer sub routine has this:
Code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Timer3.Enabled = True
PictureBox0.Visible = True
PictureBox1.Visible = False
PictureBox2.Visible = False
PictureBox3.Visible = False
PictureBox4.Visible = False
Select Case AllData(ArrayIndex)
Case 1
PictureBox1.Visible = True
Case 2
PictureBox2.Visible = True
Case 3
PictureBox3.Visible = True
Case 4
PictureBox4.Visible = True
End Select
ArrayIndex += 1
End Sub
Sorry for bothering you so much but my project is due tomorrow and this is my only issue left
Last edited by asf14; May 4th, 2010 at 08:35 PM.
Reason: Added text/typo
-
May 4th, 2010, 08:57 PM
#13
Re: Animation with 5 picture boxes using VB.NET
Also when I tested the program by writing case "1", case "2", case "3" ,
I got an error for the Select Case AllData(ArrayIndex) saying that the Object reference not set to an instance of an object.
I realized that I had AllData in two different subroutine so I changed the case statement to read the following code and I declared the variable outside the main code. But I still got the same error
Code:
PictureBox0.Visible = True
PictureBox1.Visible = False
PictureBox2.Visible = False
PictureBox3.Visible = False
PictureBox4.Visible = False
Select Case SequenceArray(ArrayIndex)
Case "1"
PictureBox1.Visible = True
Case "2"
PictureBox2.Visible = True
Case "3"
PictureBox3.Visible = True
Case "4"
PictureBox4.Visible = True
End Select
ArrayIndex += 1
-
May 4th, 2010, 09:11 PM
#14
Re: Animation with 5 picture boxes using VB.NET
Change it back to Integer rather than Character for the Select Case
Val() will convert String to Integer
Code:
Select Case VAL(SequenceArray(ArrayIndex))
Case 1
PictureBox1.Visible = True
Case 2
PictureBox2.Visible = True
Case 3
PictureBox3.Visible = True
Case 4
PictureBox4.Visible = True
End Select
Or just create the correct type as Integer and use it
-
May 4th, 2010, 09:52 PM
#15
Re: Animation with 5 picture boxes using VB.NET
I still get the same error about the object not referenced
I have this outside of my main code
Code:
Dim SequenceArray As String
Dim ArrayIndex As New Integer
My start button has this code:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
startTime = Trim(Format(Now, "hh:mm:ss tt"))
StartTimeBox.Text = startTime
My.Computer.Audio.Play("C:\Aha_TakeOnMe.wav", AudioPlayMode.Background)
Timer1.Start()
myStopwatch.Start()
ArrayIndex = 0
Dim AllData() As String = System.IO.File.ReadAllLines("C:\AhaImageText.txt")
End Sub
and my timer has this:
Code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Timer3.Enabled = True
PictureBox0.Visible = True
PictureBox1.Visible = False
PictureBox2.Visible = False
PictureBox3.Visible = False
PictureBox4.Visible = False
Select Case Val(SequenceArray(ArrayIndex))
Case 1
PictureBox1.Visible = True
Case 2
PictureBox2.Visible = True
Case 3
PictureBox3.Visible = True
Case 4
PictureBox4.Visible = True
End Select
ArrayIndex += 1
End Sub
How would the timer be able to read my text file to know which case to display? Would I need to use StreamReader or is the line in my start button sub routine sufficient to read the text file in an array?
Thanks for your help!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|