Imports System.Data.OleDb

Public Class Frm_Lista
    Dim Funzione As String = ""
    Dim Fun_Nome As String = ""
    Dim Fun_Numero As String = ""
    Dim Fun_Posizione As String = ""

    Private Sub Frm_Lista_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    End Sub


    Private Sub Frm_Lista_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Aggiorna_Lista()
    End Sub

    Private Sub Aggiorna_Lista()
        Dim quanti As Integer
        Me.List_Numeri.Items.Clear()

        Conn_Crea(CONN)
        Conn_Percorso(CONN, Path_Database)
        quanti = RS_Visualizza(RS, CONN, "SELECT * FROM Utente")
    End Sub

    Public Function RS_Visualizza(ByVal Recordset As OleDbCommand, ByVal Connessione As OleDbConnection, ByVal STSQL As String) As Integer
        Dim quanti As Integer = 0
        Dim lista As ListViewItem
        ' Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(STSQL, Connessione)

        Connessione.Open()
        Dim reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            'Nome_Letto = reader.GetString(1)
            lista = Me.List_Numeri.Items.Add(reader.GetInt32(0).ToString())
            lista.SubItems.Add(reader.GetString(1))
            lista.SubItems.Add(reader.GetString(2))
            quanti += 1
            'MsgBox(reader.GetInt32(0).ToString() + ", " + reader.GetString(1) + "," + reader.GetString(2), MsgBoxStyle.OkOnly, "Dato DB")
        End While
        ' always call Close when done reading.
        reader.Close()

        Return quanti
    End Function

    Private Sub BT_Aggiorna_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Aggiorna.Click
        Aggiorna_Lista()
    End Sub

    Private Sub BT_Pulisci_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Pulisci.Click
        Me.List_Numeri.Items.Clear()
    End Sub

    Private Sub BT_Esci_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Esci.Click
        Me.Close()
    End Sub

    Private Sub Tool_Modifica_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Modifica.Click
        Funzione = "MODIFICA"
        Me.TXT_Nome.Text = ""
        Me.TXT_Numero.Text = ""
        Me.TXT_Nome.Enabled = True
        Me.TXT_Numero.Enabled = True
        Me.BT_Annulla.Enabled = True
        Me.BT_Salva.Enabled = True
        Fun_Nome = Me.List_Numeri.SelectedItems.Item(0).SubItems(1).Text
        Fun_Numero = Me.List_Numeri.SelectedItems.Item(0).SubItems(2).Text
        Me.TXT_Nome.Text = Fun_Nome
        Me.TXT_Numero.Text = Fun_Numero
    End Sub

    Private Sub Tool_Elimina_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Elimina.Click

        Funzione = "ELIMINA"
        Me.TXT_Nome.Text = ""
        Me.TXT_Numero.Text = ""
        Me.TXT_Nome.Enabled = False
        Me.TXT_Numero.Enabled = False
        Me.BT_Annulla.Enabled = False
        Me.BT_Salva.Enabled = False
        Fun_Posizione = Me.List_Numeri.SelectedItems.Item(0).SubItems(0).Text
        Fun_Numero = Me.List_Numeri.SelectedItems.Item(0).SubItems(2).Text
        If MsgBox("Sicuro di voelere eliminare il numero '" & Fun_Numero & "' dal database?" & Chr(13) & "N.B.: Il nominativo non potrà più essere recuperato!", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Elimina...") = MsgBoxResult.Yes Then
            BT_Salva_Click(sender, e)
        End If
    End Sub

    Private Sub Tool_Inserisci_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool_Inserisci.Click
        Funzione = "INSERISCI"
        Me.TXT_Nome.Text = ""
        Me.TXT_Numero.Text = ""
        Me.TXT_Nome.Enabled = True
        Me.TXT_Numero.Enabled = True
        Me.BT_Annulla.Enabled = True
        Me.BT_Salva.Enabled = True
    End Sub

    Private Sub List_Numeri_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles List_Numeri.ItemSelectionChanged
        If Me.List_Numeri.SelectedItems.Count = 0 Then
            Me.Tool_Elimina.Enabled = False
            Me.Tool_Modifica.Enabled = False
            Me.Tool_Inserisci.Enabled = True
            Me.TXT_Nome.Text = ""
            Me.TXT_Numero.Text = ""            
        Else
            Me.Tool_Elimina.Enabled = True
            Me.Tool_Modifica.Enabled = True
            Me.Tool_Inserisci.Enabled = True            
        End If
        Me.TXT_Nome.Text = ""
        Me.TXT_Numero.Text = ""
        Me.TXT_Nome.Enabled = False
        Me.TXT_Numero.Enabled = False
        Me.BT_Annulla.Enabled = False
        Me.BT_Salva.Enabled = False
    End Sub

    Private Sub BT_Annulla_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Annulla.Click
        Me.TXT_Nome.Text = ""
        Me.TXT_Numero.Text = ""
        Me.TXT_Nome.Enabled = False
        Me.TXT_Numero.Enabled = False
        Me.BT_Annulla.Enabled = False
        Me.BT_Salva.Enabled = False
        Funzione = ""
        Fun_Nome = ""
        Fun_Numero = ""
    End Sub

    Private Sub BT_Salva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Salva.Click
        Dim nome As String = ""
        Dim numero As String = ""
        Dim errore As Boolean = False
        Select Case Funzione
            Case "MODIFICA"
                nome = Me.TXT_Nome.Text
                numero = Me.TXT_Numero.Text
                Conn_Crea(CONN)
                Conn_Percorso(CONN, Path_Database)
                errore = RS_Aggiorna(RS, CONN, "UPDATE Utente SET Nominativo='" & nome & "', Numero='" & numero & "' WHERE Numero='" & Fun_Numero & "'")
            Case "INSERISCI"
                nome = Me.TXT_Nome.Text
                numero = Me.TXT_Numero.Text
                Conn_Crea(CONN)
                Conn_Percorso(CONN, Path_Database)
                errore = RS_Inserisci(RS, CONN, "INSERT INTO Utente (Nominativo, Numero) values ('" & nome & "','" & numero & "')")
            Case "ELIMINA"
                Conn_Crea(CONN)
                Conn_Percorso(CONN, Path_Database)
                errore = RS_Cancella(RS, CONN, "DELETE * FROM Utente WHERE ID=" & Val(Fun_Posizione))
        End Select
        Me.TXT_Nome.Text = ""
        Me.TXT_Numero.Text = ""
        Me.TXT_Nome.Enabled = False
        Me.TXT_Numero.Enabled = False
        Me.BT_Annulla.Enabled = False
        Me.BT_Salva.Enabled = False
        Funzione = ""
        Fun_Nome = ""
        Fun_Numero = ""
        Select Case errore
            Case False
                MsgBox("Operazione eseguita con successo.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "...")
            Case True
                MsgBox("Operazione non completata a causa di un errore.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "...")
        End Select
        Aggiorna_Lista()
    End Sub

    Private Sub List_Numeri_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_Numeri.SelectedIndexChanged

    End Sub
End Class