Vb.net 在运行时动态更改两个复选框ForeColor

Vb.net 在运行时动态更改两个复选框ForeColor,vb.net,Vb.net,因此,我在VisualStudio2008中有一个程序,它允许用户选择一个月和一年,然后动态更改所有复选框的颜色,这些文本对应于所选月份的周六和周日的日期 让我们在第一次运行时说“Let i=1”,即2017年1月的第一个星期日和第一天((UTC-08:00)太平洋时间(美国和加拿大)) 我知道使用for each会让程序循环到我的每个控件。控件,所以我不认为它不会读取第一个IF语句 If cb.Text = i-1 只有ELIF才能退出FOR循环 但是如果我把第一句话改成 i

因此,我在VisualStudio2008中有一个程序,它允许用户选择一个月和一年,然后动态更改所有复选框的颜色,这些文本对应于所选月份的周六和周日的日期

让我们在第一次运行时说“Let i=1”,即2017年1月的第一个星期日和第一天((UTC-08:00)太平洋时间(美国和加拿大))

我知道使用for each会让程序循环到我的每个控件。控件,所以我不认为它不会读取第一个IF语句

    If cb.Text = i-1
只有ELIF才能退出FOR循环

但是如果我把第一句话改成

    if cb.Text = i+1
成功了。有人请告诉我有什么问题吗。。我只是没有得到任何错误或警告,只是这个逻辑错误,我想

    Dim cb
        While i < 31
            For Each cb In Me.Controls
                If TypeOf cb Is CheckBox Then
                    If cb.Text = i - 1 Then
                        cb.ForeColor = Color.Blue
                    ElseIf cb.Text = i Then
                        cb.ForeColor = Color.Red
                        Exit For
                    End If
                End If
            Next
            i += 7
        End While
Dim cb
而我<31岁
对于Me.控件中的每个cb
如果cb的类型为复选框,则
如果cb.Text=i-1,则
cb.ForeColor=颜色。蓝色
ElseIf cb.Text=i然后
cb.ForeColor=颜色。红色
退出
如果结束
如果结束
下一个
i+=7
结束时

这就是我解决自己问题的方法,让用户选择一个月和一年,计算天数和第一个星期天,然后继续

这是因为FOR-EACH循环从您添加到控件组上的最后一个控件开始,或者以您的形式开始,所以我使用了normal FOR循环并直接强制转换对象,以便它查看控件的名称,名称必须以数组的方式排列。像c1,c2,c3

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    On Error Resume Next
    Dim nMonths As Integer = System.DateTime.DaysInMonth(ComboBox2.Text, ComboBox1.SelectedIndex + 1)
    Dim i As Integer = 1
    Dim x As Integer = 0
    Dim customDate As Date = FormatDateTime(CDate(ComboBox1.SelectedIndex + 1 & "/1/" & ComboBox2.Text), DateFormat.ShortDate)

    While customDate.DayOfWeek <> DayOfWeek.Sunday
        customDate = customDate.AddDays(1)
        i += 1
    End While
    x = i - 1

    Dim cb
    'set visible
    For n = 1 To nMonths
        cb = DirectCast(Me.Controls("chk" + n.ToString()), CheckBox)
        cb.Visible = True
    Next

    If nMonths = 31 Then
        'set sundays and saturdays checkboxes fore color
        While i <= 31
            Dim cb1 = DirectCast(Me.Controls("chk" + x.ToString()), CheckBox)
            cb1.ForeColor = Color.Blue
            Dim cb2 = DirectCast(Me.Controls("chk" + i.ToString()), CheckBox)
            cb2.ForeColor = Color.Red
            i += 7
            x += 7
        End While
    ElseIf nMonths = 30 Then
        'set sundays and saturdays checkboxes fore color
        While i <= 30
            Dim cb1 = DirectCast(Me.Controls("chk" + x.ToString()), CheckBox)
            cb1.ForeColor = Color.Blue
            Dim cb2 = DirectCast(Me.Controls("chk" + i.ToString()), CheckBox)
            cb2.ForeColor = Color.Red
            i += 7
            x += 7
        End While
    ElseIf nMonths = 29 Then
        'set sundays and saturdays checkboxes fore color
        While i <= 29
            Dim cb1 = DirectCast(Me.Controls("chk" + x.ToString()), CheckBox)
            cb1.ForeColor = Color.Blue 'satudays
            Dim cb2 = DirectCast(Me.Controls("chk" + i.ToString()), CheckBox)
            cb2.ForeColor = Color.Red 'sundays
            i += 7
            x += 7
        End While
    ElseIf nMonths = 28 Then
        'set sundays and saturdays checkboxes fore color
        While i <= 28
            Dim cb1 = DirectCast(Me.Controls("chk" + x.ToString()), CheckBox)
            cb1.ForeColor = Color.Blue
            Dim cb2 = DirectCast(Me.Controls("chk" + i.ToString()), CheckBox)
            cb2.ForeColor = Color.Red
            i += 7
            x += 7
        End While
    End If
End Sub
Private子Combox2\u SelectedIndexChanged(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理Combox2.SelectedIndexChanged
出错时继续下一步
Dim NMONTS作为整数=System.DateTime.DaysInMonth(ComboBox2.Text,ComboBox1.SelectedIndex+1)
尺寸i为整数=1
尺寸x为整数=0
Dim customDate As Date=FormatDateTime(CDate(ComboBox1.SelectedIndex+1&“/1/”&Combox2.Text)、DateFormat.ShortDate)
而customDate.DayOfWeek.DayOfWeek.Sunday
customDate=customDate.AddDays(1)
i+=1
结束时
x=i-1
暗断路器
“设置为可见
对于n=1到n个月
cb=DirectCast(Me.Controls(“chk”+n.ToString()),复选框)
cb.Visible=True
下一个
如果nMonths=31,则
'将星期日和星期六复选框设置为彩色
而我