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

sales meeting

but on paper it comes out like this


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()
    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
        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
            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
        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, _
        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
            e.HasMorePages = False
        End If

    End Sub

Can anyone help me out.



This question has already been answered. Start a new discussion instead.