MaylorTaylor
MaylorTaylor

Reputation: 5041

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?

Upvotes: 0

Views: 1384

Answers (3)

Felix Hidalgo
Felix Hidalgo

Reputation: 25

Yes your problem is that you are nesting your if statements, which is why Thursday gets skipped because Wednesday proved to be false.

What you need to do is run a for loop that will go through each check box and check whether its checked value is true.

Upvotes: 0

Timothy Groote
Timothy Groote

Reputation: 8653

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.

Upvotes: 3

Lithu T.V
Lithu T.V

Reputation: 20021

do not use nesting

just simple if loop is needed here

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 ','

Upvotes: 1

Related Questions