vb 2010 listview中的条件语句

时间:2014-02-14 23:22:07

标签: visual-studio-2010

我在列表视图中放了三个项目,并在复选框中选择其中一个项目。如果我检查从第一个到最后一个的项目,但是它不在时间和后退时,代码可以工作。我的意思是,在选择最后一项后,如果我再次选择第二项,则显示的信息是错误的。怎么解决它?

干杯。

    For Each Me.item In lsv_school.Items
        If Not item.Index = e.Index Then
            item.Checked = False
            If e.Index = lsv_school.Items(0).Checked Then
                lbl_err.Hide()
                lbl_feed.Text = "Your school tuition will be " + "$" & dipAcc + " per term."
            ElseIf e.Index = lsv_school.Items(1).Checked Then
                lbl_err.Hide()
                lbl_feed.Text = "Your school tuition will be " + "$" & dipBus + " per term."
            ElseIf e.NewValue = lsv_school.Items(2).Checked Then
                lbl_err.Hide()
                lbl_feed.Text = "Your school tuition will be " + "$" & dipMar + " per term."
            End If
        End If
    Next

End Sub

1 个答案:

答案 0 :(得分:0)

我自己解决了这个烂摊子,感谢您的建议,但我不得不使用这些组件,否则无法做到。我将发布我使用的代码。

第一个子类是一个循环来检查一个复选框。

Private Sub lsv_school_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lsv_school.ItemCheck

        'Loop to check one checkbox at time.
        For Each Me.item In lsv_school.Items

            If Not item.Index = e.Index Then
                item.Checked = False
            End If

        Next

    End Sub

第二个子类解决问题,条件语句现在正确检查每个案例。

Private Sub lsv_school_ItemChecked(ByVal sender As Object, ByVal e As ItemCheckedEventArgs) Handles lsv_school.ItemChecked

    'Conditional statements to select one course at time.
    If lsv_school.Items(0).Checked = True Then
        item.Selected = CBool(dipAcc)
        lbl_err.Hide()
        lbl_feed.Show()
        lbl_feed.Text = "Your school tuition will be " + "$" & dipAcc
    ElseIf lsv_school.Items(1).Checked = True Then
        item.Selected = CBool(dipBus)
        lbl_err.Hide()
        lbl_feed.Show()
        lbl_feed.Text = "Your school tuition will be " + "$" & dipBus
    ElseIf lsv_school.Items(2).Checked = True Then
        item.Selected = CBool(dipMar)
        lbl_err.Hide()
        lbl_feed.Show()
        lbl_feed.Text = "Your school tuition will be " + "$" & dipMar
    Else
        If item.Checked = False Then
            lbl_feed.Text = ""
            lbl_feed.Hide()
            lbl_err.Show()
            lbl_err.Text = "Select a course from the list."
        End If
    End If

End Sub

通过这种方式,我设法让复选框在列表视图中用作单选按钮。