Textbox Printing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Textbox Printing

  1. #1
    Join Date
    Oct 2006
    Posts
    56

    Textbox Printing

    When I use the button I have on my form to print, the text5 box prints in one line right off the end of the page. So you do not get everything entered in the textbox. I am using VB 6, please help.

    Code:
    Private Sub Command4_Click()
    Dim i, j As Integer, FS As String
    'Dim printOk As PrinterOrientationConstants
    Dim ext As Boolean
    Dim error As String
    Dim X As Integer
    Dim s As String
    Dim alignment As String
    Dim Y As Integer
    
    'SET PRINTER PARAMETERS
    Printer.CurrentX = (Printer.ScaleWidth - Printer.TextWidth(s)) \ 2
    
      Printer.TrackDefault = True
       Printer.FontName = "Arial": FS = "18"     'SET FONTNAME,SIZE
    If Not IsMissing(X) Then Printer.CurrentX = Val(X)
        If Not IsMissing(Y) Then Printer.CurrentY = Val(Y)
    
    X = FreeFile
        On Error GoTo HandleError
     
    
    Open Shell("notepad.exe") For Output As #1
    
    Printer.Print
    Printer.Print
    Printer.Print Spc(75); "DM - ARC"  ' Print text to file.
    Printer.Print Spc(75); "Simple Skid"
    Printer.Print   ' Print blank line to file.
    Printer.Print "Time:"; " "; Time
    Printer.Print "Date:"; " "; Date
    Printer.Print
    
    Printer.Print  text5
    
    Printer.Print
    
    Printer.Print
    
    Printer.Print
    
    Printer.Print
    
    Printer.Print
    Printer.Print Spc(40); "The vehicle was going at least"; " "; Text4; " "; "mph at the beginning of the skid."
    Printer.Print
    Printer.Print Spc(59); Text4; " "; " = Sqr(30 * "; Text1; " * "; Text2; " * "; Text3; ")"
    Printer.Print Spc(79); Text11; " "; "km/h"
      
      Do While Not EOF(X)
            Line Input #X, s
            Printer.Print s
        Loop
        Printer.EndDoc
    
    Close #1
    MsgBox "Data sent to printer for print operation.", vbOKOnly, "Print Status"
        Exit Sub
        
    HandleError:
        MsgBox "Error :" & Err.Description, vbCritical, "Printing File..."
    
    End Sub

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Textbox Printing

    There seems to be a lot of strange things in your little masterpiece which make me wonder...

    You declare X and Y as Integer and s as String.
    You start using the variables when they are still zero respectively "".
    Also a declared variable is never missing so IsMissing(X) is never true.
    Note that Printer.TextWidth(s) will be always 0 because s is "" (empty)

    Next you assign X = FreeFile, but you never Open the file.
    Then, what is this supposed to do:
    Code:
    Open Shell("notepad.exe") For Output As #1
    But anyway, to solve your Text5 problem look at the following code.
    It prints a given TextBox on a line by line basis as seen on the screen.
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
            ByVal hWnd As Long, ByVal wMsg As Long, _
            ByVal wParam As Long, lParam As Any _
       ) As Long
    
    Private Const EM_GETLINE = &HC4
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_LINELENGTH = &HC1
    Private Const EM_LINEINDEX = &HBB
    
    Private Function GetTextBoxLine(TBox As TextBox, ByVal LineNr As Long) As String
    ' get one line out of a textbox
      Dim LineLength As Long
      Dim LineStart As Long
       
      'get start of line and length
      LineStart = SendMessage(TBox.hWnd, EM_LINEINDEX, LineNr, ByVal 0&)
      LineLength = SendMessage(TBox.hWnd, EM_LINELENGTH, LineStart, ByVal 0&)
    
      'return line
      GetTextBoxLine = Mid$(TBox, LineStart + 1, LineLength)
    End Function
    
    Private Sub PrintTextBox(TBox As TextBox)
      Dim nLines%, i%
      
      nLines = SendMessage(TBox.hWnd, EM_GETLINECOUNT, 0, 0&)
      For i = 0 To nLines - 1
        Printer.Print GetTextBoxLine(TBox, i)
      Next

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,853

    Re: Textbox Printing

    It sounds like Text5 is a multi-line text box

    You will need to split the Text5 data into their various lines and print them separately. (Move the text box to a string then look for VBCRLF at the end of each line) or look for HEX 0D0A

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Textbox Printing

    Er... George, the problem with Text5 seems to be that there are NO vbCrLf. If there were, the printer would print individual lines.
    Therefore I proposed to use API calls to get individual lines as seen on screen and print them in a loop.

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: Textbox Printing

    Multiline wraps text. There are no Cr/Lf's.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Oct 2006
    Posts
    56

    Re: Textbox Printing

    Call me stupid, but I do not understand this stuff I copied what you had on the screen and pasted it in my form as seen below. Now it prints everything, I mean all my coding gets printed as well.....everything you see on this screen of code gets printed.
    Yes it is multi line text box. With a right side scroll bar.
    dglienna.... I have word wrap selected but it does nothing.
    Below you will find an attachment of my form. Sorry if I am causing you a lot of trouble.

    Code:
    Option Explicit
    Dim notes As Variant
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
            ByVal hWnd As Long, ByVal wMsg As Long, _
            ByVal wParam As Long, lParam As Any _
       ) As Long
    
    Private Const EM_GETLINE = &HC4
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_LINELENGTH = &HC1
    Private Const EM_LINEINDEX = &HBB
    
    Private Function GetTextBoxLine(TBox As TextBox, ByVal LineNr As Long) As String
    ' get one line out of a textbox
      Dim LineLength As Long
      Dim LineStart As Long
       
      'get start of line and length
      LineStart = SendMessage(TBox.hWnd, EM_LINEINDEX, LineNr, ByVal 0&)
      LineLength = SendMessage(TBox.hWnd, EM_LINELENGTH, LineStart, ByVal 0&)
    
      'return line
      GetTextBoxLine = Mid$(TBox, LineStart + 1, LineLength)
    End Function
    
    Private Sub PrintTextBox(TBox As TextBox)
      Dim nLines%, i%
      
      nLines = SendMessage(TBox.hWnd, EM_GETLINECOUNT, 0, 0&)
      For i = 0 To nLines - 1
        Printer.Print GetTextBoxLine(TBox, i)
      Next
    
    
    Private Sub Command1_Click()
    'S = SQR (30 * D * F * E)
    Text11.Text = Sqr(9.81 * 9.81 * Val(Text1.Text) * Val(Text2.Text) * Val(Text3.Text)) * 1.609
    'Text11.Text = Val(Text4.Text) * 1.609
    
    End Sub
    
    Private Sub Command2_Click()
    Text1 = ""
    Text2 = ""
    Text3 = ""
    Text4 = ""
    Text11 = ""
    End Sub
    
    Private Sub Command3_Click()
    MsgBox "S = SQR (9.81  D F E) 1.609"
    End Sub
    
    
    
    
    Private Sub Command4_Click()
    Dim i, j As Integer, FS As String
    'Dim printOk As PrinterOrientationConstants
    Dim ext As Boolean
    Dim error As String
    Dim X As Integer
    Dim s As String
    Dim alignment As String
    Dim Y As Integer
    
    'SET PRINTER PARAMETERS
    Printer.CurrentX = (Printer.ScaleWidth - Printer.TextWidth(s)) \ 2
    
      Printer.TrackDefault = True
       Printer.FontName = "Arial": FS = "18"     'SET FONTNAME,SIZE
    If Not IsMissing(X) Then Printer.CurrentX = Val(X)
        If Not IsMissing(Y) Then Printer.CurrentY = Val(Y)
    
    X = FreeFile
        On Error GoTo HandleError
     
    
    Open "C:\WINDOWS\Desktop\acr\notes.wri" For Output As #1
    
    Printer.Print
    Printer.Print
    Printer.Print Spc(75); "DM - ARC"  ' Print text to file.
    Printer.Print Spc(75); "Simple Skid"
    Printer.Print   ' Print blank line to file.
    Printer.Print "Time:"; " "; Time
    Printer.Print "Date:"; " "; Date
    Printer.Print
    
    Printer.Print
    
    Printer.Print Text5
    
    Printer.Print
    
    Printer.Print
    
    Printer.Print
    
    Printer.Print
    Printer.Print Spc(40); "The vehicle was going at least"; " "; Text4; " "; "mph at the beginning of the skid."
    Printer.Print
    Printer.Print Spc(59); Text4; " "; " = Sqr(30 * "; Text1; " * "; Text2; " * "; Text3; ")"
    Printer.Print Spc(79); Text11; " "; "km/h"
      
      Do While Not EOF(X)
            Line Input #X, s
            Printer.Print s
        Loop
        Printer.EndDoc
    
    Close #1
    MsgBox "Data sent to printer for print operation.", vbOKOnly, "Print Status"
        Exit Sub
        
    HandleError:
        MsgBox "Error :" & Err.Description, vbCritical, "Printing File..."
    
    
    
    End Sub
    
    Private Sub Command5_Click()
    Unload Me
    End Sub
    
    
    Private Sub Command6_Click()
    MsgBox ("This module will calculate the speed of a vehcile at the beginning of a skid based on the length of the kid, cf, and bf")
    End Sub
    
    Private Sub Text1_Validate(Cancel As Boolean)
      If Val(Text1.Text) > 1.21 Then
     MsgBox "Please select the proper Value for this test."
      Text1 = ""
      frmCoe.Show
      
    End If
    End Sub
    
    Private Sub Text2_Validate(Cancel As Boolean)
    If Val(Text2.Text) > 1 Then
    MsgBox "Please select the proper Value for this test."
    Text2 = ""
    frmCoe.Show
    End If
    End Sub
    Attached Images Attached Images  

  7. #7
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: Textbox Printing

    Keep track of where you are, and send linefeeds when needed!

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Dim arr() As String
      Dim str As String
      Dim x As Integer
      str = "this is a test of a really long string that wraps"
      arr() = Split(str, " ")
      str = ""
      For x = 0 To UBound(arr()) - 1
        ' keep track of length of line, and then add a crlf
        ' otherwise, add " "
        str = str & arr(x) & vbCrLf
      Next x
      MsgBox str
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Textbox Printing

    I suppose your Print button is the Command4.

    In your Command4_Click() routine is a line which is supposed to print the Text5. it is:
    Printer.Print Text5
    Replace this line by
    PrintTextBox Text5
    for my code to work. I tested it. It prints the textbox like you see it on screen. No need to keep track of anything.

    There is still a lot of garbage in your printing routine.

    For instance: You open a file for output and do nothing with it.
    Then you have a reading loop from a file not even opened or specified.
    Code:
      Do While Not EOF(X)
            Line Input #X, s
            Printer.Print s
        Loop
    What's this supposed to do?
    I don't know why your code gets printed, but this could be reading from an elsewhere opened file and printing everything.

  9. #9
    Join Date
    Oct 2006
    Posts
    56

    Re: Textbox Printing

    WOW Wof.....you are a genius, works great ......you have made my day. Thank you, thank you, thank you.

    Code:
    'Open "C:\WINDOWS\Desktop\acr\notes.wri" For Output As #1
    
    Do While Not EOF(X)
            Line Input #X, s
            Printer.Print s
        Loop
    I removed the stuff above from my coding as you suggested. It was there because ........well I thought it was suppose to be. I really am not very good at this and have read many tutorials and just get confused. Is there anything else you see in there that should be taken out???
    Thanks a million!!!

  10. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Textbox Printing

    You're welcome.
    In fact I believe nearly everything except the Printer.Print statements are necessary, so here is a version which is stripped down from any code that seemed useless to me.
    Look at the first statements. The Font is set to Arial, but the fontsize was never set, so I added tho bold line Printer.FontSize = 18.
    If this prints too big, simply remove the line.


    Code:
    Private Sub Command4_Click()
    
    'SET PRINTER PARAMETERS
    
    Printer.TrackDefault = True
    Printer.FontName = "Arial": 'FS = "18"     'SET FONTNAME,SIZE
    Printer.FontSize = 18
    
    On Error GoTo HandleError
     
    Printer.Print
    Printer.Print
    Printer.Print Spc(75); "DM - ARC"  ' Print text to file.
    Printer.Print Spc(75); "Simple Skid"
    Printer.Print   ' Print blank line to file.
    Printer.Print "Time:"; " "; Time
    Printer.Print "Date:"; " "; Date
    Printer.Print
    
    Printer.Print  text5
    
    Printer.Print
    
    Printer.Print
    
    Printer.Print
    
    Printer.Print
    
    Printer.Print
    Printer.Print Spc(40); "The vehicle was going at least"; " "; Text4; " "; "mph at the beginning of the skid."
    Printer.Print
    Printer.Print Spc(59); Text4; " "; " = Sqr(30 * "; Text1; " * "; Text2; " * "; Text3; ")"
    Printer.Print Spc(79); Text11; " "; "km/h"
    
    MsgBox "Data sent to printer for print operation.", vbOKOnly, "Print Status"
        Exit Sub
        
    HandleError:
        MsgBox "Error :" & Err.Description, vbCritical, "Printing File..."
    
    End Sub

  11. #11
    Join Date
    Oct 2006
    Posts
    56

    Re: Textbox Printing

    WoF...works like a charm. I even discovered that I could change the font size half way throught the page by adding Printer.FontSize = 8 where I wanted it smaller, then adding Printer.FontSize = 11 when I wanted it bigger again.( FontSize 18 was too big.) All thanks to you.
    I changed all the things you mentioned in your post and everything works great.
    Again thank you very much.....you have really made my day....heck...week.
    Thanks Wof.

  12. #12
    Join Date
    Dec 2007
    Posts
    3

    Re: Textbox Printing

    I've tried this last code block given above, but my textbox still prints in one giant line, therefore loosing most of the text way off the page. I'm sure I'm missing something simple, but it's been a while since I've worked with VB, and I've been given the task of updating this fine application. Any more help here would be greatly appreciated. Thanks!

  13. #13
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: Textbox Printing

    You'd do better starting a new thread with your code, and a picture or two
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center