Ciao a tutti,come posso fare per salvare ogni volta che esco dall'applicazione glie elementi di una listbox(che variano sempre) per poi ricaricarli esattamente com'erano alla riapertura del form?
Grazie in anticipo,ciao ciao.
Ciao a tutti,come posso fare per salvare ogni volta che esco dall'applicazione glie elementi di una listbox(che variano sempre) per poi ricaricarli esattamente com'erano alla riapertura del form?
Grazie in anticipo,ciao ciao.
Puoi usare la serializzazione.
Amaro C++, il gusto pieno dell'undefined behavior.
E come funzione la serializzazione? potresti farmi un piccolo esempio?
Codice PHP:
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Module App
Sub Main()
Serialize()
Deserialize()
End Sub
Sub Serialize()
' Create a hashtable of values that will eventually be serialized.
Dim addresses As New Hashtable
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052")
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116")
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301")
' To serialize the hashtable (and its key/value pairs),
' you must first open a stream for writing.
' In this case, use a file stream.
Dim fs As New FileStream("DataFile.dat", FileMode.Create)
' Construct a BinaryFormatter and use it to serialize the data to the stream.
Dim formatter As New BinaryFormatter
Try
formatter.Serialize(fs, addresses)
Catch e As SerializationException
Console.WriteLine("Failed to serialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
End Sub
Sub Deserialize()
' Declare the hashtable reference.
Dim addresses As Hashtable = Nothing
' Open the file containing the data that you want to deserialize.
Dim fs As New FileStream("DataFile.dat", FileMode.Open)
Try
Dim formatter As New BinaryFormatter
' Deserialize the hashtable from the file and
' assign the reference to the local variable.
addresses = DirectCast(formatter.Deserialize(fs), Hashtable)
Catch e As SerializationException
Console.WriteLine("Failed to deserialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
' To prove that the table deserialized correctly,
' display the key/value pairs.
Dim de As DictionaryEntry
For Each de In addresses
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value)
Next
End Sub
End Module
Questo codice salva il contenuto della listbox di nome listbox1 in un file binario di nome listbox.bin usando la serializzazione. Il caricamento e il salvataggio vengono effettuati rispettivamente all'apertura e alla chiusura del form.
Questo metodo è il più generico possibile, per cui è possibile inserire nel listbox qualunque genere di oggetti, purché siano serializzabili.
codice:Private Sub Form_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Dim s As Stream Try s = File.Open("listbox.bin", FileMode.Create, FileAccess.ReadWrite) Catch ex As Exception MessageBox.Show(ex.ToString(), "Errore durante il salvataggio della listbox.", MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End Try Dim sf As New BinaryFormatter For Each i As Object In ListBox1.Items sf.Serialize(s, i) Next s.Close() End Sub Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim s As Stream Try s = File.Open("listbox.bin", FileMode.Open) Catch ex As FileNotFoundException 'Se non c'è il file non dice nulla Exit Sub Catch ex As Exception 'Altrimenti protesta sonoramente MessageBox.Show(ex.ToString(), "Errore durante il caricamento della listbox.", MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End Try Dim sf As New BinaryFormatter Do Try ListBox1.Items.Add(sf.Deserialize(s)) Catch ex As Exception Exit Do End Try Loop s.Close() End Sub
Amaro C++, il gusto pieno dell'undefined behavior.
One problem: file,filemode fileaccess dice che nn sono dichiarati...sotto quale categoria devo dichiararli? grazie x la risposta esaustiva.
Niente niente,ho risolto il problema...grazie tanto,ora funziona alla perfezione,bye bye alla prossiama.
Pardon, mi ero dimenticato gli Imports...Originariamente inviato da yandragon
One problem: file,filemode fileaccess dice che nn sono dichiarati...sotto quale categoria devo dichiararli? grazie x la risposta esaustiva.
codice:Imports System.IO Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Binary
Amaro C++, il gusto pieno dell'undefined behavior.