Results 1 to 2 of 2

Thread: Please examine and advise

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Location
    Georgia
    Posts
    27

    Please examine and advise

    Dear all

    Here is my code from VB.net. I want to be able to print the contents of all the Textboxes. Please tell me how to.

    Thanks in advance
    SPC Olsen

    Private StreamToPrint As IO.StreamReader
    Private printFont As Font

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    e.Graphics.DrawString("SampleText", New Font("Arial", 80, FontStyle.Bold), Brushes.Black, 150, 125)
    Dim LinesPerPage As Single = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics)
    Dim Line As String = Nothing, count As Integer, yPos As Single = 0
    For count = 0 To LinesPerPage - 1
    Line = StreamToPrint.ReadLine
    If Line Is Nothing Then Exit For
    yPos = e.MarginBounds.Top + count * PrintFont.GetHeight(e.Graphics)
    e.Graphics.DrawString(Line, PrintFont, Brushes.Black, e.MarginBounds.Left, yPos, New StringFormat())
    Next
    If Not (Line Is Nothing) Then e.HasMorePages = True
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    StreamToPrint = New IO.StreamReader("ROSTER__.txt, SECT.txt, SSN.txt, LAST_NAME.txt, FIRST_NAME.txt")
    PrintDialog1.Document = PrintDocument1
    PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
    PrintDialog1.AllowSomePages = True
    If PrintDialog1.ShowDialog = DialogResult.OK Then
    PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
    PrintDocument1.Print()
    If Not (StreamToPrint Is Nothing) Then
    StreamToPrint.Close()
    End If
    End If

    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "c:\"
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|doc files (*.doc)|*.doc|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 1
    openFileDialog1.RestoreDirectory = True

    If openFileDialog1.ShowDialog() = DialogResult.OK Then
    openFileDialog1.OpenFile()

    End If

    End Sub

    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
    Dim saveFileDialog1 As New SaveFileDialog()

    saveFileDialog1.Filter = "doc files (*.doc)|*.doc|All files (*.*)|*.*"
    saveFileDialog1.FilterIndex = 1
    saveFileDialog1.RestoreDirectory = True

    If saveFileDialog1.ShowDialog() = DialogResult.OK Then

    End If

    End Sub
    End Class

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I assume instead of TextBoxes you meant text FILES?

    In SaveFileDialog1_FileOk: This whole sub seems to me to be redundant. I take it you added a SaveFileDialog from the toolbar by drag/drop? If so then you probably don't want to mess with it like you're doing - the only way the SaveFileDialog1.FileOk event will fire is *after* the dialog box has been loaded, shown, and the user has pressed OK, which makes me scratch my head when I see you're trying to Dim saveFileDialog1 As New SaveFileDialog() and initialize it with settings you should probably set in the properties editor on the Design view for the parent form. I would be much surprised if your program executed the least bit different if you cut that whole sub out.

    In PrintDocument1_PrintPage:
    Code:
    e.Graphics.DrawString("SampleText", New Font("Arial", 80, FontStyle.Bold), Brushes.Black, 150, 125)
    ^^ this line is going to botch up the rest of your print job because you're not using MeasureString to record how much printing space it took up before you started printing.

    Code:
    Dim LinesPerPage As Single = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics)
    ^^ printFont has not been initialized yet and so the .GetHeight method may return a value that you aren't expecting

    I haven't printed with exactly this method (printing multiple files at once) but you seem to be on the right track - you just need to get rid of the Sample Text line and you may want to also look into MeasureString. Here's a very straightforward example (seemed straightforward to me anyhow) of just printing line-by-line:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDrawingPrintingPrintDocumentClassPrintPageTopic.htm

    ..which I can see you've probably already been looking at, but you missed a couple of things (notably the part where the font gets initialized, printFont = New Font("Arial", 10))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width