×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PRINT listview in tabular and formatted text
3

PRINT listview in tabular and formatted text

PRINT listview in tabular and formatted text

(OP)
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?

RE: PRINT listview in tabular and formatted text

Dim listy As ListItem

For Each listy In ListView1.ListItems
Debug.Print listy; Tab(34);
Debug.Print Spc((3 - Len(listy.ListSubItems(1))) / 2); listy.ListSubItems(1); Tab(38);
Debug.Print Spc((3 - Len(listy.ListSubItems(2))) / 2); 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)
Next

RE: PRINT listview in tabular and formatted text

(OP)
no dubt!
Stringm you are a genius!
Tks.

RE: PRINT listview in tabular and formatted text

(OP)
OPS...

possible to print also a header title of each column of listview , to the head of row?

RE: PRINT listview in tabular and formatted text

    Dim listy As ListItem
    
    With ListView1
        Debug.Print .ColumnHeaders(1); Tab(34);
        Debug.Print Spc((3 - Len(.ColumnHeaders(2))) / 2); .ColumnHeaders(2); Tab(38);
        Debug.Print Spc((3 - Len(.ColumnHeaders(3))) / 2); .ColumnHeaders(3); Tab(41);
        Debug.Print Spc(10 - Len(.ColumnHeaders(4))); .ColumnHeaders(4); Tab(51);
        Debug.Print Spc(10 - Len(.ColumnHeaders(5))); .ColumnHeaders(5)
    End With

    
    For Each listy In ListView1.ListItems
        Debug.Print listy; Tab(34);
        Debug.Print Spc((3 - Len(listy.ListSubItems(1))) / 2); listy.ListSubItems(1); Tab(38);
        Debug.Print Spc((3 - Len(listy.ListSubItems(2))) / 2); 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)
    Next 

RE: PRINT listview in tabular and formatted text

(OP)
TKS Strongm!

RE: PRINT listview in tabular and formatted text

Instead of ListView, I would be tempted to do it with MSFlexGrid (or MSHFlexGrid) instead.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: PRINT listview in tabular and formatted text

sal21 loves their Listviews ... smile

RE: PRINT listview in tabular and formatted text

(OP)
yes strongm, i love itbigsmile

RE: PRINT listview in tabular and formatted text

Quote (Maslow)

I suppose it is tempting, if the only tool you have [know] is a hammer, to treat everything as if it were a nail.

Myself, I like to use a screwdriver from time to time when screws are used, or a wrench when nuts and bolts are involved. smile

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: PRINT listview in tabular and formatted text

I don't disagree, Andy. But why - in this specific case - do you think an MSFlexgrid would be a better choice?

RE: PRINT listview in tabular and formatted text

MSFlexgrid may not be a 'better' choice, just a different approach to display the data for the user to choose from. And there are a lot of help on the Web of how to use it.

sal21 said: "i love it" ('it' being Listviews) I love (well, like) to use controls I am familiar with, I know how to use them and don't have to ask for help. [hint]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: PRINT listview in tabular and formatted text

(OP)
I can have to the left, of printer.print, this value:

strvar="PAGAMENTO ELETTRONICO:"
or
strvar="PAGAMENTO CONTANTI:"
or
strvar="RESTO:"

ecc...

and a var strnumber="0,25"

the max leght of string, to print, is 60.

now, based the strvar, i need to printer.print, similar:

strvar<(align to the left) strnumber<(to align to the right)

naturally strnumber have a variable lenght(11,58, 5,48, 650,45 ...ecc)

how to?




RE: PRINT listview in tabular and formatted text

Hard to decipher your requirements, but my guess would be:

CODE

Const MAX_LENGTH As Integer = 60
Dim strvar As String
Dim strnumber As String

strvar = "RESTO:"
strnumber = "0,25"
 
Debug.Print strnumber & Space(MAX_LENGTH - (Len(strvar) + Len(strnumber))) & strvar 

To get as an outcome:
0,25                                                  RESTO: 

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: PRINT listview in tabular and formatted text

(OP)
Andy, tks.
but i need eaxclly the opposite:

RESTO: 0,25

RE: PRINT listview in tabular and formatted text

Swap it around:

CODE

Debug.Print strvar & Space(MAX_LENGTH - (Len(strvar) + Len(strnumber))) & strnumber 

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: PRINT listview in tabular and formatted text

Or

CODE -->

Debug.Print strvar & Format(strnumber, String(MAX_LENGTH - Len(strvar), "@")) 

RE: PRINT listview in tabular and formatted text

andy & strongm...
my angels!
tks

2009luca and sal21, are
it's always me.

have 2 accountsmile

RE: PRINT listview in tabular and formatted text

(OP)
POSSIBLE to print this lines in listbox... instaed to send a printer

...
Printer.Print .ColumnHeaders(1).Text; Tab(34);
Printer.Print Spc((3 - Len(.ColumnHeaders(2).Text)) / 2); .ColumnHeaders(2).Text; Tab(38);
Printer.Print Spc((3 - Len(.ColumnHeaders(3).Text)) / 2); .ColumnHeaders(3).Text; Tab(41);
Printer.Print Spc(10 - Len(.ColumnHeaders(4).Text)); .ColumnHeaders(4).Text; Tab(51);
Printer.Print Spc(10 - Len(.ColumnHeaders(5).Text)); .ColumnHeaders(5).Text
....

RE: PRINT listview in tabular and formatted text

Is that a question? If so, yes, you can send to a printer instead. Need to finish with Printer.EndDoc

RE: PRINT listview in tabular and formatted text

(OP)
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);

RE: PRINT listview in tabular and formatted text

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

    Dim listy As ListItem
    
    With ListView1
        Debug.Print .ColumnHeaders(1); Tab(35); ' corrected from previous version
        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)
        
        ' Alternative version that actually builds a string
        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(35); ' corrected from previous version
        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)
        
        ' Alternative version that actually builds a string
        Debug.Print Format(listy, "!" & String(34, "@")) & Format(listy.ListSubItems(1), "!@@@") & Format(listy.ListSubItems(2), "!@@@") & Format(listy.ListSubItems(3), "@@@@@@@@@@") & Format(listy.ListSubItems(4), "@@@@@@@@@@")
    Next
     

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 ...


RE: PRINT listview in tabular and formatted text

(OP)
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!

RE: PRINT listview in tabular and formatted text

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: A Format function for VbScript)

RE: PRINT listview in tabular and formatted text

(OP)
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

RE: PRINT listview in tabular and formatted text

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

RE: PRINT listview in tabular and formatted text

(OP)
Andrzejek.ok
modified

RE: PRINT listview in tabular and formatted text

"i need to center the value in ID and QTY"

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

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: PRINT listview in tabular and formatted text

Define: center?

RE: PRINT listview in tabular and formatted text

>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?

.9..
..9.


2 and 4 digits can be centered

.99.
9999


And three digits can be 'centered' in two positions
.999
999.

RE: PRINT listview in tabular and formatted text

Guessing here...

You have a column ID, which is 2 characters.
To 'Center' a 2 digits ID, it would be:
 ID
----
 10 

To 'Center' a 4 digits ID, it would be:
 ID
----
1100 

How do you 'center' 1 digit? Or 3 digits?

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: PRINT listview in tabular and formatted text

(OP)
strongm,
yes!

2 and 4 digits can be centered for ID

.99.
9999

RE: PRINT listview in tabular and formatted text

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

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

RE: PRINT listview in tabular and formatted text

(OP)
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!!!!

RE: PRINT listview in tabular and formatted text

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)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close