Image Slider in Visual Basic
Submitted by Yorkiebar on Saturday, May 24, 2014 - 07:27.
Introduction:
This tutorial is on how to create an image slider in Visual Basic.
Design:
For this project, our design requires;
Button, button1, To go to previous image.
Button, button2, To go to next image.
Picturebox, picturebox1, To hold the current image.
Note; You will most probably want to set the appropriate properties for the picturebox, otherwise some images will look larger than others.
Loading:
On form load, we want to get all the images in the default directory. So first we get set a variable as the username of the currently logged in user...
Then we use that to generate the default Windows 'pictures' library of that user...
Next we create a list of path urls for the files, we do this globally to access it from outside of the form load function...
Now we iterate through each file within the pictures path and add each ones file name (including path and extension) to the new list we have just created, 'fileURLs'...
We also want to create a current file index to keep track of where we are in our list...
Finally we set the picture URL of the picture box to the first fileURL in our list...
Previous:
Now, for the previous button we simply want to minus one from the fileCounter integer variable we created earlier (remember, it was globally available) then re-set the picturebox image location path...
Next:
For the next button, we want to do the same as the previous button... except... yep, you guessed it, add one...
Error...
The only problem with this so far is that we could get an index outside the amount of images we have (such as -1, or 10000). To avoid this, for the previous button we can check if the index is not already zero before we minus one...
So, if the index is already zero (0), it won't perform a new action.
The opposite for the next button, we check if the index is already the maximum it could get to...
Finished:
Here's the full source...
- Dim username As String = Environ("username")
- Dim path As String = "C:\Users\" + username + "\Pictures\"
- Dim fileURLs As New List(Of String)
- For Each file As String In My.Computer.FileSystem.GetFiles(path)
- If (file.EndsWith(".png") Or file.EndsWith(".jpg") Or file.EndsWith(".jpeg")) Then fileURLs.Add(file)
- Next
- Dim fileIndex As Integer = 0
- PictureBox1.ImageLocation = fileURLs(fileIndex)
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- fileIndex -= 1
- PictureBox1.ImageLocation = fileURLs(fileIndex)
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- fileIndex += 1
- PictureBox1.ImageLocation = fileURLs(fileIndex)
- End Sub
- If (Not fileIndex <= 0) Then fileIndex -= 1
- If Not (fileIndex >= fileURLs.Count() - 1) Then
- Public Class Form1
- Dim fileURLs As New List(Of String)
- Dim fileIndex As Integer = 0
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Dim username As String = Environ("username")
- Dim path As String = "C:\Users\" + username + "\Pictures\"
- For Each file As String In My.Computer.FileSystem.GetFiles(path)
- If (file.ToLower().EndsWith(".png") Or file.ToLower().EndsWith(".jpg") Or file.ToLower().EndsWith(".jpeg")) Then fileURLs.Add(file)
- Next
- PictureBox1.ImageLocation = fileURLs(fileIndex)
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- If (Not fileIndex <= 0) Then fileIndex -= 1
- PictureBox1.ImageLocation = fileURLs(fileIndex)
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- If Not (fileIndex >= fileURLs.Count() - 1) Then fileIndex += 1
- PictureBox1.ImageLocation = fileURLs(fileIndex)
- End Sub
- End Class
Add new comment
- 482 views