0

我正在为我的 VB 课程做一个快速项目,但遇到了一个问题。我是该语言的新手,所以我对自己在做什么一无所知...目前我有一个非常简单的程序设置为 RSVP 列表。我设置了一个 Txt 框以将名称插入列表框和男性/女性单选按钮,带有超过 21 个复选框和一个提交按钮以将此信息发送到列表框。我想做的是以某种方式合并一个循环以向标签添加一个计数器,当添加每个名称时,它会上升直到达到 15,然后它将显示一个消息框。这可能对循环没有任何用处,但我只是想把它挤在某个地方,因为它是分配的要求。

我的代码(是的,我知道它很差)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub SubmitBtn1_Click(sender As Object, e As EventArgs) Handles SubmitBtn1.Click
    Dim NamesInput As String
    NamesInput = NamesTxt1.Text
    If GenderRdBtn1.Checked = False And GenderRdBtn2.Checked = False Then
        MessageBox.Show("Please Select a Gender")

    ElseIf GenderRdBtn1.Checked = True Then
        MessageBox.Show("Congratulations! You Have RSVP'd as " & NamesInput)
    Else
        MessageBox.Show("Congratulations! You Have RSVP'd as " & NamesInput)
    End If
    If AgeChk1.Checked = True Then
        NamesLstBox1.Items.Add(NamesInput & " / Over 21")
    Else
        NamesLstBox1.Items.Add(NamesInput)
    End If

    If GenderRdBtn1.Checked = False And GenderRdBtn2.Checked = False Then
        NamesLstBox1.Items.RemoveAt(0)
    End If
End Sub

谢谢你的帮助!

4

1 回答 1

0

你是对的 - 这里不需要循环......但是,如果循环是“功能要求”,你可以在每次添加新名称时遍历 NamesLstBox Items 集合,迭代计数器,然后设置标签的值基于计数。

Dim Count as Integer
For Each i In NamesLstBox1.Items
    Count += 1
    If Count = 15 Then
        'display message box
    End If
Next
Label.Text = Count.ToString()

再一次 - 这是一个非常低效的操作,因为每次保存计数并简单地增加它是有意义的。我可能会在其他地方寻找更好地使用循环

于 2013-10-30T02:51:13.373 回答