locked
how to print lines of text from calculations in a multiline text box RRS feed

  • Question

  • Hi,

    I am converting an old visual basic program to visual basic express 2010

    What I am trying to do is put text from code into a multiline text box.

    I used to print this to paper in old program but what I want to do now is put it all to a text box so it all shows as text in the text box to save them printing, and then they can save all the text in the text box as a file so at a later date they can reload the text file and if necessary print it then.

    Most of it was done using printer.print statements and it prints lines which use variables which contain values from calculations so all the variables have been declared previously as public.

    I have read how ppl open a previously created text file into a textbox, but am not sure how to get this information into a textbox so it is set out line by line  and they can read thru it in a scrollable textbox seeing it set out more like a document.

    I will add the code I used in old program so you can see how the text was set out originally.

    I am not sure if there is a limit to how much information a multiline text box can hold.

     

    Here is the original code for one part of it that i am using as a guide. It has been taken from 2 subs, so anything repeated is because i have cut and pasted the code.

    Also some lines are printed or not printed according to if the variable value is 0.

    Any ideas how best to get this type of info into a text box. I know the printer.print  parts need changing. Its the information after each printer.print statement that I want to enter in a text box, and if some of the values are 0, I want that line missed and the next line put in.

    If this is not possible to do, let me know. I am still struggling a bit with relearning visual basic as so much has changed from the original program I did.

    printer.fontsize = 12
            printer.fontunderline = False
            printer.Print()
        printer.Print Spc(10); "Quote Date: "; dat
        printer.Print Spc(10); "Quote Number: "; qn
        printer.Print Spc(10); "Name:    "; nam
        printer.Print Spc(10); "Address: "; add
        printer.Print Spc(10); "         "; suburb
        printer.Print Spc(10); "Contact: "; cont
        printer.Print Spc(10); "Phone:   "; ph
        printer.Print Spc(10); "Fax: "; fax
        printer.Print Spc(10); "Order No:"; ord
        printer.Print Spc(10); "Job no: "; jn
        printer.Print Spc(10); "Date Required: "; dd
            printer.Print()
            printer.fontsize = 12
            printer.Print Spc(10); "--------------------------------------------------------------------"
            printer.Print Spc(10); box1; " Size= "; l; "mm x "; w; "mm x "; h; "mm "; treat
            printer.Print()
            printer.Print Spc(10); "Main Timber          "; b; " x "; t; Spc(10); "Pkt No:"
            printer.Print Spc(10); "Bases Boards         "; wb; " x "; bb; Spc(10); "Pkt No:"
            If sw = 0 Then GoTo runners1
            printer.Print Spc(10); "Skid Boards          "; sw; " x "; ts; Spc(10); "Pkt No:"
    runners1:
            printer.Print Spc(10); "Runners              "; hr; " x "; rw; Spc(10); "Pkt No:"
            printer.Print Spc(10); "Uprights & Topcleats "; wu; " x "; tu; Spc(10); "Pkt No:"
            If bc = 0 Then GoTo skids1
            printer.Print Spc(10); "Bottomcleats         "; wu; " x "; tu; Spc(10); "Pkt No:"
    skids1:
            printer.Print()
            nr1 = Val(nr)
            sk8 = sk4 * nr1
            If sk8 = 0 Then GoTo lids1
            printer.Print Spc(10); "Skids         = "; sk8; " at "; sk; " mm "
    lids1:
            l8 = l4 * nr1
            printer.Print Spc(10); "Lids          = "; l8; " at "; l5; " mm "
            b8 = b4 * nr1
            printer.Print Spc(10); "Bases         = "; b8; " at "; l; " mm "
            s8 = s5 * nr1
            printer.Print Spc(10); "Sides         = "; s8; " at "; l; " mm"
            e8 = e1 * nr1
            printer.Print Spc(10); "Ends          = "; e8; " at "; e2; " mm "
            ru8 = ru4 * nr1
            printer.Print Spc(10); "Runners       = "; ru8; " at "; ru; " mm"
            up8 = up1 * nr1
            printer.Print Spc(10); "Uprights      = "; up8; " at "; up; " mm"
            tc8 = tc1 * nr1
            printer.Print Spc(10); "Top Cleats    = "; tc8; " at "; tc; " mm "
            bc8 = bc * nr1
            If bc8 = 0 Then GoTo instruct
            printer.Print Spc(10); "Bottom Cleats = "; bc8; " at "; ru; " mm"
    instruct:
            printer.Print()
            printer.Print Spc(10); "Instructions: "; si
            printer.Print Spc(24); sinl
            printer.Print()
            printer.Print Spc(55); "Job completed   Date"
            printer.Print()
            printer.Print()
            printer.Print Spc(10); "Sketch if required: "
            printer.EndDoc()
      
            printer.fontsize = 12
            printer.Print Spc(10); "--------------------------------------------------------------------"
            printer.Print Spc(10); box1; " Size= "; l; "mm x "; w; "mm x "; h; "mm "; treat
            printer.Print()
            If sw = 0 Then GoTo runnersply
            printer.Print Spc(10); "Skid Boards          "; sw; " x "; ts; Spc(10); "Pkt No:"
    runnersply:
            printer.Print Spc(10); "Runners              "; hr; " x "; rw; Spc(10); "Pkt No:"
            If rh = 0 Then GoTo uprightsply
            printer.Print Spc(10); "End Runners          "; rh; " x "; wr; Spc(10); "Pkt No:"
    uprightsply:
            If u = 0 Then GoTo baseboardsply
            printer.Print Spc(10); "Uprights             "; wu; " x "; tu; Spc(10); "Pkt No:"
    baseboardsply:
            If wb = 0 Then GoTo topcleatsply
            printer.Print Spc(10); "Bases Boards         "; wb; " x "; bb; Spc(10); "Pkt No:"
    topcleatsply:
            If wtc = 0 Then GoTo bbply
            printer.Print Spc(10); "Top Cleats           "; wtc; " x "; tttc; Spc(10); "Pkt No:"
    bbply:
            If tpb = 0 Then GoTo seply
            printer.Print Spc(10); "Ply Base Boards      "; tpb; " mm "
    seply:
            printer.Print Spc(10); "Ply Sides & Ends     "; tps; " mm "
            If tpt = 0 Then GoTo skidsply
            printer.Print Spc(10); "Ply Top Boards       "; tpt; " mm "
    skidsply:
            printer.Print()
            nr1 = Val(nr)
            sk8 = sk4 * nr1
            If sk8 = 0 Then GoTo basesply
            printer.Print Spc(10); "Skids                     = "; sk8; " at "; sk; " mm "
    basesply:
            b8 = b4 * nr1
            If b8 = 0 Then GoTo runply
            printer.Print Spc(10); "Bases                     = "; b8; " at "; l; " mm "
    runply:
            ru8 = ru4 * nr1
            printer.Print Spc(10); "Runners                   = "; ru8; " at "; ru; " mm"
            ru9 = 2 * nr1
            If rh = 0 Then GoTo uprights2
            printer.Print Spc(10); "End Runners               = "; ru9; " at "; ru; " mm"
    uprights2:
            up8 = u * nr1
            If up8 = 0 Then GoTo tcleat
            printer.Print Spc(10); "Uprights                  = "; up8; " at "; up; " mm"
    tcleat:
            tc8 = tc1 * nr1
            If box = "Picture Frame Box with pallet base" Then GoTo Pict
            If box = "Picture Frame Box with ply base" Then GoTo Pict
            If tc8 = 0 Then GoTo Pict
            printer.Print Spc(10); "Top Cleats                = "; tc8; " at "; tc; " mm"
    Pict:
            If tbe = 0 Then GoTo plybases
            printer.Print Spc(10); "Side, Top & Bottom Boards = "; (nr1 * 4); " at "; l; " mm"
            printer.Print Spc(10); "End, Top & Bottom Boards  = "; (nr1 * 4); " at "; tbe; " mm"
            If tc1 = 0 Then GoTo plybases
            printer.Print Spc(10); "Top Timber for Length     = "; (nr1 * 2); " at "; ttl1; " mm"
            printer.Print Spc(10); "Top Cleats for Ends       = "; (nr1 * 2); " at "; tte; " mm"
            printer.Print Spc(10); "Top Cleats for Middle     = "; (nr1 * ttm3); " at "; ttm; " mm"
    plybases:
            If tpb = 0 Then GoTo sidespl
            printer.Print Spc(10); "Plywood Base              = "; nr1; " at "; l; " x "; w
    sidespl:
            printer.Print Spc(10); "Plywood Sides             = "; (nr1 * 2); " at "; ls; " x "; hs
            printer.Print Spc(10); "Plywood Ends              = "; (nr1 * 2); " at "; we; " x "; he
            If tpt = 0 Then GoTo instruction
            printer.Print Spc(10); "Plywood Top               = "; nr1; " at "; lt; " x "; wt
    instruction:
            printer.Print()
            printer.Print Spc(10); "Instructions: "; si
            printer.Print Spc(24); sinl
            printer.Print()
            printer.Print Spc(55); "Job completed   Date"
            printer.Print()
            printer.Print()
            printer.Print Spc(10); "Sketch if required: "
            printer.EndDoc()

     

    Saturday, December 11, 2010 1:28 AM

Answers

  • Something like this might get you started:

    private function GetAString() As string
        'printer.fontsize = 12
        'printer.fontunderline = False
        dim sb as StingBuilder("")
        sb.Append(Environment.NewLine)
        sb.Append(Spc(10) & "Quote Date: " & dat & Environment.Newline)
        sb.Append(Spc(10) & "Quote Number: " & qn & Environment.Newline)
        sb.Append(Spc(10) & "Name:    " & nam & Environment.Newline)
        sb.Append(Spc(10) & "Address: " & add & Environment.Newline)
        sb.Append(Spc(10) & "         " & suburb & Environment.Newline)
        sb.Append(Spc(10) & "Contact: " & cont & Environment.Newline)
        sb.Append(Spc(10) & "Phone:   " & ph & Environment.Newline)
        sb.Append(Spc(10) & "Fax: " & fax & Environment.Newline)
        sb.Append(Spc(10) & "Order No:" & ord & Environment.Newline)
        sb.Append(Spc(10) & "Job no: " & jn & Environment.Newline)
        sb.Append(Spc(10) & "Date Required: " & dd & Environment.Newline)
        sb.Append(Environment.NewLine)
        'printer.fontsize = 12
        sb.Append(Spc(10) & "--------------------------------------------------------------------" & Environment.Newline)
        sb.Append(Spc(10) & box1 & " Size= " & l & "mm x " & w & "mm x " & h & "mm " & treat & Environment.Newline)
        sb.Append(Environment.NewLine)
        sb.Append(Spc(10) & "Main Timber          " & b & " x " & t & Spc(10) & "Pkt No:" & Environment.Newline)
        sb.Append(Spc(10) & "Bases Boards         " & wb & " x " & bb & Spc(10) & "Pkt No:" & Environment.Newline)
        If sw <> 0 Then
           sb.Append(Spc(10) & "Skid Boards          " & sw & " x " & ts & Spc(10) & "Pkt No:" & Environment.Newline)
        end if
        'Rest of sting goes here .....

        return sb.Tostring
    End function

    You will need Imports.System.Text at top of file.
    You can then use TextBox1.Text = GetAString.

    Note that I've just commented out font settings. AFAIK you cannot do that in a textbox - the font is the same for all contents (might be wrong though). You can have varying font in a RichTextBox so that might be worth looking at. Also it assumes the variables are available (declared at form or module level).

    I think I'd actually have a number of smaller functions (one for each block of text) and call them as needed. I'd definitely get rid of the Gotos and change them as in the example to skip lines not required.

    Hope this helps.


    Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
    • Edited by Riced Saturday, December 11, 2010 11:36 AM Corrected System.Txt to System.Text
    • Marked as answer by sg1fannz Saturday, December 11, 2010 1:14 PM
    Saturday, December 11, 2010 11:35 AM

All replies

  • Something like this might get you started:

    private function GetAString() As string
        'printer.fontsize = 12
        'printer.fontunderline = False
        dim sb as StingBuilder("")
        sb.Append(Environment.NewLine)
        sb.Append(Spc(10) & "Quote Date: " & dat & Environment.Newline)
        sb.Append(Spc(10) & "Quote Number: " & qn & Environment.Newline)
        sb.Append(Spc(10) & "Name:    " & nam & Environment.Newline)
        sb.Append(Spc(10) & "Address: " & add & Environment.Newline)
        sb.Append(Spc(10) & "         " & suburb & Environment.Newline)
        sb.Append(Spc(10) & "Contact: " & cont & Environment.Newline)
        sb.Append(Spc(10) & "Phone:   " & ph & Environment.Newline)
        sb.Append(Spc(10) & "Fax: " & fax & Environment.Newline)
        sb.Append(Spc(10) & "Order No:" & ord & Environment.Newline)
        sb.Append(Spc(10) & "Job no: " & jn & Environment.Newline)
        sb.Append(Spc(10) & "Date Required: " & dd & Environment.Newline)
        sb.Append(Environment.NewLine)
        'printer.fontsize = 12
        sb.Append(Spc(10) & "--------------------------------------------------------------------" & Environment.Newline)
        sb.Append(Spc(10) & box1 & " Size= " & l & "mm x " & w & "mm x " & h & "mm " & treat & Environment.Newline)
        sb.Append(Environment.NewLine)
        sb.Append(Spc(10) & "Main Timber          " & b & " x " & t & Spc(10) & "Pkt No:" & Environment.Newline)
        sb.Append(Spc(10) & "Bases Boards         " & wb & " x " & bb & Spc(10) & "Pkt No:" & Environment.Newline)
        If sw <> 0 Then
           sb.Append(Spc(10) & "Skid Boards          " & sw & " x " & ts & Spc(10) & "Pkt No:" & Environment.Newline)
        end if
        'Rest of sting goes here .....

        return sb.Tostring
    End function

    You will need Imports.System.Text at top of file.
    You can then use TextBox1.Text = GetAString.

    Note that I've just commented out font settings. AFAIK you cannot do that in a textbox - the font is the same for all contents (might be wrong though). You can have varying font in a RichTextBox so that might be worth looking at. Also it assumes the variables are available (declared at form or module level).

    I think I'd actually have a number of smaller functions (one for each block of text) and call them as needed. I'd definitely get rid of the Gotos and change them as in the example to skip lines not required.

    Hope this helps.


    Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
    • Edited by Riced Saturday, December 11, 2010 11:36 AM Corrected System.Txt to System.Text
    • Marked as answer by sg1fannz Saturday, December 11, 2010 1:14 PM
    Saturday, December 11, 2010 11:35 AM
  • Thanks David for the help

    I knew what I wanted to do, but not how to achieve it so you taking the time to use some of my old code was really helpful.

    I really appreciate it.

    I definately will check out the richtextbox. I wasn't sure what the therichtextbox was used for so that was very helpful.

    Yes all the variables were declared at module level.

    Thanks again for your help :)

    Robyn

    Saturday, December 11, 2010 1:14 PM