VB.Net – ControlChars

If you need special characters in your VB.Net use the ControlChars class, it has definitions for Lf, Crlf, etc.

See MSDN for more details


If you are using the PrintPreviewControl and what the page displayed to be refreshed with a new document, you must call the PrintPreviewControl.InvalidatePreview method. In 1.1 this did it automatically everytime a document was set, but now you have to do it manually, thats progress for you!

StatementPrintPreviewControl.Document = aDocument

TryCast – better than DirectCast?

In VB.Net Framework V2.0 a new command was introduced called TryCast It works the same as DirectCast but if the cast fails instead of throwing an exception the object is set to Nothing

Take a look at MSDN for more details.

PrintPreviewDialog – PrintDialog

Ok so this might be an obvious one but it got me stuck for a little while so thought I should post it. When you use the PrintPreviewDialog the documents Print method is automatically called when the dialog is shown. But using the PrintDialog the Print method of the associated PrintDocument is not called when the user clicks the Print button on the dialog, you have to check that OK was returned as the dialogs result!

DataGridView CheckBox Value

There are a few gotchas with checking the value of a CheckBox in a DataGridView. You can use the CellValueChanged event, the bound object will have been updated so its easy to check…

Private Sub PaymentsDataGridView_CellValueChanged(ByVal sender As System.Object, _
            ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
                Handles PaymentsDataGridView.CellValueChanged
    'This code will be called while loading the grid as well so I 
    'just check for it by setting a flag
    If Not _Loading AndAlso e.ColumnIndex = 4 AndAlso e.RowIndex >= 0 Then
        Dim aSummary As RebatePatientSummary = _
                DirectCast(PaymentsDataGridView.Rows(e.RowIndex).DataBoundItem, _
        If aSummary.PrintMe Then
            _PrintCount += 1
            _PrintCount -= 1
        End If
        AvailableToPrintLabel.Text = "Selected for printing : " & _PrintCount
     End If
End Sub

That gotcha here though is that the event is only fired once the cell has lost focus!

You can use the CurrentCellDirtyStateChanged but that only fires once so if the user clicks the checkbox multiple times you don’t actually know the final state

PrintDocument, PrintPreviewDialog and Multiple Pages

If you are generating multiple pages in a document you set the e.HasMorePages to true, normally this is done by checking how many pages you have to print, e.g.

_CurrentPageNumber += 1
e.HasMorePages = _CurrentPageNumber 

If the user then decides to Print the pages they are likely to only see the first page because the _CurrentPageNumber has not been reset to zero, as the Print command actually regenerates the pages by firing your code again not using what it has already created.

The tip here is to reset the controlling counter in the BeginPrint event