[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1450
  • Last Modified:

TAB(x) does not work in Streamwriter

Streamwriter doesn't seem to support TAB(x), where x is the numeric print column value.

Dim fs As New System.IO.FileStream("C:\Export.txt", FileMode.Create)
Dim sw As New StreamWriter(fs)

sw.WriteLine("Print Column 1", TAB(50), "Print Column 50")
sw.Close()

When I openthe file Export.txt and look at it notepad, only the text 'Print Column 1', the remaing text 'Print Column 50' does not show up in the file. What gives? Any way to make this work using streamwriter?

According to the VB.NET help the following works, but I'm figuring that the streamwriter class is the preferred way of writing to files and I want to use it instead of what's below, also I don't have to be concerned with the FreeFile thing.

FileOpen(1, "TESTFILE", OpenMode.Output) ' Open file for output.
' The second word prints at column 20.
Print(1, "Hello", TAB(20), "World.")
FileClose(1)

Thanks,
0
JEFFCECCHINI
Asked:
JEFFCECCHINI
  • 8
  • 3
  • 2
  • +2
1 Solution
 
arif_eqbalCommented:
Well I think WriteLine is not capable of handling it.
Actually when you look at the overloaded WriteLine, it is accepting a ParamArray, so you are able to pass it Three arguments, but the first arg should be the Format string which you are not providing so it displays just the first argument.

The Prin (& PrintLine) functions also take ParamArray but it might be coded to look for the TAB thing, while WriteLine does not do this.

If you look at the Documentation for TAB it says
"Used with the Print or PrintLine functions to position output."

so it might not be used with WriteLine

Now how to go about it, either use Print/PrintLine or duplicate the functionality of TAB.
0
 
Corey ScheichDeveloperCommented:
I wrote this in VB6 and seems to work

Sub main()
Debug.Print AddTabs("Hello", 50, "World!")
Debug.Print "Hello"; Tab(50); "World!"
End Sub


Function AddTabs(ParamArray Param() As Variant) As String
    Dim i As Variant
    Dim str As String
   
    For Each i In Param
        Select Case i.gettype
            Case "String"
                str +=  i
            Case "Integer"
                str += Space((i - 1) - Len(str))
            Case Else
                MsgBox "Haven't handled type " & i.gettype & "."
        End Select
    Next
    AddTabs = str
End Function


I am going to attempt to conver to vb.net

Function AddTabs(ParamArray Param() As Object) As String
    Dim i As Object
    Dim str As String
   
    For Each i In Param
        Select Case i.gettype
            Case "String", "System.String"
                str +=  i
            Case "Integer", "System.Integer"
                str += Space((i - 1) - str.length)
            Case Else
                MsgBox "Haven't handled type " & i.gettype & "."
        End Select
    Next
    AddTabs = str
End Function

HTH

Corey2
0
 
Corey ScheichDeveloperCommented:
Sorry the VB6 example should have looked like this

Function AddTabs(ParamArray Param() As Variant) As String
    Dim i As Variant
    Dim str As String
   
    For Each i In Param
        Select Case TypeName(i)
            Case "String", "System.String"
                str = str & i
            Case "Integer", "System.Integer"
                str = str & Space((i - 1) - Len(str))
            Case Else
                MsgBox "Haven't handled type " & TypeName(i) & "."
        End Select
    Next
    AddTabs = str
End Function
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
planoczCommented:
Well here is a work around....

 Private Sub TestTabPrint()
        Dim fs As New System.IO.FileStream("C:\Export.txt", FileMode.Create)
        Dim sw As New StreamWriter(fs)
        Dim t As String '<------- Make a Tabd holder
        t = ChrW(9) + ChrW(9) + ChrW(9) + ChrW(9) + ChrW(9) + ChrW(9) + ChrW(9) + ChrW(9) + ChrW(9)
        sw.WriteLine("Print Column 1" & t & "Print Column 50")
        sw.Close()

    End Sub
0
 
arif_eqbalCommented:
Hi everybody
What does the TAB function do ???
I think it just places the print position at a particular position
so suppose I say
Print "Hello", Tab(10), "World"
Print "Hi", Tab(10), "World"

output will be

Hello     World
Hi         World

I mean "World" will be printed from char position 10, so the no. of spaces between the first and the second word will automatically adjust.

so if we need to duplicate this I think we need to Adjust the space based on the previous arguments...

0
 
Corey ScheichDeveloperCommented:
That is what my example does.  It adds the number of spaces depending on how many characters are already in the string.

For example

Print "Hello", Tab(10), "World"
would add 4 spaces to put the "W" in world at the 10th character collumn.

Print "Hi", Tab(10), "World"
would add 7 spaces to put the "W" in world at the 10th character collumn.

This will work for fonts with fixed character width.  Where "i" has the same width as "W"

In this case the OP is viewing his output in Note pad which definitely has fixed width font.  If he wanted to use WordPad or fonts there should have been mention of it and he would have to output richtext or something and the approach is completely different.

Corey2
0
 
Corey ScheichDeveloperCommented:
Oh one thing I forgot to handle is if the current line is past the tab count create a new line.
I also commented it so it is easier to read

Function AddTabs(ParamArray Param() As Object) As String
    Dim i As Object
    Dim str As String
   
   'loop through the parameter array that was supplied
    For Each i In Param
        Select Case i.gettype
            Case "String", "System.String"
               'if supplied value is a string add it to the build string

                str +=  i

            Case "Integer", "System.Integer"
                'if the supplied value is an integer index the line
                'to the correct column

                Dim strSplit() as String = str.split(vbCRLF)
                if strSplit(Ubound(strSplit)) >= i then
                    'if the last line is past the supplied number of spaces
                    'create an new line and index it to the correct column

                     str += vbCRLF & Space(i - 1)

                else
                    'if not just index to the correct collumn in the current line

                     str += Space((i - 1) - str.length)

            Case Else
                'just incase you want to enable the allowance of another type
                'this will display a message showing you the type name you
                'tried to supply that caused this to not work properly

                MsgBox "Haven't handled type " & i.gettype & "."

        End Select
    Next
   'Return Str
    AddTabs = str
End Function
0
 
arif_eqbalCommented:
Nice Corey2
0
 
rspahitzCommented:
Also look into the flush method to ensure that the data is all sent before closing:

sw.WriteLine("Print Column 1", TAB(50), "Print Column 50")
sw.Flush()
sw.Close()
0
 
JEFFCECCHINIAuthor Commented:
Sorry about the delay in answering, I've been swamped. I appreciate the effort.

As a someone new to EE and have up to now searched answers first before asking, I have been a bit disappointed that potential solutions aren't tested by their authors first. That would make responses come faster from the recipient, since he/she wouldn't have to test so many possibilities. It would make for a higher quality environment for us all.
 
arif_eqbal: you seem correct on both accounts.

planocz: Sorry, your solution simply adds that tab character, which is no problem, I can accomplish that by sw.WriteLine("Some Text" & vbTab & "Some more text"), it's the print column placement of the next text I'm interested in.

rspahitz: Sorry, adding the flush did NOT work; I tried that.

Corey2: Like the idea of endless parameters: String, PrintColumn,..., however, the IDE gave error on your Select statement "i.gettype", cannot be of type 'System.Type' I tried to convert to if/then
If i.gettype = System.String, but it did not like that either.

I just went ahead and made this workaround which seems ok:
        Dim fs As New System.IO.FileStream("C:\sw.txt", FileMode.Create)
        Dim sw As New StreamWriter(fs)
        sw.Write(FormatTheStringExactly("Shorter", 10))
        sw.Write(FormatTheStringExactly("MuchLongerThan", 10))
        sw.WriteLine(FormatTheStringExactly("ExactlyIt!", 10))
        sw.Flush()
        sw.Close()
        Process.Start("notepad.exe", "C:\sw.txt")


    Private Function FormatTheStringExactly(ByVal TheString As String, ByVal NumberOfCharacters As Integer) As String
        If TheString.Length = NumberOfCharacters Then
            'Do Nothing, it's good the way it is
        ElseIf TheString.Length < NumberOfCharacters Then
            TheString = TheString.PadRight(NumberOfCharacters)
        Else 'The String is longer
            TheString = TheString.Substring(0, NumberOfCharacters)
        End If

        Return (TheString)
    End Function

which has the output as:

Shorter   MuchLongerExactlyIt!
0
 
Corey ScheichDeveloperCommented:
Corey2: Like the idea of endless parameters: String, PrintColumn,..., however, the IDE gave error on your Select statement "i.gettype", cannot be of type 'System.Type' I tried to convert to if/then

Change it to I.gettype.tostring

I would test my code but durring the day I am not at my developement computer.  I wish I could though.

Corey2
0
 
Corey ScheichDeveloperCommented:
With another look I saw a couple of errors.

Function AddTabs(ParamArray Param() As Object) As String
    Dim i As Object
    Dim str As String
   
   'loop through the parameter array that was supplied
    For Each i In Param
        Select Case i.gettype.tostring   '<----added tostring for the right type
            Case "String", "System.String"
               'if supplied value is a string add it to the build string

                str +=  i

            Case "Integer", "System.Integer"
                'if the supplied value is an integer index the line
                'to the correct column

                Dim strSplit() as String = str.split(vbCRLF)
                if strSplit(Ubound(strSplit)).Length >= i then '<----Added .length to compare the length not the string
                    'if the last line is past the supplied number of spaces
                    'create an new line and index it to the correct column

                     str += vbCRLF & Space(i - 1)

                else
                    'if not just index to the correct collumn in the current line

                     str += Space((i - 1) - strSplit(Ubound(strSplit)).Length)'<---needed to only subtract the length of the last line not the entire string.

            Case Else
                'just incase you want to enable the allowance of another type
                'this will display a message showing you the type name you
                'tried to supply that caused this to not work properly

                MsgBox "Haven't handled type " & i.gettype.tostring & "." '<----added tostring for the right type

        End Select
    Next
   'Return Str
    AddTabs = str
End Function
0
 
Corey ScheichDeveloperCommented:
I feel that the OP recieved adequate help to select a solution there are plenty of good Ideas for overcoming this problem.

Corey2
0
 
Corey ScheichDeveloperCommented:
Thank you kind Sir.
0
 
JEFFCECCHINIAuthor Commented:
Hi Corey2

You did provide other ideas/options, unfortunately after I ended up doing something else as described in my post on 05/07. I was forced to come up with something in a short timeframe.

Consequently, I did not try your later post, so that is why I didn't award the points. As a newbie, I didn't understand how to deal with that situation.

Thanks so much for the effort.

Jeff
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now