Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Adding Items to ListBox

I'm reading a file (scores.txt) into a program.  The file is a list of people with numbers after their name.  I read the name, calculate the averages of the numbers, and I want to output the Name and average to a listbox on the same line, then do the next name.

I am using ListBox.AddItem strName, is there a variation to add both at the same time.
Thanks in advance.
0
tobin46
Asked:
tobin46
  • 4
  • 3
  • 2
  • +5
1 Solution
 
Arthur_WoodCommented:
a VB listbox only has ONE item on the line, there is NO convenient way to create TRUE multiple columns.  You may want to look into using a LISTVIEW control instead, but that will involve a significant amount of additional coding, asthe method ot insert new items in a LISTVIEW is somewhat more involved that a simple "ADDITEM".

0
 
JMoon5FTMCommented:
In the unlikely case that the names are all about the same length, you may be able to seperate the name and average with a tab, like:

ListBox.AddItem strName & vbTab & avg

However, it's rather likely that the names won't be all the same, and the averages won't line up.  In that case, you will probably have to either use two ListBoxes or a ListView - the latter is recommended.  On the ListView property pages, set the view to "Report", and add two column headers - "Name" and "Average".  Then, you can add items like:

ListView.ListItems.Add(strName).SubItems(1) = avg
0
 
allstar12bradyCommented:
How do you take the information from the list box and put it in the file on the H drive (.dat)?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
allstar12bradyCommented:
using the put and get statements?
0
 
PaulHewsCommented:
To add to what has been said, if you don't want multiple columns, but just want both items to show on the same line, you would concatenate both items to a string like so:

ListBox.AddItem strName & ", " & cstr(sngAvg)

Then your list will look like this:

Alphonsus, 999
Joe, 99999
Shorty, 2030204

with no real columns, but all info present.
0
 
SweatCommented:
tobin46,

If you put a tab value (vbTab) between fixed length fields, you will obtain a column like effect.

Each name must be the same length, padding with spaces, then the tab, then the last column.

Dim stxt$, sLine$, iFileNum%, sName$, sScore$
Dim varArray() As String

iFileNum = FreeFile
Open "scores.txt" For Input As #iFileNum

Do Until EOF(iFileNum)
   Line Input #iFileNum, sLine
   varArray = Split(sLine, ",")
   sName = varArray(0)
   sScore = varArray(1)

   stxt = sName & Space$(25 - Len(sName))
   stxt = stxt & vbTab
   stxt = stxt & sScore

   List1.AddItem sName & Space$(25 - Len(sName)) & vbTab & sScore

Loop

You'll need to add the code to do the score calc, but this should give you a column effect in the List Box.

Sweat

0
 
SweatCommented:
tobin46,

Use either the stxt variable in the .Additem or spell it all out, but not both.

List1.AddItem sName & Space$(25 - Len(sName)) & vbTab & sScore

or

  stxt = sName & Space$(25 - Len(sName))
  stxt = stxt & vbTab
  stxt = stxt & sScore

  List1.AddItem stxt


Sorry.

Sweat

0
 
tobin46Author Commented:
Thanks to all!!!  I concatenated the two into a single string and used the vbTab to give it a column look.

Another part of the question that I have is, how do I now add these items to the listbox in order of the average?  In other words sorted by average.

The file is 50 employees with numbers after their name:
ex: John 50,50,40,40,30

I am able to now calculate the average and place these items into a listbox in alphabetical order.  
0
 
hazgodCommented:
try using 2 arrays.

dim names(80) as string
dim averages(80) as integer
dim thisName as string
dim thisAverage as string
dim num1 as integer
dim num2 as integer
dim num3 as integer
dim index as integer
dim count as integer

index = 0
open "scores.txt" for input as #1
  do until EOF
    input #1, thisName, num1, num2, num3 (etc)
    names(count) = thisName
    thisAverage = (num1 + num2 + num3) / 3
    averages(count) = thisAverage
    count = count + 1
  loop
close #1

you then have 2 list boxes (1 for names and 1 for averages).

for index = 0 to count
  listNames.additem names(index)
  listAverages.additem averages(index)
next


this code will put the names alone side the numbers. if you need to sort the list, add in 3 more vaiables (swapped as boolean and tempName as string and tempAverage as integer) then add this code before for index = 0 to count:

do while swapped = true
  swapped = false
  for index = 0 to count
    if averages(index) > average(index+1)
      tempAverage = average(index)
      average(index) = average(index+1)
      average(index+1) = tempAverage
      tempName = names(index)
      names(index) = names(index+1)
      names(index+1) = tempName
      swapped = true
    end if
  next
loop


hope this helps
0
 
hazgodCommented:
try using 2 arrays.

dim names(80) as string
dim averages(80) as integer
dim thisName as string
dim thisAverage as string
dim num1 as integer
dim num2 as integer
dim num3 as integer
dim index as integer
dim count as integer

index = 0
open "scores.txt" for input as #1
  do until EOF
    input #1, thisName, num1, num2, num3 (etc)
    names(count) = thisName
    thisAverage = (num1 + num2 + num3) / 3
    averages(count) = thisAverage
    count = count + 1
  loop
close #1

you then have 2 list boxes (1 for names and 1 for averages).

for index = 0 to count
  listNames.additem names(index)
  listAverages.additem averages(index)
next


this code will put the names alone side the numbers. if you need to sort the list, add in 3 more vaiables (swapped as boolean and tempName as string and tempAverage as integer) then add this code before for index = 0 to count:

do while swapped = true
  swapped = false
  for index = 0 to count
    if averages(index) > average(index+1)
      tempAverage = average(index)
      average(index) = average(index+1)
      average(index+1) = tempAverage
      tempName = names(index)
      names(index) = names(index+1)
      names(index+1) = tempName
      swapped = true
    end if
  next
loop


hope this helps
0
 
hazgodCommented:
try using 2 arrays.

dim names(80) as string
dim averages(80) as integer
dim thisName as string
dim thisAverage as string
dim num1 as integer
dim num2 as integer
dim num3 as integer
dim index as integer
dim count as integer

index = 0
open "scores.txt" for input as #1
  do until EOF
    input #1, thisName, num1, num2, num3 (etc)
    names(count) = thisName
    thisAverage = (num1 + num2 + num3) / 3
    averages(count) = thisAverage
    count = count + 1
  loop
close #1

you then have 2 list boxes (1 for names and 1 for averages).

for index = 0 to count
  listNames.additem names(index)
  listAverages.additem averages(index)
next


this code will put the names alone side the numbers. if you need to sort the list, add in 3 more vaiables (swapped as boolean and tempName as string and tempAverage as integer) then add this code before for index = 0 to count:

do while swapped = true
  swapped = false
  for index = 0 to count
    if averages(index) > average(index+1)
      tempAverage = average(index)
      average(index) = average(index+1)
      average(index+1) = tempAverage
      tempName = names(index)
      names(index) = names(index+1)
      names(index+1) = tempName
      swapped = true
    end if
  next
loop


hope this helps
0
 
hazgodCommented:
duno why it posted that 3 times
0
 
tobin46Author Commented:
looking for code for a simple paddle ball game to learn from.  Anybody have anything?
0
 
tobin46Author Commented:
looking for code for a simple paddle ball game to learn from.  Anybody have anything?
0
 
CleanupPingCommented:
tobin46:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
PaulHewsCommented:
Questioner says:
>Thanks to all!!!  I concatenated the two into a single string and used the vbTab to give it a column look<

Thus Points to JMoon5FTM

Questioner should have closed this on Mar 14, just about 4 months ago, but was distracted by oooh look a dog with a fluffy tail.
0
 
CleanupPingCommented:
tobin46:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
JMoon5FTMCommented:
Hey, I can't believe this old question is still open.  Will tobin46 be so kind as to give points to somebody - me, hazgod, Sweat - I don't care much at this point, but leaving the question open is rather rude.

If our dear questioner does not see fit to do something, I will have to make a post on the Community Support board.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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