?
Solved

Adding Items to ListBox

Posted on 2003-03-14
18
Medium Priority
?
238 Views
Last Modified: 2010-04-07
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
Comment
Question by:tobin46
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +5
18 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8138143
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
 
LVL 5

Expert Comment

by:JMoon5FTM
ID: 8138238
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
 

Expert Comment

by:allstar12brady
ID: 8138272
How do you take the information from the list box and put it in the file on the H drive (.dat)?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:allstar12brady
ID: 8138281
using the put and get statements?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8138456
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
 
LVL 2

Expert Comment

by:Sweat
ID: 8139335
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
 
LVL 2

Expert Comment

by:Sweat
ID: 8139354
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
 
LVL 1

Author Comment

by:tobin46
ID: 8140031
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
 
LVL 1

Accepted Solution

by:
hazgod earned 200 total points
ID: 8140050
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
 
LVL 1

Expert Comment

by:hazgod
ID: 8140074
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
 
LVL 1

Expert Comment

by:hazgod
ID: 8140080
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
 
LVL 1

Expert Comment

by:hazgod
ID: 8140086
duno why it posted that 3 times
0
 
LVL 1

Author Comment

by:tobin46
ID: 8140952
looking for code for a simple paddle ball game to learn from.  Anybody have anything?
0
 
LVL 1

Author Comment

by:tobin46
ID: 8141014
looking for code for a simple paddle ball game to learn from.  Anybody have anything?
0
 

Expert Comment

by:CleanupPing
ID: 8900079
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 8907085
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
 

Expert Comment

by:CleanupPing
ID: 9056309
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
 
LVL 5

Expert Comment

by:JMoon5FTM
ID: 9086610
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question