这是我的代码
Public Class FrmGameBoard
Const NumValues As Integer = 26
Dim Values(NumValues) As Decimal
Dim ValueCollection = New Collection
Dim i As Integer = 0
Dim Briefcases = New Collection
Dim RemainingBriefcases = New Collection
Dim GlobalAction As Integer = 0
Dim turn As Integer = 0
Dim name As String
Dim numberofremainingvalues, sumofremainingvalues As Integer
Dim MyValue, EVBig, EVSmall As Double
Dim n As Integer = 0
Function longif()
If MyValue = 0.01 Then
Lbl1p.Visible = False
RemainingBriefcases.Remove(0)
ElseIf MyValue = 0.02 Then
Lbl2p.Visible = False
RemainingBriefcases.Remove(1)
ElseIf MyValue = 0.05 Then
Lbl5p.Visible = False
RemainingBriefcases.Remove(2)
ElseIf MyValue = 0.1 Then
Lbl10p.Visible = False
RemainingBriefcases.Remove(3)
ElseIf MyValue = 0.25 Then
Lbl25p.Visible = False
RemainingBriefcases.Remove(4)
ElseIf MyValue = 0.5 Then
Lbl50p.Visible = False
RemainingBriefcases.Remove(5)
ElseIf MyValue = 0.75 Then
Lbl75p.Visible = False
RemainingBriefcases.Remove(6)
ElseIf MyValue = 1.0 Then
Lbl1d.Visible = False
RemainingBriefcases.Remove(7)
ElseIf MyValue = 2.5 Then
Lbl2d50p.Visible = False
RemainingBriefcases.Remove(8)
ElseIf MyValue = 5.0 Then
Lbl5d.Visible = False
RemainingBriefcases.Remove(9)
ElseIf MyValue = 10.0 Then
Lbl10d.Visible = False
RemainingBriefcases.Remove(10)
ElseIf MyValue = 25.0 Then
Lbl25d.Visible = False
RemainingBriefcases.Remove(11)
ElseIf MyValue = 50.0 Then
Lbl50d.Visible = False
RemainingBriefcases.Remove(12)
ElseIf MyValue = 100.0 Then
Lbl100d.Visible = False
RemainingBriefcases.Remove(13)
ElseIf MyValue = 500.0 Then
Lbl500d.Visible = False
RemainingBriefcases.Remove(14)
ElseIf MyValue = 1000.0 Then
Lbl1000d.Visible = False
RemainingBriefcases.Remove(15)
ElseIf MyValue = 5000.0 Then
Lbl5000d.Visible = False
RemainingBriefcases.Remove(16)
ElseIf MyValue = 10000.0 Then
Lbl10000d.Visible = False
RemainingBriefcases.Remove(17)
ElseIf MyValue = 50000.0 Then
Lbl50000d.Visible = False
RemainingBriefcases.Remove(18)
ElseIf MyValue = 100000.0 Then
Lbl100000d.Visible = False
RemainingBriefcases.Remove(19)
ElseIf MyValue = 250000.0 Then
Lbl250000d.Visible = False
RemainingBriefcases.Remove(20)
ElseIf MyValue = 500000.0 Then
Lbl500000d.Visible = False
RemainingBriefcases.Remove(21)
ElseIf MyValue = 750000.0 Then
Lbl750000d.Visible = False
RemainingBriefcases.Remove(22)
ElseIf MyValue = 1000000 Then
Lbl1000000d.Visible = False
RemainingBriefcases.Remove(23)
ElseIf MyValue = 1250000.0 Then
Lbl1250000d.Visible = False
RemainingBriefcases.Remove(24)
ElseIf MyValue = 1500000.0 Then
Lbl1500000.Visible = False
RemainingBriefcases.Remove(25)
End If
turn = turn + 1
LblTurnNumber.Text = CStr(turn)
End Function
Private Sub FrmGameBoard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim X As Integer
Dim n As Integer = 1
Dim MyRandom As New Random()
Values(0) = 0.01
Values(1) = 0.02
Values(2) = 0.05
Values(3) = 0.1
Values(4) = 0.25
Values(5) = 0.5
Values(6) = 0.75
Values(7) = 1.0
Values(8) = 2.5
Values(9) = 5.0
Values(10) = 10.0
Values(11) = 25.0
Values(12) = 50.0
Values(13) = 100.0
Values(14) = 500.0
Values(15) = 1000.0
Values(16) = 5000.0
Values(17) = 10000.0
Values(18) = 50000.0
Values(19) = 100000.0
Values(20) = 250000.0
Values(21) = 500000.0
Values(22) = 750000.0
Values(23) = 1000000.0
Values(24) = 1250000.0
Values(25) = 1500000.0
name = InputBox("Please enter your name below.")
LblWelcome.Text = "Welcome " & name
Do While i <= NumValues
ValueCollection.Add(Values(i))
RemainingBriefcases.Add(Values(i))
i = i + 1
Loop
i = 1
For n = 1 To NumValues
X = MyRandom.Next(1, ValueCollection.Count)
Briefcases.Add(ValueCollection(X))
ValueCollection.Remove(X)
Next
n = 1
i = 1
End Sub
Private Sub Lbl1_Click(sender As Object, e As EventArgs) Handles Lbl1.Click
Const MyNumber As Integer = 1
Lbl1.Visible = False
Pic1.Visible = False
MyValue = Briefcases(MyNumber)
Dim action As Integer = 0
If GlobalAction = 0 Then
LblMyBriefcase.Text = CStr(MyNumber)
action = 1
GlobalAction = 1
End If
If action = 0 Then
longif()
End If
End Sub
Private Sub Pic1_Click(sender As Object, e As EventArgs) Handles Pic1.Click
Const MyNumber As Integer = 1
Lbl1.Visible = False
Pic1.Visible = False
MyValue = Briefcases(MyNumber)
Dim action As Integer = 0
If GlobalAction = 0 Then
LblMyBriefcase.Text = CStr(MyNumber)
action = 1
GlobalAction = 1
End If
If action = 0 Then
longif()
End If
End Sub
Private Sub Lbl2_Click(sender As Object, e As EventArgs) Handles Lbl2.Click
Const MyNumber As Integer = 2
Lbl2.Visible = False
Pic2.Visible = False
MyValue = Briefcases(MyNumber)
Dim action As Integer = 0
If GlobalAction = 0 Then
LblMyBriefcase.Text = CStr(MyNumber)
action = 1
GlobalAction = 1
End If
If action = 0 Then
longif()
End If
End Sub
Private Sub Pic2_Click(sender As Object, e As EventArgs) Handles Pic2.Click
Const MyNumber As Integer = 2
Lbl2.Visible = False
Pic2.Visible = False
Dim action As Integer = 0
MyValue = Briefcases(MyNumber)
If GlobalAction = 0 Then
LblMyBriefcase.Text = CStr(MyNumber)
action = 1
GlobalAction = 1
End If
If action = 0 Then
longif()
End If
End Sub
Private Sub Lbl3_Click(sender As Object, e As EventArgs) Handles Lbl3.Click
Const MyNumber As Integer = 3
Lbl3.Visible = False
Pic3.Visible = False
Dim action As Integer = 0
MyValue = Briefcases(MyNumber)
If GlobalAction = 0 Then
LblMyBriefcase.Text = CStr(MyNumber)
action = 1
GlobalAction = 1
End If
If action = 0 Then
longif()
End If
End Sub
Private Sub Pic3_Click(sender As Object, e As EventArgs) Handles Pic3.Click
Const MyNumber As Integer = 3
Lbl3.Visible = False
Pic3.Visible = False
Dim action As Integer = 0
MyValue = Briefcases(MyNumber)
If GlobalAction = 0 Then
LblMyBriefcase.Text = CStr(MyNumber)
action = 1
GlobalAction = 1
End If
If action = 0 Then
longif()
End If
End Sub
Private Sub Lbl4_Click(sender As Object, e As EventArgs) Handles Lbl4.Click
Const MyNumber As Integer = 4
Lbl4.Visible = False
Pic4.Visible = False
Dim action As Integer = 0
MyValue = Briefcases(MyNumber)
If GlobalAction = 0 Then
LblMyBriefcase.Text = CStr(MyNumber)
action = 1
GlobalAction = 1
End If
If action = 0 Then
longif()
End If
End Sub
Private Sub Pic4_Click(sender As Object, e As EventArgs) Handles Pic4.Click
Const MyNumber As Integer = 4
Lbl4.Visible = False
Pic4.Visible = False
Dim action As Integer = 0
MyValue = Briefcases(MyNumber)
If GlobalAction = 0 Then
LblMyBriefcase.Text = CStr(MyNumber)
action = 1
GlobalAction = 1
End If
If action = 0 Then
longif()
End If
End Sub
............... (the same code just continues for the different picboxes and labels up to 26)
End Class
我想在RemainingBriefcases Collection中获取剩余公文包的值,因此我删除了longif()函数中的选定值。但是,它适用于前几次单击,但在几次之后它会突出显示RemainingBriefcases.Remove语句之一,并且表示Collection索引必须在1到集合大小的范围内。 我应该改变什么? 感谢
答案 0 :(得分:0)
Dim briefcases As List(Of Double) = New List(Of Double)
briefcases.Add( 1.0 )
briefcases.Add( 2.0 )
briefcases.Add( 3.0 )
briefcases.Remove( 2.0 )
这将允许您按编号删除项目。
答案 1 :(得分:0)
扩展我之前的评论,我创建了一个新的Windows窗体项目。我添加了一个标签“LblTurnNumber”,一个面板“pnlBriefcases”显示公文包,一个面板“valuesLabels”显示未选择的值。
Option Infer On
Option Strict On
Public Class FrmGameBoard
Dim briefcases As List(Of Briefcase)
Dim valuesLabels As List(Of Label)
Dim clicksProcessed As Integer = 0
Class Briefcase
Property Value As Decimal
Property AssociatedLabel As Label
Property AssociatedPictureBox As PictureBox
Property IsAvailable As Boolean = True
End Class
Private Sub Briefcase_Click(sender As Object, e As EventArgs)
' get the control which raised the event
Dim ctrl = DirectCast(sender, Control)
' get the briefcase for that control
Dim bc = DirectCast(ctrl.Tag, Briefcase)
bc.AssociatedLabel.Visible = False
bc.AssociatedPictureBox.Visible = False
' I don't know the purpose of the following line: I carried it over from the example oode
' referring to Briefcases vs. RemainingBriefcases
bc.IsAvailable = False
' Find the value in the available values corresponding to the item clicked on...
Dim valueToRemove = bc.Value.ToString()
Dim valueToHide = valuesLabels.First(Function(vl) vl.Text = valueToRemove)
valueToHide.Visible = False
clicksProcessed += 1
LblTurnNumber.Text = clicksProcessed.ToString()
End Sub
Sub Init()
Dim rand As New Random()
Dim values() As Decimal = {0.01D, 0.02D, 0.05D, 0.1D, 0.25D, 0.5D, 0.75D, 1D, 2.5D, 5D, 10D, 25D, 50D, 100D, 500D, 1000D, 5000D, 10000D, 50000D, 100000D, 250000D, 500000D, 750000D, 1000000D, 1250000D, 1500000D}
' Create a set of labels showing the available values
' and show them in a panel named "valuesPanel".
valuesLabels = New List(Of Label)
For i = 0 To values.Length - 1
Dim valueLabel As New Label With {.Text = values(i).ToString()}
valueLabel.TextAlign = ContentAlignment.MiddleRight
Dim yPos = valuesPanel.Height - 24 * (i + 1)
valueLabel.Location = New Point(6, yPos)
valuesPanel.Controls.Add(valueLabel)
valuesLabels.Add(valueLabel)
Next
' Get a shuffled list of the values
Dim shuffledValues As New List(Of Decimal)
For i = 0 To values.Length - 1
shuffledValues.Insert(rand.Next(0, i), values(i))
Next
' Create briefcases with imaages and labels
' and associate those to UI elements.
briefcases = New List(Of Briefcase)
For i = 0 To values.Count - 1
Dim lbl As New Label
Dim pb As New PictureBox
' N.B. The following is a crude and unsatisfactory way to display the briefcases and
' requires improvement.
'TODO: set the text of lbl and the image of pb properly
'TODO: set the position of lbl and the position of pb properly
pb.Image = Bitmap.FromFile("C:\temp\briefcase.png")
Dim xSize = pb.Image.Width
Dim ySize = pb.Image.Height
Dim xLoc = (i Mod 13) * xSize ' magic number 13 to make two rows of briefcases because there are 26 of them
Dim yLoc = (i \ 13) * ySize + 40 ' magic number 40 to crudely account for label size
pb.Size = pb.Image.Size
pb.Location = New Point(xLoc, yLoc)
lbl.Location = New Point(xLoc, yLoc + ySize)
lbl.Text = "???" & i.ToString()
' Tell it what to do when the Label or PictureBox is clicked...
AddHandler lbl.Click, AddressOf Briefcase_Click
AddHandler pb.Click, AddressOf Briefcase_Click
' Show the controls we have created...
pnlBriefcases.Controls.Add(lbl)
pnlBriefcases.Controls.Add(pb)
' Create a Briefcase which has a value and is connected to the label and picturebox:
Dim thisBriefcase = New Briefcase With {.Value = shuffledValues(i), .AssociatedLabel = lbl, .AssociatedPictureBox = pb}
' Connect the label and picturebox to the Briefcase
' - the .Tag holds an Object, i.e. anything we want as long as we know what to convert it back to when we use it.
lbl.Tag = thisBriefcase
pb.Tag = thisBriefcase
briefcases.Add(thisBriefcase)
Next
LblTurnNumber.Text = clicksProcessed.ToString()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Init()
End Sub
End Class
图像“C:\ temp \ briefcase.png”是
启动程序显示:
(我确实说它需要对UI进行改进......) 点击几个图片框/标签后......
如果您需要处理仍然显示的公文包,您可以使用类似
的列表获取它们Dim stillShown = briefcases.Where(Function(bc) bc.IsAvailable).ToList()