< Continued from page 1
Open the main form, SigBlockForm.vb again. There is currently one module level object, mySigBlock, declared there. We created this object and added it to the project in the previous version. We'll need another one now to hold our new structure since it's now in a different file. Remember that the New keyword creates an "instance" (a copy) of the object. That copy is named mySigBlockStruct in this code module.
Adding values to the fields in the copy will be done with code that we add later. Add this code below the declaration for mySigBlock:
We're going to depend on our SigBlock object to do more in this version of the program. Replace the line of code that displays the changed form with one that asks the SigBlock object to do that. Replace ...
... with ...
Because we haven't coded the SigFieldsShow method, VB Express displays an error (blue squiggly line) until we do.
Finally, replace mySigBlock.Lines with mySigBlock.GetLines(mySigBlockStruct) in DisplayBlock_Click to retrieve the signature block for display. The statement should look like this:
Now that we have changed the code to use our new structure, we need to initialize the "instance" of the structure in this module with values from a file. That will be done by a method in the SigBlock.vb module that we will code later but we have to call the method here.
To do that, we take advantage of the Load event for the main form to call the new method. You can double click the main form (be careful to click the form and not one of the controls) to get a starting Load event subroutine. (I've reformatted the lines to make them shorter so they fit the page better. Doing this without continuation characters only works in VB.NET 2010 or later.)
In addition, the Load event code repeats the code in the DisplayBlock_Click subroutine just so the initial form has some starting values displayed in it. the completed code in the Load event subroutine looks like this:
Changing the Worker Module: SigBlock
The changes to the main startup form are complete. Now open the code window for the SigBlock.vb file. This is where most of the changes will have to be made. In the previous version, this module was just a place to keep the information. In this version, it will actually do things. In fact, delete everything in the module except for the Class and End Class statements.
The Imports Statement
The first change is to add a couple of Imports statements:
If you use them, Imports statements have to be the first statements in a code module. An Imports statement is never required, but it makes the program easier to code and read. All it does is tell VB Express to try adding the name following the Imports in front of the names of objects to locate them. This lets you make names of objects in your code a little shorter.
In other words, the "fully qualified" name of an object that we will use is:
Using Imports lets us code it this way instead:
On the next page, we continue the Signature Block program and learn about several new type of statements that we'll need, the Const statement, ByVal and ByRef, and modal forms.
Open the main form, SigBlockForm.vb again. There is currently one module level object, mySigBlock, declared there. We created this object and added it to the project in the previous version. We'll need another one now to hold our new structure since it's now in a different file. Remember that the New keyword creates an "instance" (a copy) of the object. That copy is named mySigBlockStruct in this code module.
Adding values to the fields in the copy will be done with code that we add later. Add this code below the declaration for mySigBlock:
Dim mySigBlockStruct As New SigBlockStruct
We're going to depend on our SigBlock object to do more in this version of the program. Replace the line of code that displays the changed form with one that asks the SigBlock object to do that. Replace ...
SigFieldDisplay.Show()
... with ...
mySigBlock.SigFieldsShow(mySigBlockStruct)
Because we haven't coded the SigFieldsShow method, VB Express displays an error (blue squiggly line) until we do.
Finally, replace mySigBlock.Lines with mySigBlock.GetLines(mySigBlockStruct) in DisplayBlock_Click to retrieve the signature block for display. The statement should look like this:
SigBlockBox.Text =mySigBlock.GetLines(mySigBlockStruct)
Now that we have changed the code to use our new structure, we need to initialize the "instance" of the structure in this module with values from a file. That will be done by a method in the SigBlock.vb module that we will code later but we have to call the method here.
To do that, we take advantage of the Load event for the main form to call the new method. You can double click the main form (be careful to click the form and not one of the controls) to get a starting Load event subroutine. (I've reformatted the lines to make them shorter so they fit the page better. Doing this without continuation characters only works in VB.NET 2010 or later.)
In addition, the Load event code repeats the code in the DisplayBlock_Click subroutine just so the initial form has some starting values displayed in it. the completed code in the Load event subroutine looks like this:
Private Sub SigBlockForm_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.LoadmySigBlock.Load(mySigBlockStruct)' Initial startup fields displayedSigBlockBox.Text =mySigBlock.GetLines(mySigBlockStruct)End Sub
Changing the Worker Module: SigBlock
The changes to the main startup form are complete. Now open the code window for the SigBlock.vb file. This is where most of the changes will have to be made. In the previous version, this module was just a place to keep the information. In this version, it will actually do things. In fact, delete everything in the module except for the Class and End Class statements.
The Imports Statement
The first change is to add a couple of Imports statements:
Imports System.IOImports System.RunTime.Serialization.Formatters
If you use them, Imports statements have to be the first statements in a code module. An Imports statement is never required, but it makes the program easier to code and read. All it does is tell VB Express to try adding the name following the Imports in front of the names of objects to locate them. This lets you make names of objects in your code a little shorter.
In other words, the "fully qualified" name of an object that we will use is:
System.RunTime.Serialization.Formatters.Binary.BinaryFormatter
Using Imports lets us code it this way instead:
Binary.BinaryFormatter
On the next page, we continue the Signature Block program and learn about several new type of statements that we'll need, the Const statement, ByVal and ByRef, and modal forms.
SHARE