kunz
kunz

Reputation: 1047

Easier way to clear all images in VB.Net

so i have a software when ever i click a button a image is displayed but when i click the next button another image is displayed making the previous image false

'When Button1 is clicked do'
PicBasketball.Visible = True

'Flase'
PicBoxing.Visible = False
PicSoccer.Visible = False
PicCanoeing.Visible = False
PicGolf.Visible = False
PicSwimming.Visible = False
PicRugby.Visible = False

Just wondering if there is a easier method to do this instead of setting each image false

Upvotes: 0

Views: 459

Answers (3)

Visual Vincent
Visual Vincent

Reputation: 18310

If you're trying to show your images at the same position of your form (i.e. your picture boxes are put in the very same spot), you could get use of a List(Of T) and use only one picture box instead. Each image is then accessible via an index, which you should save in a variable to keep track of which image you are currently showing.

'Class level (outside any Sub or Function, but inside Public Class).
Dim Images As New List(Of Image)
Dim ImageIndex As Integer = 0

Adding images:

Images.Add(Image.FromFile("your file path here"))
'or:
Images.Add(your image object)

Removing images:

Images.RemoveAt(zero-based index)

Next image:

ImageIndex += 1
If ImageIndex >= Images.Count Then ImageIndex = 0 'Going back to the beginning if we're at the last image.

YourPictureBox.Image = Images(ImageIndex)

Previous image:

ImageIndex -= 1
If ImageIndex < 0 Then ImageIndex = Images.Count - 1 'Going to the last image if we're in the beginning.

YourPictureBox.Image = Images(ImageIndex)

Things to keep in mind when using a List(Of T):

  • Accessing an image is done by passing its index to the list:

    Images(3) 'Returns the fourth (4th) image.
    
  • The index is zero-based, meaning the first item has index 0, the second index 1 and so on (as seen in the above example index 3 = the 4th item).

Upvotes: 3

dbasnett
dbasnett

Reputation: 11773

Adding the picture boxes to a collection will help.

Dim myPBs As New List(Of PictureBox)
Dim curVis As PictureBox

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    'if ALL of the pictureboxes on the form
    ' are part of this there is
    'an easier way, but the long way works
    myPBs.Add(PictureBox1)
    myPBs.Add(PictureBox2)
    myPBs.Add(PictureBox3)

    For Each pb As PictureBox In myPBs
        pb.Visible = False 'initial setting
    Next

    curVis = myPBs(0) 'show first one
    Button1.PerformClick()
End Sub

Dim ShowPB As Integer = Integer.MaxValue - 1 'which index being shown
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    curVis.Visible = False
    ShowPB += 1
    If ShowPB >= myPBs.Count Then
        ShowPB = 0 'reset
    End If
    curVis = myPBs(ShowPB)
    curVis.Visible = True
End Sub

Upvotes: -1

Raj More
Raj More

Reputation: 48016

Make a function called SetVisible to do this:

  • Accept a string parameter called PictureName
  • Loop through all the controls, filter the ones that are image type, and that start with "Pic" and set visible = False for all of them
  • Loop through all the controls, filter the ones that are image type, and match the "PictureName" and set visible = True

You can then call it like this:

SetVisible (PicBoxing)
SetVisible (PicSoccer)
SetVisible (PicCanoeing)

Upvotes: 0

Related Questions