在VB.Net中清除所有图像的更简单方法

在VB.Net中清除所有图像的更简单方法,vb.net,Vb.net,因此,我有一个软件,每当我点击一个按钮时,会显示一个图像,但当我点击下一个按钮时,会显示另一个图像,使上一个图像为假 'When Button1 is clicked do' PicBasketball.Visible = True 'Flase' PicBoxing.Visible = False PicSoccer.Visible = False PicCanoeing.Visible = False PicGolf.Visible = False PicSwimming.Visible

因此,我有一个软件,每当我点击一个按钮时,会显示一个图像,但当我点击下一个按钮时,会显示另一个图像,使上一个图像为假

'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

只是想知道是否有一种更简单的方法来执行此操作,而不是将每个图像设置为false

使一个名为
SetVisible
的函数来执行此操作:

  • 接受名为PictureName的字符串参数
  • 循环遍历所有控件,过滤图像类型的控件,以及以“Pic”开头的控件,并为所有控件设置
    visible=False
  • 循环遍历所有控件,筛选图像类型的控件,并匹配“PictureName”并设置
    visible=True
你可以这样称呼它:

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

使用名为
SetVisible
的函数执行此操作:

  • 接受名为PictureName的字符串参数
  • 循环遍历所有控件,过滤图像类型的控件,以及以“Pic”开头的控件,并为所有控件设置
    visible=False
  • 循环遍历所有控件,筛选图像类型的控件,并匹配“PictureName”并设置
    visible=True
你可以这样称呼它:

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

将图片框添加到集合将有所帮助

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

将图片框添加到集合将有所帮助

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

如果您试图在表单的同一位置显示图像(即,您的图片框放在同一位置),您可以使用a,而只使用一个图片框。然后可以通过索引访问每个图像,您应该将索引保存在变量中,以跟踪当前显示的图像

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

Images.Add(Image.FromFile("your file path here"))
'or:
Images.Add(your image object)
Images.RemoveAt(zero-based index)
删除图像:

Images.Add(Image.FromFile("your file path here"))
'or:
Images.Add(your image object)
Images.RemoveAt(zero-based index)
下一张图片:

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)
上一张图片:

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)
  • 索引是以零为基础的,这意味着第一项有索引0,第二项有索引1,依此类推(如上例所示,索引3=第四项)


  • 如果您试图在表单的同一位置显示图像(即,您的图片框放在同一位置),您可以使用a,而只使用一个图片框。然后可以通过索引访问每个图像,您应该将索引保存在变量中,以跟踪当前显示的图像

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

    Images.Add(Image.FromFile("your file path here"))
    'or:
    Images.Add(your image object)
    
    Images.RemoveAt(zero-based index)
    
    删除图像:

    Images.Add(Image.FromFile("your file path here"))
    'or:
    Images.Add(your image object)
    
    Images.RemoveAt(zero-based index)
    
    下一张图片:

    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)
    
    上一张图片:

    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)
    
  • 索引是以零为基础的,这意味着第一项有索引0,第二项有索引1,依此类推(如上例所示,索引3=第四项)


  • 为什么会被否决?这可能不是最优雅的解决方案(到目前为止没有一个答案),但这是一个比最初的解决方案更好的解决方案。我对此的主要抱怨是,没有评论就投了反对票。为什么会投反对票?这可能不是最优雅的解决方案(到目前为止,没有一个答案是这样),但这是一个比最初的解决方案更好的解决方案。我对此的主要抱怨是,在没有评论的情况下投了反对票。