【问题标题】:Visual Basic (.NET) > randomize listbox picks (unique)Visual Basic (.NET) > 随机选择列表框(唯一)
【发布时间】:2017-06-26 19:42:58
【问题描述】:

我只想制作随机程序。它将用于挑选锦标赛对(例如欧洲冠军联赛四分之一决赛对)

图形界面:http://i.imgur.com/jqBMJjt.png

我有这个问题.. 当我尝试制作选择器时,它运行良好 > 从列表框 1(左侧)到列表框 2(列表框 1 项目的 50%)、列表框 3(列表框 1 项目的 50%)..(左side) 但这些选择并不是唯一的。您可以在图片上看到一些重复项(第二个列表框 2x noob)。

我的部分代码:

Private Sub RandomiseListBox()
    Dim count As Integer = CarbonFiberListBox1.Items.Count
    Dim countt As Integer
    'countt = count / 2
    Dim item As String
    Dim itemz As New List(Of String)()
    Dim repeat As New List(Of String)()
    Dim aa, bb As Integer


    If Not count = 0 And ((count Mod 2) = 0) Then
        CarbonFiberListBox2.Items.Clear()
        CarbonFiberListBox3.Items.Clear()

        For index As Integer = 0 To countt - 1 Step 1
            item = Me.CarbonFiberListBox1.Items(Me.randomiser.Next(index, count))
            itemz.Add(item)
            'Me.CarbonFiberListBox1.Items.Remove(item)
            'Me.CarbonFiberListBox1.Items.Insert(index, item)
            Me.CarbonFiberListBox2.Items.Insert(index, item)
        Next index

        For index As Integer = 0 To countt - 1 Step 1
            For aa = 0 To bb = 999
                item = Me.CarbonFiberListBox1.Items(Me.randomiser.Next(index, count))
                If Not (itemz.Contains(item)) And Not (repeat.Contains(item)) Then
                    repeat.Add(item)
                    'Me.CarbonFiberListBox1.Items.Remove(item)
                    'Me.CarbonFiberListBox1.Items.Insert(index, item)
                    Me.CarbonFiberListBox3.Items.Insert(index, item)
                End If
            Next
        Next index

        'For index As Integer = 0 To countt - 1 Step 1

        'Next index
    ElseIf count > 0 Then
        'CarbonFiberButton4.Text = "ODD PARTICIPANTS!"
    Else
    End If
End Sub

我可以寻求帮助吗?我认为这很容易。

【问题讨论】:

    标签: vb.net random listbox unique


    【解决方案1】:

    也许这个小例子会给你一个想法。它通过从一个列表框中以随机顺序获取列表来工作。请注意,当项目添加到其他停止重复的列表框时,列表是如何耗尽的。

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'some sample data
        ListBox1.DataSource = {"one", "two", "three", "four", "five", "six", "seven"}
        DoRandom()
    End Sub
    
    Private Shared prng As New Random
    Private Sub DoRandom()
        ListBox2.Items.Clear()
        ListBox3.Items.Clear()
        Dim l As New List(Of String)
        'random order of items in ListBox1
        l.AddRange(ListBox1.Items.Cast(Of String).OrderBy(Function(s) prng.Next))
        'add half to lb2
        For x As Integer = 0 To l.Count \ 2
            ListBox2.Items.Add(l(0))
            l.RemoveAt(0)
        Next
    
        'remainder to lb3
        For x As Integer = 0 To l.Count - 1
            ListBox3.Items.Add(l(0))
            l.RemoveAt(0)
        Next
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      相关资源
      最近更新 更多