Our program isn't quite finished (the data reported in the label components doesn't update yet) but it should compile and populate the list box correctly. VB .NET and ADO .NET make it easy to add the code necessary to update the Text properties of the label components, too! We'll do just the Description to give you the idea.
Display the properties dialog for the lblDesc component and you will see the DataBindings group of properties.
Using these properties, you can accomplish a quantum leap in automatic programming. The example shows the Articles.artDescription field from the dataset being assigned to the Text property of the lblDesc label. Assign the other labels the same way and we're (almost) done! Here's what our running program looks like:
We have been successful in almost duplicating the example from Part 2. The only differences that remain are that the URL and date don't look exactly like they did in the Part 2 program. This is because they're stored as Date/Time and Hyperlink fields in Access and when they're brought across to our form, they have extra characters and subfields that are part of those types. Solving this last problem does require a couple of additional lines of code to format the strings in the label text properties.
But if we add exactly the same code that worked for us in Part 2, we run into bugs! (Try it and see.) The problem is that since Visual Studio added the code to manage the contents of the listbox and label text properties, the events are not triggered the same way. This is an excellent example that puts genuine meaning behind a phrase that you often read, "Using bound controls gives you less control over the way data is handled." If you have ever wondered exactly what they mean by that, study this example.
The situation is improved with VB .NET over VB 6 because you can actually see (and override or overload) the class code that VB .NET uses. But this is often a less than satisfactory solution because that class code is often very complex and difficult to change. But at least the possibility is available.
In our example, I've decided to use a more simple approach. Taking advantage of the fact that all of the label fields change when a different item is selected in the listbox, the two formatting statements are simply triggered when one of the label text values changes. (The exact code to accomplish this formatting is also different. A couple of functions from some of the other Microsoft classes have been borrowed to do the job this time.):
Private Sub lblDesc_TextChanged( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles lblDesc.TextChanged
lblDate.Text = _
Microsoft.VisualBasic.Left( _
lblDate.Text, InStr(1, lblDate.Text, " "))
lblURL.Text = _
System.Web.RegularExpressions.TextRegex.Replace( _
lblURL.Text, "#", "")
End Sub
And as a final change from Part 2, rather than repeating the exact code in two places, the event subroutine is simply called from the Load event. Note that VB .NET requires that the parameters passed into the Load event be forwarded along as well.
lblDesc_TextChanged(sender, e)
Our experience in letting VB .NET wizards "Take Charge" has had typical results. They do a pretty good job, but they don't do one hundred percent of what you want! So many of the Microsoft examples (especially the ones seen in carefully scripted Microsoft presentations) simply avoid this awful truth by showing examples that don't run into it. But the "Good, Bad, and Ugly" of the situation is that even the worst cases are better than it used to be.
See the next page for downloads and the complete code.
Next page >
The Code and Download > Page
1,
2,
3,
4,
5,
6