Title | Display two forms, one showing pictures in a random order in Visual Basic .NET |
Description | This example shows how to display two forms, one showing pictures in a random order in Visual Basic .NET. |
Keywords | picture, random, VB.NET, Visual Basic .NET |
Categories | Algorithms, VB.NET |
|
|
When the first form appears, it creates an instance of the second form and displays it. It also positions the two forms.
|
|
Private PictureForm As Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Show the second form.
PictureForm = New Form2
PictureForm.Show()
Me.Location = New Point(10, 10)
PictureForm.Location = New Point( _
(Screen.PrimaryScreen.WorkingArea.Width - _
PictureForm.Width) \ 2, _
(Screen.PrimaryScreen.WorkingArea.Height - _
PictureForm.Height) \ 2)
End Sub
|
|
When the second form appears, it loads the pictures from its startup directory (or the source directory if the program is running in the debugger). It also makes a PictureBox for each picture. It finishes by calling RandomizePictures to display the pictures in a random order.
|
|
Private Pictures As New List(Of Image)
Private PictureBoxes As New List(Of PictureBox)
Private Sub Form2_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Get the source directory.
Dim dir_name As String = Application.StartupPath
If dir_name.EndsWith("\bin\Debug") Then dir_name = _
dir_name.Substring(0, dir_name.Length - 10)
' Load the pictures.
Dim dir_info As New DirectoryInfo(dir_name)
For Each file_info As FileInfo In _
dir_info.GetFiles("*.jpg")
Pictures.Add(Image.FromFile(file_info.FullName))
' Make a PictureBox for the picture.
Dim pic As New PictureBox()
pic.Parent = Me
pic.SizeMode = PictureBoxSizeMode.AutoSize
pic.Top = 5
PictureBoxes.Add(pic)
Next file_info
' Display the pictures.
RandomizePictures()
End Sub
|
|
Subroutine RandomizePictures makes a new list and copies the pictures into it in a random order. It then displays the pictures in the form's PictureBoxes.
|
|
' Notice that this is Public so the other form can use it.
Public Sub RandomizePictures()
' Randomize the list.
Dim rand As New Random()
Dim new_list As New List(Of Image)
Do While Pictures.Count > 0
' Move a random Image to the new list.
Dim i As Integer = rand.Next(0, Pictures.Count)
new_list.Add(Pictures(i))
Pictures.RemoveAt(i)
Loop
Pictures = new_list
' Display the pictures.
Dim max_height As Integer = 0
Dim x As Integer = 5
For i As Integer = 0 To Pictures.Count - 1
PictureBoxes(i).Image = Pictures(i)
PictureBoxes(i).Left = x
x += PictureBoxes(i).Width + 5
If max_height < PictureBoxes(i).Bottom Then _
max_height = PictureBoxes(i).Bottom
Next i
Me.ClientSize = New Size(x + 5, max_height + 5)
End Sub
|
|
Finally, because the RandomizePictures subroutine is declared Public, the code in the first form can call it. When you click the form's Randomize button, it uses the following code to re-randomize the pictures.
|
|
' Make the picture form randomize its pictures.
Private Sub btnRandomize_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnRandomize.Click
PictureForm.RandomizePictures()
End Sub
|
|
|
|