Vb.net 每0,8秒从列表框中随机选择一项
我有一个代码,你可以在下面看到。你能告诉我如何优化它吗?因为有时候当我点击随机选取器时(此代码每0.8秒显示一次随机项,如“滚动轮子”),它不会显示f.e.20项,如我定义的。。。我只想让这个脚本工作:我点击一个按钮,它从列表框中滚动位置,并显示不在列表框中的项目Vb.net 每0,8秒从列表框中随机选择一项,vb.net,random,listbox,Vb.net,Random,Listbox,我有一个代码,你可以在下面看到。你能告诉我如何优化它吗?因为有时候当我点击随机选取器时(此代码每0.8秒显示一次随机项,如“滚动轮子”),它不会显示f.e.20项,如我定义的。。。我只想让这个脚本工作:我点击一个按钮,它从列表框中滚动位置,并显示不在列表框中的项目 Dim rnd As New Random Dim rndIndex As Integer = rnd.Next(10, 20) For ill As Integer = 0 To rndIndex Dim rnd As N
Dim rnd As New Random
Dim rndIndex As Integer = rnd.Next(10, 20)
For ill As Integer = 0 To rndIndex
Dim rnd As New Random
Dim randomIndex As Integer = rnd.Next(0, lCount)
If Not Label1.Text = ListBox1.Items(randomIndex) Then
Label1.Text = ListBox1.Items(randomIndex)
Delay(0.08)
Else
rndIndex = rndIndex + 1
End If
Next
这不起作用,因为有时我只看到6-8个不同的项目。可能是这样的:我有10个数字(0-9),它每隔0,8秒将以下数字放入标签中:1,5,7,4,3,1,5,8,6,3,0,3,1,5 我不是100%确定我应该这么做,但是你的代码有太多的地方出错了,要一步一步地把它升级到最新版本是相当困难的。基本上,我会每隔0.8秒在
标签中显示列表框
中的随机项,而不会在一行中重复同一项两次:
Private ReadOnly rng As New Random
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This is done in code here for clarity but you'd normally do this in the designer.
Timer1.Interval = 800
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Get all the items from the ListBox.
Dim items = ListBox1.Items.Cast(Of String)().ToList()
'Remove the currently displayed item.
items.Remove(Label1.Text)
'Get a random item from the remainder and display it.
Label1.Text = items(rng.Next(items.Count))
End Sub
正如我在评论中所说,没有循环。Timer
组件专门用于您希望定期执行操作时使用。还要注意的是,在生成随机数之前,当前显示的项已从等式中删除,因此重复次数永远不会有问题
编辑:
下面是根据我第一次错过的内容修改的代码:
Private ReadOnly rng As New Random
Private tickCount As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This is done in code here for clarity but you'd normally do this in the designer.
Timer1.Interval = 800
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
tickCount = rng.Next(10, 20)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
tickCount -= 1
If tickCount = 0 Then
Timer1.Stop()
End If
'Get all the items from the ListBox.
Dim items = ListBox1.Items.Cast(Of String)().ToList()
'Remove the currently displayed item.
items.Remove(Label1.Text)
'Get a random item from the remainder and display it.
Label1.Text = items(rng.Next(items.Count))
End Sub
我还对原始代码做了一点修改,因为最后一行是从ListBox1.Items
而不是Items
,随机索引获取项目。我不是100%确定我应该这样做,但您的代码有太多错误,要一步一步地将其更新到最新版本是相当困难的。基本上,我会每隔0.8秒在标签中显示列表框
中的随机项,而不会在一行中重复同一项两次:
Private ReadOnly rng As New Random
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This is done in code here for clarity but you'd normally do this in the designer.
Timer1.Interval = 800
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Get all the items from the ListBox.
Dim items = ListBox1.Items.Cast(Of String)().ToList()
'Remove the currently displayed item.
items.Remove(Label1.Text)
'Get a random item from the remainder and display it.
Label1.Text = items(rng.Next(items.Count))
End Sub
正如我在评论中所说,没有循环。Timer
组件专门用于您希望定期执行操作时使用。还要注意的是,在生成随机数之前,当前显示的项已从等式中删除,因此重复次数永远不会有问题
编辑:
下面是根据我第一次错过的内容修改的代码:
Private ReadOnly rng As New Random
Private tickCount As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This is done in code here for clarity but you'd normally do this in the designer.
Timer1.Interval = 800
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
tickCount = rng.Next(10, 20)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
tickCount -= 1
If tickCount = 0 Then
Timer1.Stop()
End If
'Get all the items from the ListBox.
Dim items = ListBox1.Items.Cast(Of String)().ToList()
'Remove the currently displayed item.
items.Remove(Label1.Text)
'Get a random item from the remainder and display it.
Label1.Text = items(rng.Next(items.Count))
End Sub
我还对原始代码做了一点修改,因为最后一行是从ListBox1.Items
中随机索引获取项目,而不是Items
为什么rndIndex=rndIndex+1
在循环中?此外,您的If
块也没有任何实际作用。在if语句中,您仅延迟if,这意味着您正在偏向随机性。仔细检查你的逻辑,你应该能够修复它。它可以工作,但有时不会显示20次,只有5次。为什么循环中出现rndIndex=rndIndex+1
?此外,您的If
块也没有任何实际作用。在if语句中,您仅延迟if,这意味着您正在偏向随机性。仔细检查你的逻辑,你应该能够修正它。它起作用,但有时不显示20次,只有5.不起作用。。它是一直随机的项目,而不是固定的时间,比如:Dim rndIndex As Integer=rnd。接下来(10,20)你解释得很糟糕,但我想我理解了。您是说您首先要生成一个范围为10到20的随机数,然后要每隔0.8秒多次显示列表框中的随机项?如果是这样,这是一个简单的修改。我将更新答案以反映这一点。如果这不是你想要的,请再次尝试解释清楚。。它是一直随机的项目,而不是固定的时间,比如:Dim rndIndex As Integer=rnd。接下来(10,20)你解释得很糟糕,但我想我理解了。您是说您首先要生成一个范围为10到20的随机数,然后要每隔0.8秒多次显示列表框中的随机项?如果是这样,这是一个简单的修改。我将更新答案以反映这一点。如果这不是你想要的,请再次尝试解释清楚。