Imports System.Configuration
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Diagnostics.Eventing

Public Class FormRicette
    'Dim con As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|\Ricette.accdb")
    Dim ConnDBA As New OleDbConnection(ConfigurationManager.ConnectionStrings("DBA_Recipt").ConnectionString)
    Dim dt As New DataTable

    Private Sub FormRicette_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBoxInsJoint.Items.Clear()
        ComboBoxInsJoint.Items.Add("STD-R")
        ComboBoxInsJoint.Items.Add("STD-L")
        ComboBoxInsJoint.Items.Add("BLOCK-P")
        ComboBoxInsJoint.Items.Add("BLOCK-Q")
        ComboBoxInsJoint.Items.Add("PLUMBING-P")
        ComboBoxInsJoint.Items.Add("PLUMBING-Q")
        ComboBoxInsJoint.Items.Add("SHORT-R")
        ComboBoxInsJoint.Items.Add("SHORT-L")
        ComboBoxInsJoint.Text = ""

        TextBoxInsCod.Text = ""
        TextBoxInsDesc.Text = ""
        TextBoxInsCoreH.Text = ""
        TextBoxInsCoreW.Text = ""
        TextBoxInsAux1.Text = "0"
        TextBoxInsAux2.Text = "0"
        TextBoxInsAux3.Text = "0"
        TextBoxInsAux4.Text = "0"

        UpdateDataGridMod()

    End Sub

    Private Sub UpdateDataGridMod()

        DataGridViewMod.DataSource = Nothing

        dt.Clear()
        dt.Rows.Clear()
        dt.Columns.Clear()

        Dim strquery As String
        ConnDBA.Open()
        strquery = "SELECT * FROM Modelli order by Codice asc"
        Dim QCommand As New OleDbCommand(strquery, ConnDBA)
        Dim InsRead = QCommand.ExecuteReader
        dt.Load(InsRead)
        ConnDBA.Close()

        DataGridViewMod.DataSource = dt
        DataGridViewMod.AutoResizeColumns()

    End Sub

    Private Sub RecordDelete()

        'Cancella record
        ConnDBA.Open()
        Dim strquery As String
        strquery = "DELETE FROM Modelli WHERE Codice = " & TextBoxInsCod.Text & ""
        Dim QCommand As New OleDbCommand(strquery, ConnDBA)
        Dim InsData = QCommand.ExecuteScalar
        ConnDBA.Close()

    End Sub

    Private Sub RecordInsert()

        'Inserisci record
        ConnDBA.Open()
        Dim strquery As String
        strquery = "INSERT INTO Modelli VALUES (" & TextBoxInsCod.Text & ",'" & TextBoxInsDesc.Text & "'," & TextBoxInsCoreH.Text & "," & TextBoxInsCoreW.Text & ",'" & ComboBoxInsJoint.Text & "'," & TextBoxInsAux1.Text & "," & TextBoxInsAux2.Text & "," & TextBoxInsAux3.Text & "," & TextBoxInsAux4.Text & ")"
        Dim QCommand As New OleDbCommand(strquery, ConnDBA)
        Dim InsData = QCommand.ExecuteScalar
        ConnDBA.Close()

    End Sub


    Private Sub TextBoxInsCod_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxInsCod.KeyPress

        ' Accetto solo l'immissione di numeri interi

        Dim KeyAscii As Short = Asc(e.KeyChar)

        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If

        If e.KeyChar = "0" And TextBoxInsCod.TextLength = 0 Then
            KeyAscii = 0
        End If

        e.KeyChar = Chr(KeyAscii)

    End Sub

    Private Sub TextBoxInsAux1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxInsAux1.KeyPress

        ' Accetto solo l'immissione di numeri interi

        Dim KeyAscii As Short = Asc(e.KeyChar)

        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If

        e.KeyChar = Chr(KeyAscii)

    End Sub

    Private Sub TextBoxInsAux2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxInsAux2.KeyPress

        ' Accetto solo l'immissione di numeri interi

        Dim KeyAscii As Short = Asc(e.KeyChar)

        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If

        e.KeyChar = Chr(KeyAscii)

    End Sub

    Private Sub TextBoxInsAux3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxInsAux3.KeyPress

        ' Accetto solo l'immissione di numeri interi

        Dim KeyAscii As Short = Asc(e.KeyChar)

        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If

        e.KeyChar = Chr(KeyAscii)

    End Sub

    Private Sub TextBoxInsAux4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxInsAux4.KeyPress

        ' Accetto solo l'immissione di numeri interi

        Dim KeyAscii As Short = Asc(e.KeyChar)

        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If

        e.KeyChar = Chr(KeyAscii)

    End Sub

    Private Sub TextBoxInsCoreH_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxInsCoreH.KeyPress

        Dim KeyAscii As Short = Asc(e.KeyChar)

        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 And e.KeyChar <> "." And e.KeyChar <> "," Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If

        If e.KeyChar = "," Then
            KeyAscii = 46 ' 46 è il codice ascii del punto
        End If

        If TextBoxInsCoreH.TextLength = 0 And KeyAscii = 46 Then
            KeyAscii = 0
        End If

        If (KeyAscii = 46) And
            TextBoxInsCoreH.Text.IndexOf(".") > 0 Then
            KeyAscii = 0
        End If

        If TextBoxInsCoreH.TextLength > 2 And TextBoxInsCoreH.Text.IndexOf(".") <= 0 And KeyAscii <> 46 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        End If

        e.KeyChar = Chr(KeyAscii)

    End Sub

    Private Sub TextBoxInsCoreW_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxInsCoreW.KeyPress

        Dim KeyAscii As Short = Asc(e.KeyChar)

        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 And e.KeyChar <> "." And e.KeyChar <> "," Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If

        If e.KeyChar = "," Then
            KeyAscii = 46 ' 46 è il codice ascii del punto
        End If

        If TextBoxInsCoreW.TextLength = 0 And KeyAscii = 46 Then
            KeyAscii = 0
        End If

        If (KeyAscii = 46) And
            TextBoxInsCoreW.Text.IndexOf(".") > 0 Then
            KeyAscii = 0
        End If

        If TextBoxInsCoreW.TextLength > 2 And TextBoxInsCoreW.Text.IndexOf(".") <= 0 And KeyAscii <> 46 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        End If

        e.KeyChar = Chr(KeyAscii)

    End Sub


    Private Sub ButtonSaveRecipt_Click(sender As Object, e As EventArgs) Handles ButtonSaveRecipt.Click

        If TextBoxInsCod.Text = "" Then
            MessageBox.Show("Compilare il campo Codice")
            Exit Sub
        End If
        If TextBoxInsDesc.Text = "" Then
            MessageBox.Show("Compilare il campo Descrizione")
            Exit Sub
        End If
        If TextBoxInsCoreH.Text = "" Then
            MessageBox.Show("Compilare il campo Core H")
            Exit Sub
        End If
        If TextBoxInsCoreW.Text = "" Then
            MessageBox.Show("Compilare il campo Core W")
            Exit Sub
        End If
        If ComboBoxInsJoint.Text = "" Then
            MessageBox.Show("Selezionare il tipo di giunto")
            Exit Sub
        End If

        If TextBoxInsAux1.Text = "" Then
            TextBoxInsAux1.Text = "0"
        End If
        If TextBoxInsAux2.Text = "" Then
            TextBoxInsAux2.Text = "0"
        End If
        If TextBoxInsAux3.Text = "" Then
            TextBoxInsAux3.Text = "0"
        End If
        If TextBoxInsAux4.Text = "" Then
            TextBoxInsAux4.Text = "0"
        End If

        RecordDelete()
        RecordInsert()
        UpdateDataGridMod()

    End Sub

    Private Sub ButtonDeleteRecipt_Click(sender As Object, e As EventArgs) Handles ButtonDeleteRecipt.Click

        If TextBoxInsCod.Text = "" Then
            MessageBox.Show("Compilare il campo Codice")
            Exit Sub
        End If

        RecordDelete()
        UpdateDataGridMod()

    End Sub

    Private Sub ButtonCloseFormRecipt_Click(sender As Object, e As EventArgs) Handles ButtonCloseFormRecipt.Click

        Select Case MsgBox("Per rendere effettive le modifiche occorre riavviare l'applicazione, vuoi farlo adesso?", MsgBoxStyle.YesNoCancel, "Chiusura Applicazione")
            Case MsgBoxResult.Yes
                Form1.CloseProcess()
                Application.Restart()
            Case MsgBoxResult.No
                Me.Close()
        End Select

    End Sub

End Class