codice:Imports System.io Public Class frmUtenti Dim PercorsoOrigineImmagine As String 'Nomino una variabile che mi indicherà che la foto potrebbe essere stata cambiata. Dim SalvaFoto As Boolean 'Nomino una variabile che indica lo stato se è stata selezionata una foto oppure no Dim Foto As Boolean Sub Pulizia() txtID.Text = "" txtUsername.Text = "" PercorsoOrigineImmagine = "" SalvaFoto = False PictureBox1.BackgroundImage = Nothing End Sub Private Sub dgvUtenti_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvUtenti.CellDoubleClick Dim objConnection As New OleDb.OleDbConnection(DriveConfig) Dim DataReader As OleDb.OleDbDataReader Dim Cmd As New OleDb.OleDbCommand("", objConnection) Pulizia() objConnection.Open() GroupBox2.Text = "Modifica" 'Cerco l'utente selezionato. Cmd.CommandText = "select * from Utenti where ID = @ID" Cmd.Parameters.AddWithValue("@ID", dgvUtenti.Item(0, e.RowIndex).Value) DataReader = Cmd.ExecuteReader DataReader.Read() txtID.Text = DataReader("ID") txtUsername.Text = DataReader("Username") If DataReader("Avatar") = "no" Then PictureBox1.BackgroundImage = ImageList1.Images(0) Foto = False Else Using Img As Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory() & "/Users/" & Trim(txtUsername.Text) & "/Avatar.jpg") PictureBox1.BackgroundImage = Img.Clone() End Using Foto = True End If Cmd.Parameters.Clear() DataReader.Close() End Sub Private Sub sbtnNuovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sbtnNuovo.Click Dim objConnection As New OleDb.OleDbConnection(DriveConfig) Dim DataReader As OleDb.OleDbDataReader Dim Cmd As New OleDb.OleDbCommand("", objConnection) GroupBox2.Text = "Nuovo" Pulizia() objConnection.Open() 'Cerco l'ID da assegnare al nuovo elemento. Cmd.CommandText = "select * from contatore" DataReader = Cmd.ExecuteReader DataReader.Read() txtID.Text = DataReader("utenti") + 1 DataReader.Close() objConnection.Close() 'Visualizzo nella picturebox che conterrà la foto l'immagine di default che indica che non è stata inserita nessuna foto. PictureBox1.BackgroundImage = ImageList1.Images(0) End Sub Private Sub txtID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtID.KeyPress e.KeyChar = "" End Sub Private Sub btnFoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFoto.Click 'Visualizzazione finestra di dialogo per l'apertura dei file If ofdSelezionaImmagine.ShowDialog = Windows.Forms.DialogResult.OK Then 'Caricamento dell'immagine nel controllo PictureBox. Using Img As Image = Image.FromFile(ofdSelezionaImmagine.FileName) PictureBox1.BackgroundImage = Img.Clone() End Using PercorsoOrigineImmagine = ofdSelezionaImmagine.FileName 'Imposto lo stato della variabile Foto su True essendo stata selezionata una foto ed esco dalla routine Foto = True SalvaFoto = True End If Private Sub btnRimuovi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRimuovi.Click 'Se si è in modalità modifica significa che si stanno modificando le informazioni di un utente e quindi si stà apportando una modifica ad una foto. If GroupBox2.Text = "Modifica" Then SalvaFoto = True End If 'Visualizzo nella picturebox che conterrà la foto l'immagine di default che indica che non è stata inserita nessuna foto. PictureBox1.BackgroundImage = ImageList1.Images(0) PercorsoOrigineImmagine = "" 'Imposto la selezione dell'immagine sullo stato False essendo stata rimossa la foto. Foto = False End Sub Private Sub sbtnSalva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sbtnSalva.Click Dim objConnection As New OleDb.OleDbConnection(DriveConfig) Dim DataReader As OleDb.OleDbDataReader Dim Cmd As New OleDb.OleDbCommand("", objConnection) If txtUsername.Text = "" Then MessageBox.Show("Inserire un Username.", "Salvataggio non riuscito", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End If objConnection.Open() 'Controllo che non esista già un utente con lo stesso Username. If GroupBox2.Text = "Nuovo" Then Cmd.CommandText = "select * from Utenti where Username = @Username" Cmd.Parameters.AddWithValue("@Username", txtUsername.Text) ElseIf GroupBox2.Text = "Modifica" Then Cmd.CommandText = "select * from Utenti where Username = @Username and ID <> @ID" Cmd.Parameters.AddWithValue("@Username", txtUsername.Text) Cmd.Parameters.AddWithValue("@ID", txtID.Text) End If DataReader = Cmd.ExecuteReader If DataReader.HasRows = True Then MessageBox.Show("L'Username inserito è già utilizzato da un altro utente, scegliere un Username diverso.") Cmd.Parameters.Clear() DataReader.Close() objConnection.Close() Exit Sub End If Cmd.Parameters.Clear() DataReader.Close() objConnection.Close() If GroupBox2.Text = "Nuovo" Then SalvaUtente() ElseIf GroupBox2.Text = "Modifica" Then ModificaUtente() End If End Sub Sub SalvaUtente() Dim objConnection As New OleDb.OleDbConnection(DriveConfig) Dim DataReader As OleDb.OleDbDataReader Dim Cmd As New OleDb.OleDbCommand("", objConnection) objConnection.Open() 'assegno all'elemento l'ID disponibile in quel momento. Cmd.CommandText = "select * from Contatore" DataReader = Cmd.ExecuteReader DataReader.Read() txtID.Text = DataReader("Utenti") + 1 DataReader.Close() 'Aggiorno il contatore degli utenti. Cmd.CommandText = "update contatore set Utenti = @Utenti" Cmd.Parameters.AddWithValue("@Utenti", txtID.Text) Cmd.ExecuteNonQuery() Cmd.Parameters.Clear() 'Salvo l'elemento nel database. Cmd.CommandText = "insert into Utenti values(@ID, @Username, @Avatar)" Cmd.Parameters.AddWithValue("@ID", txtID.Text) Cmd.Parameters.AddWithValue("@Username", Trim(txtUsername.Text)) If Foto = True Then Cmd.Parameters.AddWithValue("@Avatar", "sì") Else Cmd.Parameters.AddWithValue("@Avatar", "no") End If Cmd.ExecuteNonQuery() Cmd.Parameters.Clear() objConnection.Close() 'Creo la cartella dell'utente. Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory() & "/Users/" & Trim(txtUsername.Text)) 'Copio la foto se è stata inserita. If Foto = True Then File.Copy(PercorsoOrigineImmagine, System.AppDomain.CurrentDomain.BaseDirectory() & "/Users/" & Trim(txtUsername.Text) & "/Avatar.jpg") End If MessageBox.Show("Salvataggio completato!", "Salvataggio", MessageBoxButtons.OK) GroupBox2.Text = "Modifica" End Sub Sub ModificaUtente() Dim objConnection, objconnection2 As New OleDb.OleDbConnection(DriveConfig) Dim DataReader As OleDb.OleDbDataReader Dim Cmd, cmd2 As New OleDb.OleDbCommand("", objConnection) objConnection.Open() 'Modifico l'elemento nel database. Cmd.CommandText = "update Utenti Username = @Username, Avatar = @Avatar where ID = @ID" Cmd.Parameters.AddWithValue("@Username", txtUsername.Text) If Foto = True Then Cmd.Parameters.AddWithValue("@Avatar", "sì") Else Cmd.Parameters.AddWithValue("@Avatar", "no") End If Cmd.Parameters.AddWithValue("@ID", txtID.Text) Cmd.ExecuteNonQuery() Cmd.Parameters.Clear() objConnection.Close() If SalvaFoto = True Then 'PictureBox1.BackgroundImage.Dispose() 'PictureBox1.BackgroundImage = Nothing 'Elimino la vecchia foto se esiste. If File.Exists(System.AppDomain.CurrentDomain.BaseDirectory() & "/Users/" & Trim(txtUsername.Text) & "/Avatar.jpg") Then File.Delete(System.AppDomain.CurrentDomain.BaseDirectory() & "/Users/" & Trim(txtUsername.Text) & "/Avatar.jpg") End If If Foto = True Then 'Copio la nuova foto inserita. File.Copy(PercorsoOrigineImmagine, System.AppDomain.CurrentDomain.BaseDirectory() & "/Users/" & Trim(txtUsername.Text) & "/Avatar.jpg", True) End If End If MessageBox.Show("Salvataggio completato!, "Salvataggio", MessageBoxButtons.OK) End Sub Private Sub txtUsername_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUsername.KeyPress 'Impedisco di inserire caratteri speciali che non possono essere contenuti nel nome di una cartella. If e.KeyChar = "\" Or e.KeyChar = "/" Or e.KeyChar = ":" Or e.KeyChar = "*" Or e.KeyChar = "?" Or e.KeyChar = ChrW(34) Or e.KeyChar = "<" Or e.KeyChar = ">" Or e.KeyChar = "|" Then e.KeyChar = "" MessageBox.Show("I caratteri speciali: \/:*?""<>| non sono consentiti nel campo Username.", "Carattere non valido", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End Sub End Class

Rispondi quotando
