0

嗨,我正在尝试制作一个有 6 个复选框的程序,当一个复选框被选中时,标签会显示演员的平均值,但如果选中多个复选框,则标签只会显示选中框的最高平均值。

 Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    SC = ((Val(425488741) + Val(555909803) + Val(868659354) + Val(966435555) + Val(720388023) + Val(617520987)) / 6)

    If CheckBox1.Checked = True Then

        Label3.Text = "Sean Connery $" & SC

        Exit Sub

    End If

End Sub



Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

    GL = 513445231

    If CheckBox2.Checked = True Then

        Label3.Text = "George Lazenby $" & GL

        Exit Sub

    End If

End Sub



Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged

    RM = ((Val(785677477) + (426826774) + (666367656)  + (624527272) + (481005579) + (405873493) + (316186616)) / 7)

    If CheckBox3.Checked = True Then

        Label3.Text = "Roger Moore $" & RM

        Exit Sub

    End If

End Sub



Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged

    TD = ((Val(362876056) + (271586451)) / 2)

    If CheckBox4.Checked = True Then

        Label3.Text = "Timothy Dalton $" & TD

        Exit Sub

    End If

End Sub



Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged

    PB = ((Val(499954330) + (465588535) + (504705882)  + (546490272)) / 4)

    If CheckBox5.Checked = True Then

        Label3.Text = "Pierce Brosnan $" & PB

        Exit Sub

    End If

End Sub



Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged

    DC = ((Val(640803677) + (586090727)) / 2)

    If CheckBox6.Checked = True Then

        Label3.Text = "Daniel Craig $" & DC

        Exit Sub

    End If

 End Sub
4

2 回答 2

0

详细信息: Saulius 是正确的,因为您应该使用数据库,并且应该使用选中列表框来使其更容易。但是,我只是假设您出于任何原因都想使用单独的复选框。

解决方案:

创建一个 Actor 类

Public Class Actor
   Property Name As String
   Property TotalValue As Double
End Class


在你的主要形式

Public Sub DisplayHighestPaidActor(ByVal actorName As String, ByVal isChecked As Boolean)

    If isChecked Then
        'Add the Actor
        selectedActors.Add((From actor In allActors
                            Where actor.Name = actorName).FirstOrDefault())

        'Order the Actors
        selectedActors = (From actor In selectedActors
                          Order By actor.TotalValue Descending).ToList()
    Else
        'Remove the Actor
        selectedActors.Remove((From actor In allActors
                               Where actor.Name = actorName).FirstOrDefault())

        'Order the Actors
        selectedActors = (From actor In selectedActors
                          Order By actor.TotalValue Descending).ToList()

    End If

    If (selectedActors.Count > 0) Then

        'Display the highest value
        lblHighestPaidActor.Text = selectedActors.Item(0).Name.ToString() _
                          + " $" + selectedActors.Item(0).TotalValue.ToString()
    Else
        lblHighestPaidActor.Text = ""
    End If

End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    DisplayHighestPaidActor("Sean Connery", CheckBox1.Checked)

End Sub


Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

    DisplayHighestPaidActor("George Lazenby", CheckBox2.Checked)

End Sub


Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged

    DisplayHighestPaidActor("Roger Moore", CheckBox3.Checked)

End Sub
于 2012-10-16T12:34:30.470 回答
0

以后在提问时,请不要包含所有不必要的细节。如果正确理解您的问题,这是一种可能的简单解决方案:

Dim incomes = New Integer() {100, 2000, 500}
Dim names = New String() {"John", "Tim", "Allan"}

Private Sub CheckedListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
    Dim maxIncome As Integer = 0
    Dim name As String = ""

    For i = 0 To incomes.Length - 1
        If CheckedListBox1.GetItemChecked(i) And incomes(i) > maxIncome Then
            maxIncome = incomes(i)
            name = names(i)
        End If
    Next

    Label1.Text = name & " $" & maxIncome
End Sub

要使此代码正常工作,您需要在其集合中创建一个包含 3 个项目的 CheckedListBox。但是,一旦有很多值,这种方法可能会导致代码非常糟糕。在这种情况下,您应该使用数据库或至少使用如下结构:

Structure People
    Dim name As String
    Dim income As Integer
    Dim checkBox As CheckBox
End Structure

此外,无需Exit Sub在代码中的任何地方编写,它什么也不做。

于 2012-10-15T23:01:31.480 回答