所以我有一个软件,当我点击按钮时显示图像但是当我点击下一个按钮时会显示另一个图像,使前一个图像显示为
'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
只是想知道是否有更简单的方法来执行此操作而不是将每个图像设置为假
答案 0 :(得分:3)
如果您试图将图像显示在表单的相同位置(即您的图片框放在同一个位置),则可以使用List(Of T)
并仅使用一个图片框代替。然后可以通过索引访问每个图像,您应该将其保存在变量中以跟踪当前显示的图像。
def balancedBrackets(s: String): Boolean =
s.substring(0,s.length/2) == s.substring(Math.floorDiv(s.length(),2)).map {
x => if(x == ')') '('
else if(x == ']') '['
else if(x == '}') '{'
}.mkString.reverse
添加图片:
'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)
上一张图片:
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)
时要记住的事项:
通过将索引传递到列表来访问图像:
List(Of T)
索引从零开始,意味着第一项具有索引0,第二项索引为1,依此类推(如上例中索引3 =第4项所示)。
答案 1 :(得分:0)
创建一个名为SetVisible
的函数来执行此操作:
visible = False
visible = True
然后你可以这样称呼它:
SetVisible (PicBoxing)
SetVisible (PicSoccer)
SetVisible (PicCanoeing)
答案 2 :(得分:-1)
将图片框添加到集合中会有所帮助。
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