Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PRINT listview in tabular and formatted text 3

Status
Not open for further replies.

sal21

Programmer
Apr 26, 2004
429
IT
i have a listview with data in image.

i need to send the value to the printer in a tabular mode...

the param are:

max lenght for each row 60 character max

for

REPARTO max lenght 34 align to the left
ID max lenght 3 align to the center
qty max lenght 3 align to the center
PREZZO max lenght 10 align to the right
TOT. max lenght 10 align to the right

max lenght of each row 34+3+3+10+10

possible?
 
 https://files.engineering.com/getfile.aspx?folder=ed023410-f725-48e3-8ca2-e990f8f18037&file=SCO.jpg
but really, i can assign to the var STRINGA (As String) the code line:

Printer.Print Spc((3 - Len(.ColumnHeaders(2).Text)) / 2); .ColumnHeaders(2).Text; Tab(38);

similar:

STRINGA = .Print Spc((3 - Len(.ColumnHeaders(2).Text)) / 2); .ColumnHeaders(2).Text; Tab(38);
 
Not quite, since to build a string we need to feed it stings. But Tab(n) is not a string. So you need to build the string differently - converting Tab(n) - which is an absolute position - with Space(m) where you'd have to calculate m based on your current position inthe string and where you want to move to.

We can build it via a nUmber of Format functions, eg:

STRINGA = Format(listy, "!" & String(34, "@")) & Format(listy.ListSubItems(1), "@@@") & Format(listy.ListSubItems(2), "@@@") & Format(listy.ListSubItems(3), "@@@@@@@@@@") & Format(listy.ListSubItems(4), "@@@@@@@@@@")


And here's that inserted into my example code duplicating the original method. There are also a couple of minor tweaks:
1) FRixed a positioning bug, as I used wrong tab for first parameter
2) Removed some of the 'centering' code, as with only a 3 character space in a fixed width solution it really didn't do very much useful

Code:
[blue]    Dim listy As ListItem
    
    With ListView1
        Debug.Print .ColumnHeaders(1); Tab([b][COLOR=red]35[/color][/b]); [COLOR=green]' corrected from previous version[/color]
        Debug.Print .ColumnHeaders(2); Tab(38);
        Debug.Print .ColumnHeaders(3); Tab(41);
        Debug.Print Spc(10 - Len(.ColumnHeaders(4))); .ColumnHeaders(4); Tab(51);
        Debug.Print Spc(10 - Len(.ColumnHeaders(5))); .ColumnHeaders(5)
        
        [COLOR=green]' Alternative version that actually builds a string[/color]
        Debug.Print Format(.ColumnHeaders(1), "!" & String(34, "@")) & Format(.ColumnHeaders(2), "!@@@") & Format(.ColumnHeaders(3), "!@@@") & Format(.ColumnHeaders(4), "@@@@@@@@@@") & Format(.ColumnHeaders(5), "@@@@@@@@@@")
    End With
        
    
    For Each listy In ListView1.ListItems
        Debug.Print listy; TabTab([b][COLOR=red]35[/color][/b]); [COLOR=green]' corrected from previous version[/color]
        Debug.Print listy.ListSubItems(1); Tab(38);
        Debug.Print listy.ListSubItems(2); Tab(41);
        Debug.Print Spc(10 - Len(listy.ListSubItems(3))); listy.ListSubItems(3); Tab(51);
        Debug.Print Spc(10 - Len(listy.ListSubItems(4))); listy.ListSubItems(4)
        
        [COLOR=green]' Alternative version that actually builds a string[/color]
        Debug.Print Format(listy, "!" & String(34, "@")) & Format(listy.ListSubItems(1), "!@@@") & Format(listy.ListSubItems(2), "!@@@") & Format(listy.ListSubItems(3), "@@@@@@@@@@") & Format(listy.ListSubItems(4), "@@@@@@@@@@")
    Next
    [/blue]

It's be great if we could use some sort of single formatting function to feed strings, alignment and position to be built into a final string, but the VB(A) format function isn't quite that flexible ... but VB.NET has such a flexible, powerful formatting capability. If only we could use it somehow ...


 
GREAT!

but have a dubt...

you fixed a position of first element to 35, and i see in Format(.ColumnHeaders(1), "!" & String(34, "@"))... 34, is correct?

the code work in other case!
 
Yes, they work slightly differently - one represents the next print position, the other represents the length of the string.

So, if a string is 34 characters long, the next position would be 35

Oh, and no-one biting on the "If only we could use it somehow"? It was a leading question ... (although it leads into slightly esoteric areas; for those that want a hint, have a look at my code in thread329-1570966)
 
Strongm,
sorry me, but prob to center text

Code:
...
 With TICKET!LVSCONTRINO
        STRINGA = Format(.ColumnHeaders(1), "!" & String(32, "@")) & Format(.ColumnHeaders(2), "!@@@@") & Format(.ColumnHeaders(3), "!@@@@") & Format(.ColumnHeaders(4), "@@@@@@@@@@") & Format(.ColumnHeaders(5), "@@@@@@@@@@")
        Me.RichTextBox1.SelText = Me.RichTextBox1.SelText & STRINGA & vbCrLf
    End With

    Me.RichTextBox1.SelText = Me.RichTextBox1.SelText & "------------------------------------------------------------" & vbCrLf

    With TICKET!LVSCONTRINO
        For K = 0 To UBound(STRDBROWS, 2)
            STRPRZ = Format(STRDBROWS(7, K), "#,##0.00")
            STOTALE = Format(STRDBROWS(9, K), "#,##0.00")
            STRINGA = Format(STRDBROWS(4, K), "!" & String(32, "@")) & Format(STRDBROWS(5, K), "!@@@@") & Format(STRDBROWS(8, K), "!@@@@") & Format(STRPRZ, "@@@@@@@@@@") & Format(STOTALE, "@@@@@@@@@@")
            PRZ = PRZ + STRDBROWS(9, K)
            NRPZ = NRPZ + STRDBROWS(8, K)
            Me.RichTextBox1.SelText = Me.RichTextBox1.SelText & STRINGA & vbCrLf
        Next K
    End With 

...

i have modifiyed the code, because instead 3 digit i need 4 digit, always for 60 lenght string.

tghe valuye not is centered... see image

thevalue are in:

REPARTO ID QTY PREZZO TOT.
------------------------------------------------------------
13-NOLEGGIO SPIAGGIA 101 2 15,00 30,00
1-CAFFETTERIA 1 4 1,00 4,00

i need to center the value in ID and QTY

note:
this code print value in a richtextbox
 
 https://files.engineering.com/getfile.aspx?folder=ab8c2f65-2ac3-4389-8327-82ef114caaea&file=SCO.jpg
sal21 / 2009luca,
By now you probably have noticed that everybody here shows you code formatted as code. It is a lot easier to read than just simple text, and you can differentiate between 'explanation' and 'code' in the posts.

Why don't you format your code as CODE... [ponder]



---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
i need to center the value in ID and QTY"

[pre]
REPARTO [red]ID QTY[/red] PREZZO TOT.
------------------------------------------------------------
13-NOLEGGIO SPIAGGIA [red]101 2[/red] 15,00 30,00
1-CAFFETTERIA [red]1 4[/red] 1,00 4,00
[/pre]
Define: center

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
>i need to center the value in ID and QTY

You sure? I ask because it isn't actually possible to center if using a fixed width font and print statements; I skated over that in earlier solutions, because with a 3 char column it isn't really noticeable that we didn't really center it (we just decided to go with left aligned)

For example

Single digit can be 'centered' in two positions - which would you want?

[tt].9..
..9.[/tt]

2 and 4 digits can be centered

[tt].99.
9999[/tt]

And three digits can be 'centered' in two positions
[tt].999
999.[/tt]
 
Guessing here...

You have a column ID, which is 2 characters.
To 'Center' a 2 digits ID, it would be:[blue]
[pre]
ID
----
10[/pre][/blue]
To 'Center' a 4 digits ID, it would be:[blue]
[pre]
ID
----
1100[/pre][/blue]
How do you 'center' 1 digit? Or 3 digits?

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
strongm,
yes!

2 and 4 digits can be centered for ID

.99.
9999
 
Is it just me? But looks like your second [tt]With TICKET!LVSCONTRINO... End With[/tt] does not do anything, does not refer anywhere to [tt]TICKET!LVSCONTRINO[/tt]

What I would do is:
Code:
With TICKET!LVSCONTRINO
    STRINGA = Format(.ColumnHeaders(1), "!" & String(32, "@")) & _
        Format(.ColumnHeaders(2), "!@@@@") & _
        Format(.ColumnHeaders(3), "!@@@@") & _
        Format(.ColumnHeaders(4), "@@@@@@@@@@") & _
        Format(.ColumnHeaders(5), "@@@@@@@@@@")
End With

With Me.RichTextBox1
    .SelText = .SelText & STRINGA & vbCrLf
    .SelText = .SelText & String(60, "-") & vbCrLf
End With

For K = 0 To UBound(STRDBROWS, 2)
    STRPRZ = Format(STRDBROWS(7, K), "#,##0.00")
    STOTALE = Format(STRDBROWS(9, K), "#,##0.00")
    STRINGA = Format(STRDBROWS(4, K), "!" & _
        String(32, "@")) & _
        Format(STRDBROWS(5, K), "!@@@@") & _
        Format(STRDBROWS(8, K), "!@@@@") & _
        Format(STRPRZ, "@@@@@@@@@@") & _
        Format(STOTALE, "@@@@@@@@@@")
    PRZ = PRZ + STRDBROWS(9, K)
    NRPZ = NRPZ + STRDBROWS(8, K)

    With Me.RichTextBox1
        .SelText = .SelText & STRINGA & vbCrLf
    End With
Next K

Besides of renaming RichTextBox1 to something more meaningful, and use more standardized naming conventions, like strDBRows, strPRZ, strTotale (STOTALE), strA (STRINGA ) etc.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Andrzejek, yes!

I jhave see the second with dont refer to nothing, correct!

i cannot test now your code... i'm busy, wiht my wife in cucine! Pizza!!!!
 
Of course, now you are using a Rich Textbox we have a number of new options - such as setting our own tab stops in the control, with whatever alignment we want (although to be fair this is not straightforward)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top