Adding Multiple Signature Blocks
As was the case in previous upgrades, this one is built as a modification of the previous version. To download the previous program, Click Here. And keep in mind that the completed version is downloadable at the end of the lesson. Since this is a fairly complex upgrade, you might want to download both and compare them in separate instances of VB.NET to understand the changes.
The previous version of the Signature Block program was easy to use and worked pretty well -- for just one signature block. But if you wanted to have several of them ... say, one for business and one for personal use ... then you were out of luck. To upgrade the program, we first modify the file so we can identify multiple blocks with an XML attribute. The illustration below shows the addition of the BlockName attribute. We'll use this in the code to find the block that we're interested in.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
The different signature blocks will be selected using a ComboBox control. I've added this to the form. And after using the program for a while, I decided that a better user interface could be achieved by simply redisplaying the block automatically instead of clicking a button just to do that. So I eliminated one button. The end result is shown in this illustration:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
This has created a whole new logic for the program, but fortunately, the code for the main form is still pretty simple. It mainly consists of calling objects from the SigBlock class. But there are only four methods (three Sub's and a Function) that do all the work.
SigFieldsShow(mySigBlockStruct, BlockListBox.SelectedItem)
Load(mySigBlockStruct, BlockListBox.SelectedItem)
GetBlockNames(myBlockNames)
GetLines(mySigBlockStruct)
One of the changes in the main form code is the addition of an ArrayList object to receive the names of all the XML blocks so they can be loaded into the ComboBox component.
' The names of all the blocks are loaded from the XML file
' in the New constructor for mySigBlock and then passed into
' this program in a variable length ArrayList collection.
Dim myBlockNames As ArrayList
The SigBlock class is completely different. The first technique that has to be added is the ability to read and write the XML file. The previous program only retrieved data from the XML file. This version has to write changes to the "selected items" in the signature block back to the XML. Since the XML file is only a text file, I decided to create a temporary copy of the file and then replace the file when the changes are complete. The System.IO namespace is imported for the File.Copy object.
The main loop of the biggest Sub, Load is as follows:
Using writer As XmlWriter = _
XmlWriter.Create(SigDocumentTemp, writerSettings)
Using reader As XmlReader = _
XmlReader.Create(SigDocument, readerSettings)
While Not reader.EOF
If reader.IsStartElement("SigBlock")
... lots of code ...
WriteShallowNode(reader, writer)
reader.Read()
End If
End While
End Using
End Using
File.Copy(SigDocumentTemp, SigDocument, True)
There's some heavy duty work happening here.
Using Blocks
The Using blocks are resource management. They allocate and then release a resource that's used for a specific purpose to make the program more efficient. In this case, the resource is the XMLWriter object and the XMLReader object. The loop reads and processes each XML node and either writes a new node or just the same node to a temporary file which is then copied over the original file after the resources are released.
But what's that WriteShallowNode(reader, writer) call? Good question. The easiest way to process the XML is to read each node and only process the ones that are important to the program. The rest get rewritten immediately to the output XML. This isn't a built in method of XML, it's a Sub that is right in the program source.
As I said in the beginning, this tutorial really is meant for people who are just learning VB.NET, but this program is, quite frankly, probably a step up from that level. So if you're just starting out, you won't understand a lot of what is happening in the program. There are two good reasons for having it here, however.
- The best way to learn is to stretch your mind to the maximum. Like they say in competitive sports: No pain. No gain. If you study the program, you'll pick up more than you might think.
- This is the kind of programming that is done in the real world. I don't think it's useful for students to learn techniques that they won't use, even if they're easier to understand.
To download the source code for the Lesson 8 program, click here.
Next - Preventing and Handling Errors in VB.NET
Error prevention and error handling is a priority subject for today's software and the tools for doing it are one of the biggest improvements in .NET. In part 11 of the tutorial, Errors - Preventing and Handling Them, these improvements are explained.

