1

只是想问为什么这对我不起作用?

我正在尝试为预订系统(或类似的东西)带回数据。

问题是它只使用一个 if 语句而忽略第二个。

使用 VB.net 并尝试连接到 MySQL 数据库。

谢谢大家...

彼得

谢谢大家的回答,我会尝试所有的问题是,如果它不是=让说星期一,那么它应该使复选框的颜色变为白色。

我将尝试选择案例并查看。

我做了一些更改,添加了复选框列表,使其更容易。问题是在不同的日子清除复选框。

    CheckBoxListMon.BackColor = Drawing.Color.White
    CheckBoxListMon.Enabled = True
    CheckBoxListMon.ClearSelection()

这使复选框已启用且未选中,但我仍然无法单击它们,它们已被禁用。

有人有什么想法吗?

    Try
        strQuery = "SELECT BookingDate, BookingTime,BookRegUserID,Booked FROM bookings"

        MySQLCmd = New MySqlCommand(strQuery, dbCon)

        dbCon.Open()

        DR = MySQLCmd.ExecuteReader

        While DR.Read

            bookDate = DR.Item("BookingDate")
            bookTime = DR.Item("BookingTime")
            bookRegID = DR.Item("BookRegUserID")
            booked = DR.Item("Booked")

            Select Case True

                Case bookDate = lblMonday.Text And    CheckBoxListMon.Items.FindByValue(test) IsNot Nothing

                    CheckBoxListMon.Items.FindByValue(bookTime).Enabled = False
                    CheckBoxListMon.Items.FindByValue(bookTime).Selected = True
                    CheckBoxListMon.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")


                Case bookDate = lblTuesday.Text And CheckBoxListTue.Items.FindByValue(test) IsNot Nothing

                    CheckBoxListTue.Items.FindByValue(bookTime).Enabled = False
                    CheckBoxListTue.Items.FindByValue(bookTime).Selected = True
                    CheckBoxListTue.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")
        End While
        DR.Close()
        dbCon.Close()
    Catch ex As Exception

    End Try
4

3 回答 3

1

我认为我现在看到了问题。如果它们不匹配,您不想将组合框翻转回来。假设您的结果集中返回了星期一星期二。第一行将星期一翻转为真,星期二翻转为假,第二行将星期一设置为假-我认为您不希望这样做-并将星期二设置为真,一切都由最后一行决定。答案是在处理行之前将它们全部设置为 false,并且只有在匹配时才设置为 true,否则忽略它。

   CbMon10.BackColor = Drawing.Color.White
    CbMon10.Checked = False
    CbMon10.Enabled = True
    CbTue10.BackColor = Drawing.Color.White
    CbTue10.Checked = False
    CbTue10.Enabled = True

而读博士

bookDate = DR.Item("BookingDate")
bookTime = DR.Item("BookingTime")
bookRegID = DR.Item("BookRegUserID")
booked = DR.Item("Booked")

    Select Case True
        Case bookDate = lblMonday.Text And bookTime = CbMon10.Text
            CbMon10.BackColor = Drawing.Color.Red
            CbMon10.Checked = True
            CbMon10.Enabled = False

        Case bookDate = lblTuesday.Text And bookTime = CbTue10.Text
            CbTue10.BackColor = Drawing.Color.Red
            CbTue10.Checked = True
            CbTue10.Enabled = False

    End Select

End While
于 2012-11-24T15:18:27.683 回答
0

你应该if-else-if像这样使用梯子:

 If bookDate = lblMonday.Text And bookTime = CbMon10.Text Then
        CbMon10.BackColor = Drawing.Color.Red
        CbMon10.Checked = True
        CbMon10.Enabled = False
 Else If bookDate = lblTuesday.Text And bookTime = CbTue10.Text Then
        CbTue10.BackColor = Drawing.Color.Red
        CbTue10.Checked = True
        CbTue10.Enabled = False
 Else
        CbTue10.BackColor = Drawing.Color.White
        CbTue10.Checked = False
        CbTue10.Enabled = True
 End If
于 2012-11-24T15:07:31.180 回答
0

而不是多个if,你可以像这样使用它: -

  If (bookDate = lblMonday.Text And bookTime = CbMon10.Text) or (bookDate = lblTuesday.Text And bookTime = CbTue10.Text) Then
            CbMon10.BackColor = Drawing.Color.Red
            CbMon10.Checked = True
            CbMon10.Enabled = False
        Else

            CbMon10.BackColor = Drawing.Color.White
            CbMon10.Checked = False
            CbMon10.Enabled = True
        End If
于 2012-11-24T15:07:32.397 回答