|
|
Title | Use a loop to load pictures into PictureBoxes in Visual Basic 2005 |
Description | This example shows how to use a loop to load pictures into PictureBoxes in Visual Basic 2005. |
Keywords | PictureBox, picture, LoadPicture, Bitmap, loop, Visual Basic 2005 |
Categories | Controls, Graphics |
|
|
When the form loads, it builds the path to the image files. It then creates an array holding referencbes to the form's PictureBoxes. (If you use this array in more than once place, you can declare it at a module level so you don't need to recreate it later.)
The program then loops through the items in the array, loading the corresponding pictures named pic0.jpg, pic1.jpg, and so forth.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Dim path As String = Application.StartupPath
If path.EndsWith("\bin\Debug") Then path = _
path.Substring(0, path.LastIndexOf("\bin\Debug"))
If Not path.EndsWith("\") Then path &= "\"
' Make an array holding the PictureBoxes.
Dim pics() As PictureBox = _
{PictureBox1, PictureBox2, PictureBox3, PictureBox4}
' Load the pictures in a loop.
For i As Integer = 0 To pics.Length - 1
pics(i).Image = New Bitmap(path & "pic" & i & _
".jpg")
Next i
End Sub
|
|
|
|
|
|