February 2006 - Posts

Recently, I co-authored a series of articles for MSDN online with Paul Sheriff, aimed at helping VB6 developers get the hang of working with ADO.NET. Unfortunately, the demo files for the first two articles didn't make it online. I've received several requests for the files, so I've made them available here. The following links take you to the articles, and to the downloads:

Converting a Data-Oriented Application from Visual Basic 6 to Visual Basic 2005, Part 1: Download sample

Converting a Data-Oriented Application from Visual Basic 6 to Visual Basic 2005, Part 2: Download sample

Converting a Data-Oriented Application from Visual Basic 6 to Visual Basic 2005, Part 3: Download the sample through the MSDN article link

I'm sorry for the inconvenience, but I hope you find the series useful.

 

Posted by KenG | with no comments
Filed under:

Speaking of Bill McCarthy, he also solved another recent problem for me, inadvertently.

I recently wrote an article for MSDN Magazine, ostensibly about setting Word properties programmatically, but from my perspective, it's really about object data binding.

Got the demo working perfectly, submitted it, and then went back in to modify the code. The form simply wouldn't load in the form editor!

Bill's blog includes a quick and easy solution. If you're attempting to follow the steps in the article, and get the same error, the answer is:

Open the Data Sources window before you open the form in the designer.

I've submitted (yet another, I assume) bug on this to Microsoft.

Because I've already received a few email questions about this, it seemed worthwhile to post a response here.

Posted by KenG | with no comments

Soon after posting the previous item, my VB pal from down under, Bill McCarthy,  responded quickly with a suggested upgrade. We went back and forth a few times, and here's what I ended up with:

Private Sub ToggleFormat(ByVal rtb As RichTextBox, ByVal newStyle As FontStyle)
  If rtb IsNot Nothing Then
    Dim fnt As Font = rtb.SelectionFont
    If fnt IsNot Nothing Then
      rtb.SelectionFont = New Font(fnt, fnt.Style Xor newStyle)
    End If
  End If
End Sub

Originally, I had balked at the use of Xor (mostly 'cause I'm writing this up in courseware for beginners) but Bill reminded me that that's even a BETTER reason to use Xor--to show folks how they can use it in real code. Thanks, Bill, for the easy and elegant code!

Posted by KenG | 1 comment(s)

I'm sure it made sense to someone not to be able to modify styles on an existing font in .NET, but it always surprises me. In addition, the example in the documentation for the RichTextBox.SelectionFont property is pretty, well, broken. They include a ToggleFont method which supposedly toggles the state of the boldness of the selection. Unfortunately, it doesn't handle the text already having other attributes (italic, underline) and does its work awfully clumsily. In case you need this behavior, here's the procedure I came up with:

Private Sub ToggleFormat(ByVal rtb As RichTextBox, ByVal newStyle As FontStyle)
  If rtb.SelectionFont IsNot Nothing Then
    Dim currentFont As Font = rtb.SelectionFont
    Dim currentFontStyle As FontStyle = currentFont.Style
    Dim newFontStyle As FontStyle

    If (currentFontStyle And newStyle) = newStyle Then
      newFontStyle = currentFontStyle And Not newStyle
    Else
      newFontStyle = currentFontStyle Or newStyle
    End If

    rtb.SelectionFont = New Font(currentFont, newFontStyle)
  End If
End Sub

The issue is that it's not clear from the example, or from playing, that the font style is a set of bits (which makes sense, obviously, once you think about it). Setting and removing a font style is just a matter of setting and clearing bits. Therefore, the cleaner ToggleFormat.

Posted by KenG | 1 comment(s)