Option Explicit On 

Imports System.Drawing
Imports System.IO
Imports System.Globalization
Imports System.Xml
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Panel2 As System.Windows.Forms.Panel
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Panel2 = New System.Windows.Forms.Panel
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Label2 = New System.Windows.Forms.Label
        Me.Button3 = New System.Windows.Forms.Button
        Me.PictureBox2 = New System.Windows.Forms.PictureBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Button4 = New System.Windows.Forms.Button
        Me.Button5 = New System.Windows.Forms.Button
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(192, Byte), CType(192, Byte))
        Me.Panel1.Location = New System.Drawing.Point(8, 32)
        Me.Panel1.Size = New System.Drawing.Size(217, 32)
        '
        'Panel2
        '
        Me.Panel2.BackColor = System.Drawing.Color.Transparent
        Me.Panel2.Location = New System.Drawing.Point(8, 64)
        Me.Panel2.Size = New System.Drawing.Size(217, 136)
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Size = New System.Drawing.Size(100, 16)
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(152, 8)
        Me.Button1.Size = New System.Drawing.Size(32, 20)
        Me.Button1.Text = "<<"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(192, 8)
        Me.Button2.Size = New System.Drawing.Size(32, 20)
        Me.Button2.Text = ">>"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold)
        Me.Label2.Location = New System.Drawing.Point(8, 200)
        Me.Label2.Size = New System.Drawing.Size(232, 16)
        '
        'Button3
        '
        Me.Button3.Enabled = False
        Me.Button3.Location = New System.Drawing.Point(8, 272)
        Me.Button3.Size = New System.Drawing.Size(40, 24)
        Me.Button3.Text = "予定"
        '
        'PictureBox2
        '
        Me.PictureBox2.Location = New System.Drawing.Point(80, 296)
        Me.PictureBox2.Size = New System.Drawing.Size(32, 8)
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Regular)
        Me.Label3.Location = New System.Drawing.Point(136, 288)
        Me.Label3.Size = New System.Drawing.Size(96, 16)
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.TopCenter
        Me.Label3.Visible = False
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(152, 216)
        Me.PictureBox1.Size = New System.Drawing.Size(70, 70)
        Me.PictureBox1.Visible = False
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(56, 272)
        Me.Button4.Size = New System.Drawing.Size(40, 24)
        Me.Button4.Text = "削除"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(104, 272)
        Me.Button5.Size = New System.Drawing.Size(40, 24)
        Me.Button5.Text = "現在"
        '
        'Form1
        '
        Me.ClientSize = New System.Drawing.Size(242, 312)
        Me.Controls.Add(Me.Button5)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.PictureBox2)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Panel2)
        Me.Controls.Add(Me.Panel1)
        Me.Text = "カレンダー予定登録画面"

    End Sub

#End Region
    Dim myButton As Button
    Dim beforeMonthButton As Button
    Dim nextMonthButton As Button

    Dim dummyCheck As Integer
    Dim nowMonth As Integer
    Dim yearCheck As Integer
    Dim check As Integer = 0
    Dim check2 As Integer = 0
    Dim checkCheck As Integer

    Dim readNen, readTuki, readHi As Integer
    Dim nenCheck As Integer
    Dim monthCheck As Integer
    Dim hiCheck As Integer

    Dim nowYearCheck As Integer
    Dim nowMonthCheck As Integer
    Dim nowHiCheck As Integer
    Dim allNo As Integer

    Dim d As DateTime

    Dim xReader As XmlTextReader

    Dim imageArray As ArrayList
    Dim nenArray As ArrayList
    Dim tukiArray As ArrayList
    Dim hiArray As ArrayList
    Dim titleArray As ArrayList

    Dim selectednenTukiHi As String
    Dim clickDate As String
    Dim clickDate2 As String

    Dim myImage As Image

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        Dim youbi() As String = New String(6) {"日", "月", "火", "水", "木", "金", "土"}
        Dim myLabel As Label
        Dim no As Integer = 0

        d = Now
        For i As Integer = 0 To 6
            myLabel = New Label
            With myLabel
                .Location = New Point(no, 10)
                .Size = New Size(30, 20)
                .Text = youbi(i)
                .Font = New Font("ＭＳ Ｐゴシック", "10", FontStyle.Bold)
                .TextAlign = ContentAlignment.TopCenter
            End With
            no = no + 31
            With Panel1
                .Controls.Add(myLabel)
            End With
            If youbi(i) = "日" Then myLabel.ForeColor = Color.Red
        Next

        If File.Exists("\Program Files\Rainman_calendar\data\monthInfo.xml") = True And nenTukiCheck = 2525 Then
            myYear = nen
            myMonth = tuki
        Else
            myYear = d.Year
            myMonth = d.Month
        End If
        xReader = Nothing

        imageArray = New ArrayList
        nenArray = New ArrayList
        tukiArray = New ArrayList
        hiArray = New ArrayList
        titleArray = New ArrayList

        If File.Exists("\Program Files\Rainman_calendar\data\calendarInfo.xml") = True Then
            xReader = New XmlTextReader("\Program Files\Rainman_calendar\data\calendarInfo.xml")

            While xReader.Read

                If xReader.IsStartElement("画像名") = True Then
                    imageArray.Add(xReader.ReadElementString)
                End If

                If xReader.IsStartElement("年") = True Then
                    nenArray.Add(xReader.ReadElementString)
                End If

                If xReader.IsStartElement("月") = True Then
                    tukiArray.Add(xReader.ReadElementString)
                End If

                If xReader.IsStartElement("日") = True Then
                    hiArray.Add(xReader.ReadElementString)
                End If


                If xReader.IsStartElement("タイトル") = True Then
                    titleArray.Add(xReader.ReadElementString)
                End If
            End While
            xReader.Close()

        Else
            Button4.Enabled = False
        End If
        With Button4
            .BackColor = Color.Crimson
            .ForeColor = Color.Yellow
        End With

        With Button3
            .BackColor = Color.Navy
            .ForeColor = Color.White
        End With

        DateShow()
        WarekiShow()
    End Sub

    Sub DateShow()
        Panel2.Controls.Clear()
        Dim buttonNo As Integer = 0
        Dim buttonHeight As Integer = 0



        If nenTukiCheck = 2525 Then
            myYear = nen
            myMonth = tuki
        End If

        Label1.Text = myYear & "年" & myMonth & "月"

        Dim nenTukiHi As Date = CType(myYear & "/" & myMonth & "/1", Date)
        Dim myWeek As Integer = Weekday(nenTukiHi)
        'Dim check As Integer
        'Dim check2 As Integer

        Dim beginDay As Integer = myWeek - 1
        Dim LastDay As Integer

        Select Case myMonth
            Case 1, 3, 5, 7, 8, 10, 12
                LastDay = 31
            Case 4, 6, 9, 11
                LastDay = 30
            Case 2
                yearCheck = myYear Mod 4
                If yearCheck = 0 Then
                    LastDay = 29
                Else
                    LastDay = 28
                End If
            Case Else
                Exit Sub
        End Select

        '当月を表示する
        For i As Integer = 1 To LastDay '1 To 42
            myButton = New Button
            check = CInt(Int((i + 6 + beginDay) / 7)) '行番号

            check2 = (i + 6 + beginDay) Mod 7

            buttonNo = check2 * 31
            buttonHeight = (check * 21) - 21

            With myButton
                .BackColor = Color.White
                .Size = New Size(30, 20)
                .Location = New Point(buttonNo, buttonHeight)
                .Text = i.ToString
                .Font = New Font("ＭＳ Ｐゴシック", "11", FontStyle.Regular)
            End With


            If File.Exists("\Program Files\Rainman_calendar\data\calendarInfo.xml") = True Then


                '予定が記録されている日の日付けを赤色に背景色をGOLDにする(当月)
                For f As Integer = 0 To imageArray.Count - 1

                    '現在の日付の背景色と文字色を変える
                    nowYearCheck = String.Compare(nenArray(f), d.Year)
                    nowMonthCheck = String.Compare(myMonth, d.Month)
                    nowHiCheck = String.Compare(i, d.Day)

                    If nowMonthCheck = 0 And nowHiCheck = 0 Then
                        myButton.ForeColor = Color.Navy
                        myButton.BackColor = Color.Pink
                        myButton.Font = New Font("Arial Black", 12, FontStyle.Bold)
                    End If

                    '予定の記録されている日の背景色と文字色を変える
                    nenCheck = String.Compare(nenArray(f), myYear)
                    monthCheck = String.Compare(tukiArray(f), myMonth)
                    hiCheck = String.Compare(hiArray(f), myButton.Text)

                    If nenCheck = 0 And monthCheck = 0 And hiCheck = 0 Then
                        myButton.ForeColor = Color.Red
                        myButton.BackColor = Color.Gold
                        myButton.Font = New Font("Arial Black", 12, FontStyle.Bold)
                    End If
                Next
                selectednenTukiHi = myYear.ToString & "/" & myMonth.ToString & "/" & myButton.Text
            End If

            With Panel2
                .Controls.Add(myButton)
            End With

            AddHandler myButton.Click, AddressOf Me.myButton_Click
        Next


        '********************//前月を表示させる//************************

        Select Case beginDay
            Case 1, 2, 3, 4, 5, 6
                Dim beforeMonth As Integer = myMonth - 1
                Dim beforeMonthDay As Integer

                Select Case beforeMonth
                    Case 0, 1, 3, 5, 7, 8, 10
                        beforeMonthDay = 31
                    Case 4, 6, 9, 11
                        beforeMonthDay = 30
                    Case 2
                        yearCheck = myYear Mod 4
                        If yearCheck = 0 Then
                            beforeMonthDay = 29
                        Else
                            beforeMonthDay = 28
                        End If
                    Case Else
                        Exit Sub
                End Select

                Dim displayBefore As Integer
                displayBefore = beforeMonthDay - (beginDay - 1)


                For i As Integer = displayBefore To beforeMonthDay
                    beforeMonthButton = New Button
                    buttonNo = (i - displayBefore) * 31
                    buttonHeight = 0

                    With beforeMonthButton
                        .BackColor = Color.Gainsboro
                        .Enabled = False
                        .ForeColor = Color.Blue
                        .Size = New Size(30, 20)
                        .Location = New Point(buttonNo, buttonHeight)
                        .Text = i.ToString
                        .Font = New Font("ＭＳ Ｐゴシック", "11", FontStyle.Bold)
                    End With
                    With Panel2
                        .Controls.Add(beforeMonthButton)
                    End With
                Next
        End Select

        '******************//次月を表示させる//************************
        Dim nowMonthDay As Integer
        Select Case myMonth
            Case 1, 3, 5, 7, 8, 10, 12
                nowMonthDay = 31
            Case 4, 6, 9, 11
                nowMonthDay = 30
            Case 2
                yearCheck = myYear Mod 4
                If yearCheck = 0 Then
                    nowMonthDay = 29
                Else
                    nowMonthDay = 28
                End If
            Case Else
                Exit Sub
        End Select

        Dim overMonth As Integer = nowMonthDay + beginDay
        Dim nextMonthDay As Integer = 42 - overMonth
        Dim lastDayX As Integer = check2

        If lastDayX >= 0 Or lastDayX <= 5 Then
            For i As Integer = 1 To nextMonthDay
                nextMonthButton = New Button

                buttonNo = ((overMonth + i + 6) Mod 7) * 31
                Dim dummyHeight As Integer = CInt(Int((overMonth + i + 6) / 7))
                buttonHeight = dummyHeight * 21 - 21

                With nextMonthButton
                    .BackColor = Color.Silver
                    .ForeColor = Color.Navy
                    .Enabled = False
                    .Size = New Size(30, 20)
                    .Location = New Point(buttonNo, buttonHeight)
                    .Text = i.ToString
                    .Font = New Font("ＭＳ Ｐゴシック", "11", FontStyle.Bold)
                End With
                With Panel2
                    .Controls.Add(nextMonthButton)
                End With
            Next
        End If
    End Sub

    '表示されている当月をクリックした時の処理
    Sub myButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        Button3.Enabled = True
        Dim selectBtn As Button = CType(sender, Button)

        selectDate = CType(selectBtn.Text, Integer)

        'If yearCheck = 2 Then
        '    myYear = myYear - 1
        '    yearCheck = 0
        'ElseIf yearCheck = 1 Then
        '    myYear = myYear + 1
        '    yearCheck = 0
        'End If

        selectMonth = myMonth
        nowMonth = selectMonth
        selectMonthSave()
        PlanShow()
        checkCheck = 1
        WarekiShow()
    End Sub

    Sub selectMonthSave()
        Dim monthXml As XmlDocument = New XmlDocument
        monthXml.LoadXml("<?xml version='1.0'?><カレンダー />")
        Dim calendarYearElement As XmlElement = monthXml.CreateElement("選択年")
        Dim calendarYearText As XmlText = monthXml.CreateTextNode(myYear)
        calendarYearElement.AppendChild(calendarYearText)

        Dim calendarElement As XmlElement = monthXml.CreateElement("選択月")
        Dim calendarText As XmlText = monthXml.CreateTextNode(selectMonth)
        calendarElement.AppendChild(calendarText)

        monthXml.DocumentElement.AppendChild(calendarYearElement)
        monthXml.DocumentElement.AppendChild(calendarElement)
        monthXml.Save("\Program Files\Rainman_calendar\data\monthInfo.xml")
        monthXml = Nothing
    End Sub

    Sub PlanShow()

        If File.Exists("\Program Files\Rainman_calendar\data\calendarInfo.xml") = True Then


            Dim imageCheck As Integer
            clickDate = myYear & "/" & nowMonth & "/" & selectDate

            For i As Integer = 0 To imageArray.Count - 1
                clickDate2 = CType(nenArray(i), String) & "/" & CType(tukiArray(i), String) & "/" & CType(hiArray(i), String)

                imageCheck = String.Compare(clickDate, clickDate2)
                If imageCheck = 0 Then
                    ImageNo = i
                    dummyCheck = 2525
                    Button4.Enabled = True
                    Exit For
                Else
                    'Button4.Enabled = False
                    PictureBox1.Visible = False
                    Label3.Visible = False
                End If
            Next
            If dummyCheck = 2525 Then
                Button3.Enabled = False
                myImage = New Bitmap("\Program Files\Rainman_calendar\calendar_image\image70\" & CType(imageArray(ImageNo), String))

                PictureBox1.Visible = True
                PictureBox1.Image = myImage
                Label3.Visible = True
                Label3.Text = titleArray(ImageNo)
            Else
                Button3.Enabled = True
            End If
        End If
    End Sub

    '次月
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PictureBox1.Visible = False
        Label3.Visible = False
        myMonth = myMonth + 1
        nowMonth = myMonth
        If myMonth > 12 Then
            myYear = myYear + 1
            myMonth = 1
            nowMonth = 1
        End If
        Label2.Text = String.Empty
        DateShow()
    End Sub

    '前月
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Visible = False
        Label3.Visible = False
        myMonth = myMonth - 1
        nowMonth = myMonth
        If myMonth < 1 Then
            myYear = myYear - 1
            myMonth = 12
            nowMonth = 12
        End If
        Label2.Text = String.Empty
        DateShow()
    End Sub

    Sub WarekiShow()
        Dim myCulture As CultureInfo = New CultureInfo("ja-JP", True)
        Dim jaCalendar As JapaneseCalendar = New JapaneseCalendar
        myCulture.DateTimeFormat.Calendar = jaCalendar
        Dim myDate As DateTime
        Dim warekiText As String
        Label2.Visible = True
        If checkCheck = 1 Then
            If selectMonth = 0 Then selectMonth = 12
            'MsgBox(myYear & "--" & selectMonth & "--" & selectDate)
            myDate = New DateTime(myYear, selectMonth, selectDate)
            warekiText = myDate.ToString("gg yy年MM月dd日dddd", myCulture)
            'Else
            '    Exit Sub
            'myYear = d.Year
            'selectMonth = d.Month
            'selectDate = d.Day
            'myDate = New DateTime(myYear, selectMonth, selectDate)
        End If


        If dummyCheck = 2525 Then
            Label2.ForeColor = Color.Red
            Label2.Text = warekiText & "の予定です。"
        Else
            Label2.ForeColor = Color.Empty
            Label2.Text = warekiText
        End If

        dummyCheck = 0
        nenTukiCheck = 0
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button3.Enabled = False
        Dim myForm2 As yotei_Form2 = New yotei_Form2
        myForm2.Show()
        'Dim myForm1 As Form1 = New Form1
        'myForm1.Dispose()
    End Sub


    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        PictureBox1.Visible = False
        Label3.Visible = False
        Label2.Visible = False
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim delForm As deleteForm = New deleteForm
        delForm.Show()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        nenTukiCheck = 0
        check = 0
        d = Now
        myYear = d.Year
        myMonth = d.Month
        DateShow()
        WarekiShow()
    End Sub

End Class
