2016-12-29 64 views
0

所以我有这个程序在Visual Studio 2008中,让用户选择一个月和一年,然后动态更改所有复选框的颜色哪些文本对应于所选月份的星期六的日期和星期天。在运行时动态更改两个复选框ForeColor

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

我知道使用每个将让程序循环在Me.Controls每个控制,所以我不认为它会看不懂的第一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 

回答

0

所以这是我如何解决我自己的问题,让用户选择一个月,一年,计算天的第一个星期日再下去....

那是因为FOR EACH LOOP从您在控件组或控件上添加的最后一个控件开始,因此我使用了正常的FOR LOOP并直接转换对象,以便查看控件的名称,名称必须以数组的方式排列。像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 
相关问题