Hi All,

Hope evryone is well.

I have a program I have created for booking out conference rooms, I want to code a print button so the user can print the booking they have created on the booking form.

I have been stuck on this for about a week and its driving me mad.

I have used a print document method and bound my text boxes to it, but I am not getting the print out format correct.

For instance my form on the screen will look like this

3
12/01/2011
08:00
13:00
sales meeting

but on paper it comes out like this

312/01/201108:0013:00salesmeeting

this is my code

Dim str As String
    Dim f As Font
    Dim b As SolidBrush
    Dim lines As Integer
    Dim chars As Integer
    Dim strformat As StringFormat
    Dim printstr As String
    Dim ps As PageSettings = New PageSettings()
  'LOADS PRINT DIALOG BOX
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PrintDocument1.DefaultPageSettings = ps
        str = BookingIDTextBox.Text & RoomIDTextBox.Text & UserIDTextBox.Text _
        & ReasonTextBox.Text
        PrintDialog1.Document = PrintDocument1

        If PrintDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            PrintDocument1.Print()
        End If

    End Sub
    'Part of the print sub procedure
    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As PrintEventArgs) Handles PrintDocument1.BeginPrint
        Try
            f = New Font("Arial", 12)
            b = New SolidBrush(Color.Black)
            strformat = New StringFormat()
            strformat.Trimming = StringTrimming.Word
            str = BookingIDTextBox.Text & RoomIDTextBox.Text & UserIDTextBox.Text _
            BookDateTextBox.Text & StartTimeTextBox.Text & EndTimeTextBox.Text _ &      ReasonTextBox.Text
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
    'Part of the print sub procedure
    Private Sub PrintDocument1_PrintPage_1(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim myrect As RectangleF = New RectangleF(e.MarginBounds.Left, _
                                                  e.MarginBounds.Top, e.MarginBounds.Width, _
                                                  e.MarginBounds.Height)
        Dim sz As SizeF = New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - f.GetHeight(e.Graphics))
        e.Graphics.MeasureString(str, f, sz, strformat, chars, lines)
        printstr = str.Substring(0, chars)
        e.Graphics.DrawString(printstr, f, b, myrect, strformat)

        If str.Length > chars Then
            str = str.Substring(chars)
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If

    End Sub

Can anyone help me out.

Thanks

John

***UPDATE***

After all this I found the solution.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.