不實作控制項,用程式自動產生控制項陣列範例

範例是以日曆的方式來呈現 button array 的做法和事件的使用方式。


Public Class frmCalendarByPaul
    Dim btnDoSomeThing As Button
    Dim cmbWeek1th As New ComboBox
    Dim pnlDays As New Panel
    Dim txtMon As New TextBox
    Dim txtYear As New TextBox
    Dim btnArrowDown As New Button
    Dim btnarrowUP As New Button
    Private Sub frmCalendarByPaul_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Me
            .Size = New Size(375, 500)
            .WindowState = FormWindowState.Normal
            .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
            .ControlBox = False
        End With
        With btnArrowDown
            .Text = "《"
            ObjStyle(btnArrowDown, 10, 15, 30, 30)
        End With
        Me.Controls.Add(btnArrowDown)
        AddHandler btnArrowDown.Click, AddressOf btnArrowDown_Click
        With btnarrowUP
            .Text = "》"
            ObjStyle(btnarrowUP, 325, 15, 30, 30)
        End With
        Me.Controls.Add(btnarrowUP)
        AddHandler btnarrowUP.Click, AddressOf btnarrowUP_Click
        With txtMon
            .Text = Month(Now)
            .TextAlign = HorizontalAlignment.Right
            ObjStyle(txtMon, 140, 15, 32, 30)
        End With
        Me.Controls.Add(txtMon)
        AddHandler txtMon.TextChanged, AddressOf txtMon_TextChanged
        With txtYear
            .Text = Year(Now)
            .TextAlign = HorizontalAlignment.Right
            ObjStyle(txtYear, 177, 15, 50, 30)
        End With
        Me.Controls.Add(txtYear)
        AddHandler txtYear.TextChanged, AddressOf txtYear_TextChanged
        ObjStyle(pnlDays, 0, 50, Me.Width, Me.Height)
        Me.Controls.Add(pnlDays)
        JustNewOther()
        JustNewDay(1)
        cmbWeek1th.SelectedIndex = 0
    End Sub
    Sub btnArrowDown_Click(ByVal sender As Object, ByVal e As EventArgs)
        If txtYear.Text > 0 AndAlso txtMon.Text = 1 Then txtMon.Text = 12 : txtYear.Text -= 1 : Exit Sub
        If txtMon.Text >= 1 Then txtMon.Text -= 1
    End Sub
    Sub btnarrowUP_Click(ByVal sender As Object, ByVal e As EventArgs)
        If txtMon.Text = 12 Then txtMon.Text = 1 : txtYear.Text += 1 : Exit Sub
        If txtMon.Text <= 12 Then txtMon.Text += 1
    End Sub
    Sub txtMon_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        If txtMon.Text <> Nothing AndAlso (txtMon.Text > 12 Or txtMon.Text <= 0) Then Exit Sub
        ReDaysText()
    End Sub
    Sub ReDaysText()
        For i As Integer = 0 To pnlDays.Controls.Count - 1
            With pnlDays.Controls(i)
                If Mid(.Name, 1, 7) = "btnDays" Then _
                    JustReName(i, cmbWeek1th.SelectedIndex + 1) : Exit For
            End With
        Next
    End Sub
    Sub txtYear_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        ReDaysText()
    End Sub
    Sub JustNewOther()
        Dim lblweek1th As New Label
        With lblweek1th
            .Text = "每星期第一天的設定"
            ObjStyle(lblweek1th, 10, 12, 200, 30)
        End With
        pnlDays.Controls.Add(lblweek1th)
        With cmbWeek1th
            .Items.Clear()
            ObjStyle(cmbWeek1th, 210, 10, 45, 45)
        End With
        pnlDays.Controls.Add(cmbWeek1th)
        AddHandler cmbWeek1th.SelectedIndexChanged, AddressOf cmbWeek1th_SelectedIndexChanged
    End Sub
    Sub ObjStyle(ByVal tmpObj As Object, ByVal tmpX As Int32, ByVal tmpY As Int32, ByVal tmpW As Int32, ByVal tmpH As Int32)
        With tmpObj
            .Location = New Point(tmpX, tmpY)
            .Size = New Size(tmpW, tmpH)
            .Font = New Font("Arial", 14)
            .BackColor = Color.FromKnownColor(KnownColor.ControlLightLight)
        End With
    End Sub
    Sub JustNewDay(ByVal tmpFirstDay As UInt16)
        For i As Integer = 0 To 48
            btnDoSomeThing = New Button
            With btnDoSomeThing
                .Name = "btnDays" & i + 1
                ObjStyle(btnDoSomeThing, 10 + (i Mod 7) * 50, 50 + (i \ 7) * 50, 45, 45)
                If i <= 6 Then
                    .Text = Microsoft.VisualBasic.Right(WeekdayName(i + 1, True, tmpFirstDay), 1)
                    If cmbWeek1th.Items.Count <= 6 Then cmbWeek1th.Items.Add(.Text)
                End If
            End With
            pnlDays.Controls.Add(btnDoSomeThing)
            AddHandler btnDoSomeThing.Click, AddressOf btnDoSomeThing_Click
        Next
    End Sub
    Sub JustReName(ByVal tmpIndex As UInt32, ByVal tmpFirstDay As UInt16)
        If txtYear.Text = Nothing Or txtMon.Text = Nothing Then Exit Sub
        Dim tmpBeginDay As Date = txtYear.Text & "-" & txtMon.Text & "-01"
        Dim tmpi As Integer = Weekday(tmpBeginDay, tmpFirstDay)
        Dim tmpLastDay As Integer = Microsoft.VisualBasic.DateAndTime.Day(Date.Parse( _
                                    tmpBeginDay.AddMonths(1).ToString("yyyy-MM") & "-01").AddDays(-1))
        For i As Integer = tmpIndex To tmpIndex + 6
            With pnlDays.Controls(i)
                .Text = Microsoft.VisualBasic.Right(WeekdayName(i - tmpIndex + 1, True, tmpFirstDay), 1)
                If cmbWeek1th.Items.Count <= 6 Then cmbWeek1th.Items.Add(.Text)
            End With
        Next
        Dim tmpExit As Integer = Nothing
        For i As Integer = tmpIndex + 7 To tmpIndex + 48
            With pnlDays.Controls(i)
                .Visible = False
                If tmpi > 1 Then
                    tmpi -= 1
                Else
                    If tmpExit < tmpLastDay Then
                        .Text = tmpExit + 1
                        .Visible = True
                        tmpExit += 1
                        If i - 1 <= 42 Then
                            Me.Height = 450
                        Else
                            Me.Height = 500
                        End If
                    End If
                End If
            End With
        Next
    End Sub
    Sub btnDoSomeThing_Click(ByVal sender As Object, ByVal e As MouseEventArgs)
        With CType((sender), Button)
            If .BackColor = Color.Red Then
                .BackColor = Color.FromKnownColor(KnownColor.ControlLightLight)
            Else
                .BackColor = Color.Red
            End If
        End With
    End Sub
    Sub cmbWeek1th_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        ReDaysText()
    End Sub
End Class

沒有留言:

張貼留言