从列表框中选择一个随机行

时间:2014-05-03 13:48:02

标签: vb.net listbox selectedindex

使用这个程序我试图让它在两个设定值之间生成信息,同时它正确地完成这部分我也试图得到它所以它从这个列表生成一个随机数(它在循环中做) )然后作为第三个功能是在列表框中也选择生成的随机数。我一直在尝试不同形式的编码,以使其选择与生成的随机数相同的数字,但我似乎无法使其工作。

Private Sub Generate_Click(sender As System.Object, e As System.EventArgs) Handles Generate.Click

    'Displays to enter correct information if lower is <=
    If System.Convert.ToInt32(Upper.Text) <= System.Convert.ToInt32(Lower.Text) Then
        List.Items.Clear()
        Number.Text = ""
        List.Items.Add("Please enter correct info Upper # higher value than Lower #")
    Else

        'If Upper range is higher than lower range then display numbers until total value is displayed
        List.Items.Clear()
        Number.Text = ""
        Dim i As Integer = Lower.Text

        Do While i <= Upper.Text 'Loop generates the numbers between values specified
            List.Items.Add(i)
            i += 1
        Loop

        'Select a random value from the list generated
        Dim myRandom As New Random
        Dim b As Integer = List.Items.Count
        Dim chosenItem As System.Object = List.Items.Item(myRandom.Next(b))
        Number.Text = chosenItem.ToString


    End If

2 个答案:

答案 0 :(得分:0)

您要将随机数分配给&#34; Number&#34;但不是&#34; List&#34;。

我补充一点,当你说明时,循环中没有生成随机数。循环用于将最小数字到最大数字的所有数字添加到&#34; List&#34;。它只不过是这样。随机数在循环下面的代码中生成。

试试这个:

Private Sub Generate_Click(sender As System.Object, e As System.EventArgs) Handles Generate.Click

Dim iMinimum As Integer = Integer.Parse(Lower.Text) 
Dim iMaximum As Integer = Integer.Parse(Upper.Text) 

List.Items.Clear()
Number.Text = ""

'Displays to enter correct information if lower is <=
If iMaximum <= iMinimum Then

    List.Items.Add("Please enter correct info Upper # higher value than Lower #")

Else

    'If Upper range is higher than lower range then display numbers until total value is displayed
    Dim i As Integer = iMinimum

    Do While i <= iMaximum 'Loop generates the numbers between values specified
        List.Items.Add(i.ToString)
        i += 1
    Loop

    'Select a random value and display
    Dim myRandom As New Random
    Dim iRandomNumber As Integer = myRandom.Next(iMinimum, iMaximum).ToString
    Number.Text = iRandomNumber.ToString
    List.Text = iRandomNumber.ToString

End If

End Sub

答案 1 :(得分:0)

如果我理解正确,您的代码大多是正确的。要使列表框突出显示随机数,请设置.SelectedItem属性。

Private Sub Generate_Click(sender As System.Object, e As System.EventArgs) Handles Generate.Click
    Dim i As Integer = System.Convert.ToInt32(Lower.Text)
    Dim limit As Integer = System.Convert.ToInt32(Upper.Text)
    If limit <= i Then
        List.Items.Clear()
        Number.Text = ""
        List.Items.Add("Please enter correct info Upper # higher value than Lower #")
    Else

        'If Upper range is higher than lower range then display numbers until total value is displayed
        List.Items.Clear()
        Number.Text = ""


        Do While i <= limit 'Loop generates the numbers between values specified
            List.Items.Add(i)
            i += 1
        Loop

        'Select a random value from the list generated
        Dim myRandom As New Random
        Dim b As Integer = List.Items.Count
        List.SelectedItem = List.Items.Item(myRandom.Next(b))
        Number.Text = List.SelectedItem.ToString
    End If
End Sub

另一方面,将变量更改为Double而不是Integer将允许您使用NumericUpDown控件而不是文本框。只需分配.Value属性,无需转换任何内容。