Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,040 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,594 people online right now. Registration is fast and FREE... Join Now!




Using Streamwriter to write text

 
Reply to this topicStart new topic

Using Streamwriter to write text

zekeholmes
2 Mar, 2008 - 10:41 AM
Post #1

New D.I.C Head
*

Joined: 19 Feb, 2008
Posts: 8


My Contributions
I have been working on an invoice program that saves the text box data as a .txt file for backup. With the different lenghts of text data the .txt file does not line up. I am new to vb and i am running Visual Basic 2008 Express. As a short fix I have added spaces to seperate the data. I have tried using tab() funtion, but it seems to not print the rest of the data for that line. I am trying to get the quanitys and prices to line up down the page. Here is the code that i am currently using

CODE
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

        Dim FileName As String = JobNumberBox.Text
        Dim Sw As System.IO.StreamWriter
        Sw = My.Computer.FileSystem.OpenTextFileWriter("c:\AtHomeInc\Invoice " & FileName & ".txt", False)


        'prints invoice number and date
        Sw.WriteLine("Invoice #" & JobNumberBox.Text & "          Date:" & DateBox.Text)
        Sw.WriteLine(" ")

        'prints customer info
        Sw.WriteLine(CustName.Text)
        Sw.WriteLine(CustStreet.Text)
        Sw.WriteLine(CustCity.Text)
        Sw.WriteLine(CustPhone.Text)
        Sw.WriteLine(" ")

        'prints line items to txt file
        Sw.WriteLine(DescriptionBox1.Text & "     " & LocationBox1.Text & "     " & qtyBox1.Text & "     " & PriceEachBox1.Text & "     " & LineTotalBox1.Text)
        Sw.WriteLine(DescriptionBox2.Text & "     " & LocationBox2.Text & "     " & qtybox2.Text & "     " & PriceEachBox2.Text & "     " & LineTotalBox2.Text)
        Sw.WriteLine(DescriptionBox3.Text & "     " & LocationBox3.Text & "     " & QtyBox3.Text & "     " & PriceEachBox3.Text & "     " & LineTotalBox3.Text)
        Sw.WriteLine(DescriptionBox4.Text & "     " & LocationBox4.Text & "     " & QtyBox4.Text & "     " & PriceEachBox4.Text & "     " & LineTotalBox4.Text)
        Sw.WriteLine(DescriptionBox5.Text & "     " & LocationBox5.Text & "     " & QtyBox5.Text & "     " & PriceEachBox5.Text & "     " & LineTotalBox5.Text)
        Sw.WriteLine(DescriptionBox6.Text & "     " & LocationBox6.Text & "     " & QtyBox6.Text & "     " & PriceEachBox6.Text & "     " & LineTotalBox6.Text)
        Sw.WriteLine(DescriptionBox7.Text & "     " & LocationBox7.Text & "     " & QtyBox7.Text & "     " & PriceEachBox7.Text & "     " & LineTotalBox7.Text)
        Sw.WriteLine(DescriptionBox8.Text & "     " & LocationBox8.Text & "     " & QtyBox8.Text & "     " & PriceEachBox8.Text & "     " & LineTotalBox8.Text)
        Sw.WriteLine(DescriptionBox9.Text & "     " & LocationBox9.Text & "     " & QtyBox9.Text & "     " & PriceEachBox9.Text & "     " & LineTotalBox9.Text)
        Sw.WriteLine(DescriptionBox10.Text & "     " & LocationBox10.Text & "     " & QtyBox10.Text & "     " & PriceEachBox10.Text & "     " & LineTotalBox10.Text)
        Sw.WriteLine(DescriptionBox11.Text & "     " & LocationBox11.Text & "     " & QtyBox11.Text & "     " & PriceEachBox11.Text & "     " & LineTotalBox11.Text)
        Sw.WriteLine(DescriptionBox12.Text & "     " & LocationBox12.Text & "     " & QtyBox12.Text & "     " & PriceEachBox12.Text & "     " & LineTotalBox12.Text)
        Sw.WriteLine(DescriptionBox13.Text & "     " & LocationBox13.Text & "     " & QtyBox13.Text & "     " & PriceEachBox13.Text & "     " & LineTotalBox13.Text)
        Sw.WriteLine(DescriptionBox14.Text & "     " & LocationBox14.Text & "     " & QtyBox14.Text & "     " & PriceEachBox14.Text & "     " & LineTotalBox14.Text)
        Sw.WriteLine(DescriptionBox15.Text & "     " & LocationBox15.Text & "     " & QtyBox15.Text & "     " & PriceEachBox15.Text & "     " & LineTotalBox15.Text)
        Sw.WriteLine(DescriptionBox16.Text & "     " & LocationBox16.Text & "     " & QtyBox16.Text & "     " & PriceEachBox16.Text & "     " & LineTotalBox16.Text)
        Sw.WriteLine(DescriptionBox17.Text & "     " & LocationBox17.Text & "     " & QtyBox17.Text & "     " & PriceEachBox17.Text & "     " & LineTotalBox17.Text)
        Sw.WriteLine(DescriptionBox18.Text & "     " & LocationBox18.Text & "     " & QtyBox18.Text & "     " & PriceEachBox18.Text & "     " & LineTotalBox18.Text)
        Sw.WriteLine(DescriptionBox19.Text & "     " & LocationBox19.Text & "     " & QtyBox19.Text & "     " & PriceEachBox19.Text & "     " & LineTotalBox19.Text)
        Sw.WriteLine(DescriptionBox20.Text & "     " & LocationBox20.Text & "     " & QtyBox20.Text & "     " & PriceEachBox20.Text & "     " & LineTotalBox20.Text)
        Sw.WriteLine(DescriptionBox21.Text & "     " & LocationBox21.Text & "     " & QtyBox21.Text & "     " & PriceEachBox21.Text & "     " & LineTotalBox21.Text)
        Sw.WriteLine(DescriptionBox22.Text & "     " & LocationBox22.Text & "     " & QtyBox22.Text & "     " & PriceEachBox22.Text & "     " & LineTotalBox22.Text)
        Sw.WriteLine(DescriptionBox23.Text & "     " & LocationBox23.Text & "     " & QtyBox23.Text & "     " & PriceEachBox23.Text & "     " & LineTotalBox23.Text)
        'prints grand total to txt file
        Sw.WriteLine(" ")
        Sw.WriteLine("                                                          Total $" & grandtotalbox.Text)
        Sw.Close()

    End Sub


I would really appriciate any advise smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Using Streamwriter To Write Text
2 Mar, 2008 - 10:44 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.Net (Then Ill look through your code)
User is offlineProfile CardPM
+Quote Post

zekeholmes
RE: Using Streamwriter To Write Text
2 Mar, 2008 - 10:50 AM
Post #3

New D.I.C Head
*

Joined: 19 Feb, 2008
Posts: 8


My Contributions
QUOTE(PsychoCoder @ 2 Mar, 2008 - 11:44 AM) *

Moved to VB.Net (Then Ill look through your code)


Wow, that was fast.
Thank you, I'm not familiar with differance between the two.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Using Streamwriter To Write Text
2 Mar, 2008 - 10:57 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well the Visual Basic Forum is for VB6 and older, VB2003, VBG2005, VB2008 are all part of the .Net family and are object orientated languages, whereas VB6 and older aren't
User is offlineProfile CardPM
+Quote Post

zekeholmes
RE: Using Streamwriter To Write Text
2 Mar, 2008 - 11:17 AM
Post #5

New D.I.C Head
*

Joined: 19 Feb, 2008
Posts: 8


My Contributions
Thank you for the explanation.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using Streamwriter To Write Text
2 Mar, 2008 - 12:10 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Since you didn't post how you are using the Tab function. I would recommend using the ControChars.Tab constant to help space things out.

CODE

Sw.WriteLine(DescriptionBox1.Text & ControlChars.Tab & LocationBox1.Text & ControlChars.Tab & qtyBox1.Text & ControlChars.Tab & PriceEachBox1.Text & ControlChars.Tab & LineTotalBox1.Text)


However, if the text being added is of different lengths then you are still going to have problems. You would probably be better off using the PadRight/PadLeft to get a fixed format text file.

Can you post an example of how you want the text file to display with some sample data?

Enclosed it in code.gif tags when you post it, so the forum software doesn't remove the extra spaces.
User is offlineProfile CardPM
+Quote Post

zekeholmes
RE: Using Streamwriter To Write Text
2 Mar, 2008 - 12:31 PM
Post #7

New D.I.C Head
*

Joined: 19 Feb, 2008
Posts: 8


My Contributions
I had tried to use the tab function like this

CODE
        Sw.WriteLine(DescriptionBox1.Text, TAB(25), LocationBox1.Text & "     " & qtyBox1.Text & "     " & PriceEachBox1.Text & "     " & LineTotalBox1.Text)



here is how the text file is created now
CODE
Invoice #12345          Date:12/12/1212

Name
Street
City, State, Zip
Phone

Caulk          1     12.00     12.00
Mortor Patch          1     16.00     16.00
Threshold, Wood With Vinyl          1     40.00     40.00
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      

                                                          Total $68.00



I would like it to be easier to read like this

CODE
Invoice #12345          Date:12/12/1212

Name
Street
City, State, Zip
Phone

Caulk                                     1     12.00     12.00
Mortor Patch                              1     16.00     16.00
Threshold, Wood With Vinyl                1     40.00     40.00
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      

                                                     Total $68.00



User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using Streamwriter To Write Text
2 Mar, 2008 - 02:14 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Well as I already mentioned you should use the PadRight/PadLeft functions to make it fixed width.

Here is an example using your code, showing what I mean. It is up to you to fix the rest of your code, just apply the same logic.

CODE

Sw.WriteLine(DescriptionBox2.Text.PadRight(40) & LocationBox2.Text.PadLeft(3) & qtybox2.Text.PadLeft(10) & PriceEachBox2.Text.PadLeft(10) & & LineTotalBox2.Text.PadLeft(10))
Sw.WriteLine(DescriptionBox3.Text.PadRight(40) & LocationBox3.Text.PadLeft(3) & qtybox3.Text.PadLeft(10) & PriceEachBox3.Text.PadLeft(10) & & LineTotalBox3.Text.PadLeft(10))


This will result in the following formatting:
CODE

Threshold, Wood With Vinyl                1     40.00     40.00
Mortor Patch                              1     12.00     12.00
Caulk                                     1     16.00     16.00

                                                          Total $68.00

User is offlineProfile CardPM
+Quote Post

zekeholmes
RE: Using Streamwriter To Write Text
2 Mar, 2008 - 02:39 PM
Post #9

New D.I.C Head
*

Joined: 19 Feb, 2008
Posts: 8


My Contributions
Thank you very much, the padright/padleft worked great. I set dims and strings to clean it up.

Thanks Again
jayman9 is the man
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month