Programmi Visual Basic - prof. Claudio Maccherani - Perugia - 2009
gestione archivi RANDOM (ad accesso diretto) in Visual Basic 2008
applicazione
Alunni0Rnd
- archivio random
Alunni.dat
record
ALU
:
Matr
integer,
Nome
string
*
25,
Citta
string
*
20,
Sesso
string
*
1,
Classe
string
*
4
'+------------------------------------------------------------------------------------------+ '| Alunni0Rnd - applicazione VB2008, esempio di gestione Archivi RANDOM | '| (Prof.Claudio Maccherani) | '+------------------------------------------------------------------------------------------+ Imports VB = Microsoft.VisualBasic ' per usare alcune vecchie funzioni Visual Basic 6.0 Public Class frmAlunniRnd ' definisce la struttura del record (si usa VBFixedString perché VB2008 NON ha string*n) Structure RecAlu Dim Matr As Integer
Dim Nome As String
Dim Citta As String
Dim Sesso As String
Dim Classe As String End Structure Dim Alu As RecAlu Dim File As String = "Alunni.dat", MaxRec As Integer = 20, FilNum As Integer ' apre l'archivio al caricamento del form ----------------------------------------------- Private Sub frmArchRnd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load FilNum = FreeFile() FileOpen(FilNum, File, OpenMode.Random, , , Len(Alu)) Call Elenco() End Sub ' creazione iniziale dell'archivio (o suo azzeramento, se esiste) ----------------------- Private Sub cmdCrea_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCrea.Click If MsgBox("CREAZIONE ARCHIVIO: se esiste sarà azzerato. Continuo ?", vbYesNo) = vbYes Then Alu.Matr = 0 : Alu.Nome = "" : Alu.Citta = "" : Alu.Sesso = "" : Alu.Classe = "" For i = 1 To MaxRec : FilePut(FilNum, Alu, i) : Next MsgBox("Archivio creato") End If End Sub ' attiva la finestra di scrittura ------------------------------------------------------- Private Sub cmdInse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInse.Click Azzera() : cmdReg.Visible = True : cmdLeggi.Visible = False panMenu.Visible = False : boxRec.Visible = True End Sub ' azzeramento text box ------------------------------------------------------------------ Private Sub Azzera() TxtRec.Text = "" : TxtMat.Text = "" : txtNom.Text = "" : txtCit.Text = "" txtSex.Text = "" : txtCla.Text = "" End Sub ' chiude la finestra di gestione (e torna a quella di scelta) --------------------------- Private Sub cmdChiudi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChiudi.Click panMenu.Visible = True : boxRec.Visible = False End Sub ' registra un nuovo record -------------------------------------------------------------- Private Sub cmdReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReg.Click Dim rec As Integer, norec As Boolean : rec = 0 : norec = True If Len(Trim(TxtRec.Text)) > 0 Then rec = CInt(TxtRec.Text) If rec > 0 And rec < MaxRec Then norec = False If MsgBox("Confermi la registrazione del record numero " & rec & " ? ", vbYesNo) = vbYes Then Alu.Matr = CInt(TxtMat.Text) Alu.Nome = txtNom.Text Alu.Citta = txtCit.Text Alu.Sesso = txtSex.Text Alu.Classe = txtCla.Text FilePut(FilNum, Alu, rec) Call Elenco() MsgBox("Record registrato") End If End If End If If norec Then MsgBox("Numero Record NON valido !", MsgBoxStyle.Exclamation) End Sub ' fine prpgramma ------------------------------------------------------------------------ Private Sub cmdEsci_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEsci.Click FileClose(FilNum) : Me.Close() End Sub ' attiva la finestra di lettura --------------------------------------------------------- Private Sub cmdLettura_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLettura.Click Azzera() : cmdReg.Visible = False : cmdLeggi.Visible = True panMenu.Visible = False : boxRec.Visible = True End Sub ' legge il record specificato ----------------------------------------------------------- Private Sub cmdLeggi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLeggi.Click Dim rec As Integer, norec As Boolean : rec = 0 : norec = True If Len(Trim(TxtRec.Text)) > 0 Then rec = CInt(TxtRec.Text) If rec > 0 And rec < MaxRec Then FileGet(FilNum, Alu, rec) TxtMat.Text = Alu.Matr txtNom.Text = Alu.Nome txtCit.Text = Alu.Citta txtSex.Text = Alu.Sesso txtCla.Text = Alu.Classe norec = False End If End If If norec Then MsgBox("Numero Record NON valido !", MsgBoxStyle.Exclamation) End Sub ' visualizza sulla list box l'elenco degli studenti ------------------------------------- Private Sub Elenco() Dim riga As String : lstA.Items.Clear() lstA.Items.Add("n.rec Matr Nome" & Space(16) & " Città" & Space(11) & "Sesso Classe") lstA.Items.Add("---------------------------------------------------------------------") Alu.Matr = 0 : Alu.Nome = "" : Alu.Citta = "" : Alu.Sesso = "" : Alu.Classe = "" For i = 1 To MaxRec FileGet(FilNum, Alu, i) If Alu.Matr > 0 Then riga = " " & VB.Right(Space(2) & i, 2) & " " & _ VB.Right(Space(4) & Alu.Matr, 4) & " " & _ VB.Left(Alu.Nome & Space(20), 20) & " " & _ VB.Left(Alu.Citta & Space(20), 15) & " " & Alu.Sesso & " " & Alu.Classe lstA.Items.Add(riga) End If Next End Sub End Class