Search code examples
if-statementdayofweek

IF Statement for day of the week


I have a set of 7 checkboxes on my winForm that allow the user to select which days of the week they want assigned with the order being created. I am trying to create the IF Statement that implements these checkbox decisions correctly. I have tried many combination of If, IfElse, and Select statement but all to no avail.

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

That what i have so far that works the best. The problem though is that if i check "Monday, Tuesday, and Thursday" on the winForm...Monday and Tuesday will show up, but Thursday gets skipped because it obviously breaks out of the if statement. Any guidance?


Solution

  • The problem is that you should not be nesting your if statements. In your example, any portion of code will only hit if the day before it (all the way up to monday) is checked.

    Simply flatten out the if statements, and do not nest them, like so :

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

    ...etc...

    If you want the users to select only ONE option, perhaps a dropdown or radio button list is more suitable rather than checkboxes.