Option Strict Off
Imports System.Data.SqlClient
Public Class 匯率管理
    Dim ds As New DataSet : Dim ds2 As DataSet
    Private Sub Set_grid()
        歷史匯率_dgv.Columns(0).FillWeight = 60 : 歷史匯率_dgv.Columns(1).FillWeight = 60
        歷史匯率_dgv.Columns(2).FillWeight = 60 : 歷史匯率_dgv.Columns(3).FillWeight = 60
        歷史匯率_dgv.Columns(4).FillWeight = 60 : 歷史匯率_dgv.Columns(5).FillWeight = 60
    End Sub
    Private Sub Set_日期()
        年_tb.Text = Format(DateTimePicker1.Value, "yyyy") : 月_tb.Text = Format(DateTimePicker1.Value, "MM")
    End Sub
    Private Sub Set_歷史匯率清單()
        歷史匯率_dgv.DataSource = Nothing : ds.Clear()
        歷史匯率_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        歷史匯率_dgv.ColumnHeadersHeight = 25
        歷史匯率_dgv.AllowUserToAddRows = False
        SQL_匯率管理_歷史匯率清單()
        da.Fill(ds) : 歷史匯率_dgv.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 歷史匯率_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            買進匯率_tb.Text = 歷史匯率_dgv(2, e.RowIndex).Value.ToString
            賣出匯率_tb.Text = 歷史匯率_dgv(3, e.RowIndex).Value.ToString
            目前匯率_tb.Text = 歷史匯率_dgv(4, e.RowIndex).Value.ToString
            美金匯率_tb.Text = 歷史匯率_dgv(5, e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub Set_日期寫入()
        Set_日期()
        SQL_匯率管理_查詢指定匯率()
        If dr.Read() Then
            SQL_匯率管理_修改匯率() : conn.Close()
        Else
            SQL_匯率管理_新增匯率() : conn.Close()
        End If
        conn.Close()
        MsgBox("完成")
    End Sub
    Private Sub 匯率管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DateTimePicker1.Value = Today
        SQL_匯率管理_查詢人民幣匯率()
        If dr.Read() Then : 目前匯率_tb.Text = dr("匯率").ToString : End If
        If 目前匯率_tb.Text = "" Then : 目前匯率_tb.Text = "0.00" : End If
        conn.Close() : dr.Close()
        SQL_匯率管理_查詢指定匯率()
        If dr.Read() Then : 買進匯率_tb.Text = dr("買進匯率").ToString : 賣出匯率_tb.Text = dr("賣出匯率").ToString : 美金匯率_tb.Text = dr("美金匯率").ToString : End If
        If 目前匯率_tb.Text = "" Then : 目前匯率_tb.Text = "0.00" : End If
        conn.Close() : dr.Close()
        Set_歷史匯率清單() : Set_grid() : Set_日期()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 修改_bt.Click
        目前匯率_tb.Enabled = True : 買進匯率_tb.Enabled = True : 賣出匯率_tb.Enabled = True : 美金匯率_tb.Enabled = True : 儲存修改_bt.Enabled = True : 取消_bt.Enabled = True
        目前匯率_tb.Text = "" : 買進匯率_tb.Text = "" : 賣出匯率_tb.Text = "" : 美金匯率_tb.Text = ""
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 取消_bt.Click
        目前匯率_tb.Enabled = False : 買進匯率_tb.Enabled = False : 賣出匯率_tb.Enabled = False : 美金匯率_tb.Enabled = False : 儲存修改_bt.Enabled = False : 取消_bt.Enabled = False
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 儲存修改_bt.Click
        If 目前匯率_tb.Text = "" Or 買進匯率_tb.Text = "" Or 賣出匯率_tb.Text = "" Then
            MsgBox("輸入資料有誤")
        Else
            If 寫入歷史資料_cb.Checked = False Then
                DateTimePicker1.Value = Today
                Set_日期寫入()
                SQL_匯率管理_修改人民幣匯率()
                conn.Close() : dr.Close()
            Else
                Set_日期寫入()
            End If
        End If
        目前匯率_tb.Enabled = False : 買進匯率_tb.Enabled = False : 賣出匯率_tb.Enabled = False : 美金匯率_tb.Enabled = False : 儲存修改_bt.Enabled = False : 取消_bt.Enabled = False
        Set_歷史匯率清單() : Set_grid() : Set_日期()
    End Sub
    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles 買進匯率_tb.TextChanged
        If 買進匯率_tb.Text <> "" And 賣出匯率_tb.Text <> "" Then
            目前匯率_tb.Text = (Val(買進匯率_tb.Text) + Val(賣出匯率_tb.Text)) / 2
        End If
    End Sub
    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles 賣出匯率_tb.TextChanged
        If 買進匯率_tb.Text <> "" And 賣出匯率_tb.Text <> "" Then
            目前匯率_tb.Text = (Val(買進匯率_tb.Text) + Val(賣出匯率_tb.Text)) / 2
        End If
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        歷史匯率_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
    End Sub
End Class