Archive for the 'Dot NET' Category

Adding a pagebreak in a RichTextBox for printing

I found a creative way to force a new page when printing from .NET using a RichTextBox.
Here’s some C# code to show you how i did it. It’s not working copy paste ready code, it just shows the idea.


rtfText = getSomeRTFTextFromDatabaseFunction();
printingTextBox = new RichTextBoxEx();
printingTextBox.SelectedRtf = rtfText.Replace("newpage", "\\page");

As you can see when you put the text “newpage” somewhere in your rtf text, it will replace it with a rtf command \page which forces a new page when printing.

No Comments »

Adding data from an Array to a combobox in VB.NET

Add this Class:

Public Class ValueDescriptionPair

    Public Value As Object
    Public Description As String

    Public Sub New(ByVal NewValue As Object, ByVal NewDescription As String)
        Value = NewValue
        Description = NewDescription
    End Sub

    Public Overrides Function ToString() As String
        Return Description
    End Function

End Class

This is the way to add items to a combobox:

Dim Test_Array As New ArrayList

For i = 0 To someArrayWithData.GetLength(0) - 1
    Test_Array.Add(New ValueDescriptionPair(i, someArrayWithData(0)))
Next i

With ComboBox1
    .DisplayMember = "Description"
    .ValueMember = "Value"
    .DataSource = Test_Array
End With

No Comments »

Unable to cast object of type ‘System.String[*]’ to type ‘System.String[]’

I was using some DLL and got this error. I found this out that the DLL was returning a 1-based Array. VB.NET only has 0-based arrays. The following VB.NET script converts the 1-based Array to a 0-based Array.

Dim destArray() AS String
Dim counter AS Integer
counter = 0
For Each str As String In someDLLComponent.getStuffAsArray
    ReDim destArray(counter + 1)
    destArray(counter) = p
    counter += 1
Next

destArray now contains a 0-based Array.

No Comments »