If statement 一周中某一天的IF语句

If statement 一周中某一天的IF语句,if-statement,dayofweek,If Statement,Dayofweek,我的winForm上有一组7个复选框,允许用户选择一周中的哪几天分配创建的订单。我正在尝试创建正确实现这些复选框决策的IF语句。我尝试了许多If、IfElse和Select语句的组合,但都没有用 If cbMon.Checked = True Then .WriteString("Monday") If cbTues.Checked = True Then .WriteString("Tuesday")

我的winForm上有一组7个复选框,允许用户选择一周中的哪几天分配创建的订单。我正在尝试创建正确实现这些复选框决策的IF语句。我尝试了许多If、IfElse和Select语句的组合,但都没有用

If cbMon.Checked = True Then
            .WriteString("Monday")
            If cbTues.Checked = True Then
                .WriteString("Tuesday")
                If cbWed.Checked = True Then
                    .WriteString("Wednesday")
                    If cbThur.Checked = True Then
                        .WriteString("Thursday")
                        If cbFri.Checked = True Then
                            .WriteString("Friday")
                            If cbSat.Checked = True Then
                                .WriteString("Saturday")
                                If cbSun.Checked = True Then
                                    .WriteString("Sunday")
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
到目前为止,我所拥有的是最有效的。但问题是,如果我在winForm上勾选“周一、周二和周四”,周一和周二会出现,但周四会被跳过,因为它显然超出了if语句。有什么指导吗?

不要使用嵌套

如果这里需要循环,那么就简单了

Initialise an array

if monday
 add monday to array


if tuesday checked
 add tuesday to array

.
.
.
if sunday checked
 add sunday to array



get the string by append all values in array with ','
不要使用嵌套

如果这里需要循环,那么就简单了

Initialise an array

if monday
 add monday to array


if tuesday checked
 add tuesday to array

.
.
.
if sunday checked
 add sunday to array



get the string by append all values in array with ','

问题是您不应该嵌套if语句。 在您的示例中,只有在选中前一天(一直到周一)时,代码的任何部分才会命中

只需展平if语句,不要嵌套它们,如下所示:

If cbMon.Checked = True Then
            .WriteString("Monday")
End If

If cbTue.Checked = True Then
            .WriteString("Tuesday")
End If
……等等


如果您想让用户只选择一个选项,也许下拉列表或单选按钮列表比复选框更合适。

问题是您不应该嵌套If语句。 在您的示例中,只有在选中前一天(一直到周一)时,代码的任何部分才会命中

只需展平if语句,不要嵌套它们,如下所示:

If cbMon.Checked = True Then
            .WriteString("Monday")
End If

If cbTue.Checked = True Then
            .WriteString("Tuesday")
End If
……等等


如果您希望用户只选择一个选项,也许下拉列表或单选按钮列表比复选框更合适。

是的,您的问题是您嵌套了If语句,这就是为什么跳过周四,因为周三被证明是错误的


您需要做的是运行一个for循环,该循环将遍历每个复选框,并检查其选中值是否为true。

是的,您的问题是您正在嵌套if语句,这就是为什么跳过星期四,因为星期三被证明为false


您需要做的是运行一个for循环,该循环将遍历每个复选框,并检查其选中值是否为true。

工作正常,一定是因为睡眠不足。或是新手状态。谢天谢地,工作完美,一定是睡眠不足。或是新手状态。多谢