如何折叠随机化的字符串列表中的空格

时间:2017-11-20 22:07:23

标签: vb.net

我有一个包含10个答案的字符串列表。 每个问题都有不同的答案2-10。 随机化列表后,根据答案的数量,我的列表中最后会出现空格或空格。

在随机化了一个让我们说2个答案的列表之后,我想将它们移回我的列表中的位置0和1,保持列表的大小为10,当然保持顺序随机化。

我不确定如何以编程方式解决此问题...

我试图在随机化后对列表进行排序/反转,但当然,这会删除随机化。

我试图用

之类的东西删除空白区域
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))

但是当我尝试将它们写回我的radiobuttons.text时,我得到了一个界限,因为它们有10个。

这是写我的......的列表的地方。

            RadioAnswer1.Text = answerlist(0)
            RadioAnswer2.Text = answerlist(1)
            RadioAnswer3.Text = answerlist(2)
            RadioAnswer4.Text = answerlist(3)
            RadioAnswer5.Text = answerlist(4)
            RadioAnswer6.Text = answerlist(5)
            RadioAnswer7.Text = answerlist(6)
            RadioAnswer8.Text = answerlist(7)
            RadioAnswer9.Text = answerlist(8)
            RadioAnswer10.Text = answerlist(9)

理想情况下,我希望列表随机化,然而许多答案会从0开始写回到列表中。 我希望我的问题很明确。

其他信息编辑

所以这就是我如何将答案加载到List Of ..         昏暗的答案列表作为新列表(字符串)

    WEFESQLConn.ConnectionString = connectstring
    WEFESQLConn.Open()

    WERESQLStatment.CommandText = "SELECT * FROM [WEFE Questions] WHERE QuestionID = " & SQLQuestionNum.ToString
    WERESQLStatment.Connection = WEFESQLConn
    WEFESQLRead = WERESQLStatment.ExecuteReader

    If WEFESQLRead.HasRows Then
        WEFESQLRead.Read()

        lblQuestion.Text = WEFESQLRead.Item("Question").ToString
        answerlist.Add(WEFESQLRead.Item("CorrectAnswer").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer2").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer3").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer4").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer5").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer6").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer7").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer8").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer9").ToString)
        answerlist.Add(WEFESQLRead.Item("Answer10").ToString)

        answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))

        WEFESQLRead.Close()
        WEFESQLConn.Close()


        RadioAnswer1.Text = answerlist(0)
            RadioAnswer2.Text = answerlist(1)
            RadioAnswer3.Text = answerlist(2)
            RadioAnswer4.Text = answerlist(3)
            RadioAnswer5.Text = answerlist(4)
            RadioAnswer6.Text = answerlist(5)
            RadioAnswer7.Text = answerlist(6)
            RadioAnswer8.Text = answerlist(7)
            RadioAnswer9.Text = answerlist(8)
            RadioAnswer10.Text = answerlist(9)

使用此代码,我得到了界限,因为没有足够的答案来填充答案列表。

没有

answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))

我在预先绘制的单选按钮中获得了空格。 我已准备好隐藏未使用的按钮 - 按钮的10个位置以及列表的随机化问题。

4 spots of 10 used image

3 个答案:

答案 0 :(得分:2)

为什么在从列表中删除空白条目后,无法动态切换控件的可见性?看一下这个例子:

'Store all controls in a collection
Dim answers(9) As RadioButton = {RadioAnswer1, RadioAnswer2, RadioAnswer3, RadioAnswer4, RadioAnswer5, RadioAnswer6, RadioAnswer7, RadioAnswer8, RadioAnswer9, RadioAnswer10}

'Iterate through all answers
For index As Integer = 0 To answerlist.Count - 1
    'Show the control and set the text
    With answers(index)
        .Text = answerlist.Item(index)
        .Visible = True
    End With
Next

'Loop through the rest of the answer controls
For index As Integer = answerlist.Count To answers.Length - 1
    'Hide the control
    answers(index).Visible = False
Next

答案 1 :(得分:0)

初始设置......

Dim radioButtons As New List(Of RadioButton)
radioButtons.Add(RadioAnswer1)
radioButtons.Add(RadioAnswer2)
...
radioButtons.Add(RadioAnswer10)

从随机列表中删除空白答案后......

For i = 0 To answerList.Count - 1
    radioButtons(i).Text = answerList(i)
    radioButtons(i).Visible = True
Next
For i = answerList.Count to radioButtons.Count - 1
    radioButtons(i).Visible = False
Next

答案 2 :(得分:0)

要关闭它,我最终会从我的ListOF中删除所有空格,然后为每个radiobutton.text条目使用DO WHILE。

示例部分

 answerlist = RandomizeListOrder(answerlist)
            answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))

            Dim ALCount As Integer = answerlist.Count
            Dim ALCounter = 0

            Do

                If ALCounter < ALCount Then
                    ALCounter += 1
                    RadioAnswer1.Text = answerlist(0)
                ElseIf ALCounter = ALCount Then
                    Exit Do
                End If

也许不是很干净,因为这些条目中有10个,但我稍后会对此进行处理。只是想把这个想法弄清楚。

感谢大家的建议,他们让我走上了正确的道路。