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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display data in columns

Status
Not open for further replies.

peterd51

Technical User
Feb 22, 2005
109
GB
Hi,

I have some data, held on the hard-drive in a .csv file. At start-up I read this in to an array and when the user hits a command button I want to show the data in a grid or tabulated screen that the user can scroll up and down and select one item at a time.

I've poked around with this for a day or two and come up with the code below.

code start------------------------------------
myTab = vbTab & " "

lstDisplay.Visible = True

For n = 1 To 100
'name
xfer2 = Trim(myData(n, 1)) & " "
xfer2 = Left(xfer2, 18)
xfer1 = xfer2 & myTab

'price
xfer2 = Trim(myData(n, 2)) & " "
xfer2 = Left(xfer2, 12)
xfer1 = xfer1 & xfer2 & myTab

'quantity
xfer2 = Trim(myData(n, 3)) & " "
xfer2 = Left(xfer2, 10)
xfer1 = xfer1 & xfer2 & myTab

'tax
xfer2 = Trim(myData(n, 4)) & " "
xfer2 = Left(xfer2, 10)
xfer1 = xfer1 & xfer2

lstDisplay.AddItem xfer1
Next n
code end------------------------------------

xfer2 gets mydata and adds fifteen spaces (I tried various figures here), then gets trimmed to a suitable length and added to xfer1. When I've got all the data for one line I add it to the listbox.

It sort of works, bearing in mind my slightly dodgy code (I learnt on QuickBasic 30 years ago!)

From time to time, if the 'name' is quite long to start with, it throws the rest of the columns out but only on that line. The next line shows as normal. But why doesn't it get limited when I trim it to length?

Is there something that I'm missing, or can anyone suggest a better way of displaying upto maybe 400 lines, of up to nine columns, and still allow the user to click to select one item?

Regards
Peter
 
There are a number of grid controls you could use. Most have click events to permit selection.
 
Maybe the problem might be when you read the file into the array.
Can you put a breakpoint after your next n and examine the array contents in question in the immediate window?
Eg assuming the problem is at item 52,
for a=1 to 4:? myData(52, a)):next


Maybe the longer names have a comma in them so your array code thinks it is an extra field?
 
Hi,

I looked at grids but can't see a simple way of using one for this. One of the books that I have shows how to set one up and link to a database file. A specified it a 'text' and assumed it would find the headers and auto set up the right number of columns but it didn't display anything. There doesn't seem to be a way of speciying the number of columns.

I might have to play with one for a couple of days to see what I can get out of it. Do some more research, there's probably something on the net that'll work.

There's definately nothing in the data that shouldn't be there as the file's already been set up by a web-browser sub-routine. It would have caused problems there if there was anything that shouldn't be in it.

Thinking about this on the way in to work this morning, I was wondering if it's something to do with the number of 'spaces' that I'm using. Difficult to explain in text, but maybe I need to count the characters, gaps, etc and make sure I've got the page balanced out...enough room to display it, etc.

I'll go to multiple listboxes if I have to, I've used three linked together before.

Regards
Peter
 
>But why doesn't it get limited when I trim it to length

Erm ... what font are you using? The default font for a textbox is a proportional font, which means that whilst it may be trimmed to a particualr number of characters it is not trimmed to a particular display width since different characters take up different amounts of space, e.g. here on tek-tips

Illustration
Magestically

or in a VB textbox:

' Text1 needs to be multiline
Text1.Text = "Illustration" & vbCrLf & "Magestically"

 

How about displaying your data in MSHFlexGrid?
[tt]
Components - Microsoft Hierarchical FlexGrid Control 6.0
[/tt]

Here is a little sample of how it works:
Code:
Option Explicit

Private Sub Form_Load()
Dim r As Integer

With MSHFlexGrid1
    .AllowUserResizing = flexResizeColumns
    .Cols = 5
    .Rows = 8
    .FormatString = " Zero |< First |< Second |< Third |< Fourth "
    For r = 1 To .Rows - 1
        .TextMatrix(r, 0) = "Item in row " & r & " col 0"
        .TextMatrix(r, 1) = "Row " & r & " col 1"
        .TextMatrix(r, 2) = "Row " & r & " col 2"
        .TextMatrix(r, 3) = "Row " & r & " col 3"
        .TextMatrix(r, 4) = "Row " & r & " col 4"
    Next r
End With

End Sub

Private Sub MSHFlexGrid1_Click()
Dim c As Integer
Dim r As Integer

With MSHFlexGrid1
    .Col = .MouseCol
    .Row = .MouseRow
    MsgBox "You clicked on " & .Text
End With

End Sub

Have fun.

---- Andy
 
Hi Strongm,

yes, I was playing around with it earlier this evening and realised it was the font. Specifically it didn't like the variable spaces.

Andrzejek:
I had another search for 'grids' this afternoon and saw 'flexi-grids' mentioned once. I've never heard of these before but just had a couple of hours playing around with one and it does what I want.

My grid is a type 5.0 so I'm not sure if your code will work with it yetbut it gives me a bit more to play with, thanks.

How about justifying the columns? Most of the text columns need to be centralised while currency columns need to be on the right. There's nothing in the controls for that and everything hangs to the left as it is now.

Regards
Peter

 

Looks like my code is working for you, Great! :)

"justifying the columns":
Code:
Dim r As Integer

With MSHFlexGrid1
    .AllowUserResizing = flexResizeColumns
    .Cols = 5
    .Rows = 8
    .FormatString = " Zero |< First |< Second |< Third |< Fourth "
    For r = 1 To .Rows - 1
        .TextMatrix(r, 0) = "Item in row " & r & " col 0"
        .TextMatrix(r, 1) = "Row " & r & " col 1"
        .TextMatrix(r, 2) = "Row " & r & " col 2"
        .TextMatrix(r, 3) = "Row " & r & " col 3"
        .TextMatrix(r, 4) = "Row " & r & " col 4"
    Next r
    [blue]
    .ColAlignment(0) = flexAlignLeftCenter
    .ColAlignment(1) = flexAlignCenterCenter
    .ColAlignment(2) = flexAlignRightCenter
    [/blue]
    .ColWidth(0) = .Width * 0.2
    .ColWidth(1) = .Width * 0.2
    .ColWidth(2) = .Width * 0.2
    .ColWidth(3) = .Width * 0.2
    .ColWidth(4) = .Width * 0.2
End With

Have fun.

---- Andy
 

BTW, these little [blue]BLUE [/blue]characters will allign text in your header:
Code:
.FormatString = " Zero |[blue]^[/blue] First |[blue]>[/blue] Second |[blue]<[/blue] Third |[blue]<[/blue] Fourth "

Have fun.

---- Andy
 
Hi

thanks, I'd never have found these just by poking around with the program!

I sometimes think that MS want to keep this sort of info top secret as I have major problems trying to find it.

Regards
Peter
 
Seriously, most of the information is right in the VB6 documentation. You should have a copy of the MSDN Library on CDs that is contemporary with VB6 for doing these sorts of things. There are also versions of the manuals in hardcopy form, but it seems like few people have those anymore. I think the basic set is 3 thick volumes, but the print is pretty large in them and there is a lot of whitespace.
 
Hi Dilettante,

I have VB5 but I recall that it has a second CD that I've never looked at. Maybe I should take a look but I assumed this would be available on the net. I can't seem to find the right search words to get anything usefull out of the MS site, and a google search finds just a couple of bits for VB5 and thousands for VB6 and VB.net.

The code works now that I've used the US spelling for 'centre'... it only took an hour to figure out why 'left' and 'right' worked but 'centre' didn't! :)

Regards
Peter
 

So the [tt][blue] flexAlignCenterCenter [/blue][/tt] did not work for you?

Have fun.

---- Andy
 
Hi,

yes, eventually.

But I spent a bit of time trying to figure out why

flexAlignCentreCentre didn't work.
^^ ^^

Then I realised I was spelling it the English way rather than the American way.

Muppetry, I reckon...

Regards
Peter

 

That's why I ALWAYS have Option Explicit at the top of my code and I type all code in lower case (Except when I declare any variables or procedures). This way - if I make any mistakes - my variables, called procedures or reserved words do not change case and I know I did something wrong (without compiling my code).

Have fun.

---- Andy
 
Well I can see the confusion. VB5 came with an old-style separate Help along with samples and such on that second CD.

But you have to remember that VB5 had a very short life: it came out in early 1997 and was replaced by VB6 in late 1998. Old versions of a product tend to get dropped off MSDN fairly quickly, and VB.Net was a new product rather than a new version.

A lot of people feel they're really similar, but a lot changed in VB6. Product Documentation
 
Hi Andy,

yes, I have option explicit on too, but in this case it capitalised where I thought is should and looked OK bearing in mind I've not seen the command before.

It's working now, thanks!

Dilettante,

I didn't realise it was that short! I bought VB6 but never opened the packet. VB.net seems like overkill to me as I generally push a bit of data around a local PC. The only time I touch the internet with a program is to download data with Webbrowser.

How long before MS bring out yet another new programming language/version of VB? It seems to me that it's a lot to do with marketting rather than a direct need for new software.

Anyhow, I'm off on holiday tomorrow, taking a laptop and that second CD to do a bit of reading and programming.

Regards
Peter




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top