Readers frequently write in to ask this question (in various forms): "If VB.NET is so great, why is it so hard to write a program that will just maintain a simple list of things?"
It's actually a great question. Back in the bad old days, it seemed a lot easier to just read and write a sequential, comma delimited file. So I decided to see just how hard it would be to use the latest techniques to do the same thing. In this article, I use the fantastically powerful DataGridView control to maintain a simple "Contact List" and binary serialization to read and write the file in a Windows Forms application. It really is just a few lines of code - just a main processing class with only two subroutines and a class for the object that defines the columns of the grid. Here's what the result looks like:
Click Here to display the illustration
This simple application features all of the standard CRUD (Create-Read-Update-Delete) methods in a familiar database-like interface. When the system is opened, the grid is refreshed automatically from a binary serialized file and saved again when the form is closed. You could extend it to include a lot of features easily, but let's jump right into the code and see how it works. Here's the complete program:
The main star of the application is a DataGridView control that has already been added to the form. But other than giving it a new name and resizing it to fit the form, all of the properties are still set at their defaults. But the DataGridView gives this application most of it's features.
Imports System.ComponentModel Imports System.IO Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Binary Public Class Form1 Dim Contacts As New BindingList(Of Contact) Private Sub Form1_Load( ByVal sender As Object, ByVal e As System.EventArgs ) Handles Me.Load My.Computer.FileSystem.CurrentDirectory = "G:/ContactList" If My.Computer.FileSystem.FileExists("ContactList.bin") Then Using str As Stream = System.IO.File.OpenRead("ContactList.bin") Dim bf As New BinaryFormatter Contacts = bf.Deserialize(str) End Using End If DataGridView1.DataSource = Contacts End Sub Private Sub Form1_FormClosed( ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs ) Handles Me.FormClosed My.Computer.FileSystem.CurrentDirectory = "G:/ContactList" Using str As Stream = System.IO.File.OpenWrite("ContactList.bin") Dim bf As New BinaryFormatter bf.Serialize(str, Contacts) End Using End SubEnd Class <Serializable()> Public Class Contact Public Property LastName As String Public Property FirstName As String Public Property PhoneNum As String Public Property Email As String End Class
Contacts, a BindingList object, is declared at the beginning. This object is a generic type and in this case is declared to be of type Contact - the name of the class that defines the four "fields" that are displayed in the form. These fields are actually properties in the Contact object. BindingList is used so the DataGridView control will update itself whenever the list changes. A List(Of T) object won't do that.
After the Contacts object is declared, the code loads up the grid with the data from a binary serialized file. Because the program checks for the file's existance, you can "start over" by just deleting the file. The FormClosed event sub will recreate it. Binary serialization is a good choice if you're never going to use the file for anything else. If the file might be useful in some other program, you might be better off going with XML serialization. You can read more about serialization in the article, All About Serializing in Visual Basic.
Finally, the grid is bound to the Contacts object. DataGridView is smart enough to use the property names from the Contact object automatically as column headings.
The only other sub in the program just reverses the process and serializes the data in Contacts to the file again. I hardcoded the file location for simplicity ... and because I was planning on using this program in an in-person class I'm teaching. That way, I can keep it on a thumbdrive rather than the classroom computer.
The only other detail of note is that the Contact object must be decorated with the <Serializable> attribute to make things work.