I am new to VB, and am having problems with a program. I have a textbox where the user enters a grade value, then clicks tghe add button to display the grade in a listbox. The user continues the entries until all grades have been entered. I have also programmed a remove button so that the user could select a grade int he listbox and then click the remove button to delete it from the list. When the user clicks the calculate button, I determine the total number of entries (listgrades.item.count); this value is returned correctly. I then try to copy the contents of the listbox into an array as I thought that would be the best way to determine the highest, lowest, and sum of all the grades. I would then use the sum/total to get an average. I then need to assign a letter grade to each numeric grade and display the total number of grades in each category. I obviously am not doing this correctly, as the strallgrades has a zero value.
My code is below - any assistance would be appreciated!!

'to count the number of items in the listbox
total = LstGrades.Items.Count
Dim curindex As Integer
Dim strallgrades() As String
Dim intCounter As Integer
LstGrades.Items.Add(" ")
For intCounter = 0 To UBound(strgrade)
strallgrades(intCounter) = LstGrades.Items.Item(intCounter)
Next
For curindex = 0 To UBound(strgrade)
Debug.WriteLine(strgrade(curindex))
Next
Array.Sort(strgrade)
Array.Reverse(strgrade)
For curindex = 0 To UBound(strgrade)
Debug.WriteLine(strgrade(curindex))
Next
LblSum.Text = total
 End Sub

Recommended Answers

All 5 Replies

First of all, did you color the code yourself? If so, please don't. Using code tags properly will color it for you. it's [[B]code=vb[/B]] (no spaces) at the beginning, and at the end of the code [[B]/code[/B]]. Also, indenting your code would help a lot, too.

I can't figure out how your question and the code is related. You mention

When the user clicks the calculate button, I determine the total number of entries (listgrades.item.count); this value is returned correctly. I then try to copy the contents of the listbox into an array as I thought that would be the best way to determine the highest, lowest, and sum of all the grades.

Why are you defining strallgrades as a string if you want to find the lowest, highest, and sum? Wouldn't integers be more appropriate?

I would then use the sum/total to get an average.

Where is sum calculated? I expected to see a + somewhere in the code used to add the values together.

Update the code to add the grades together and see how that works. mIf it does, try calculating the average. If it doesn't, post again and we can tweak your attempt.

We also want to see how you are adding Items to Listbox, because if you don't have correct code then users can add empty items which might cause another error.

'use a list box and a form
'use the list box functin to add items to the list box
dim max, min,average,total as integer
'calculaindg maximum value
max=lstgrades.listindex(0)'i have set the max value to the first value in the list
for index as integer= 0 to lstgrades.listcount
   if max>lstgrades.listindex(1) then
         max=lstgrades.listindex(0)
   else
         max=lstgrades.listindex(1)
   endif
max=val(txtmax.text)
next index
'caculating minimum value
min=lstgrades.listindex(0)
for index as integer= 0 to lstgrades.listcount
     if min>lstgrades.listindex(1) then
         min=lstgrades.listindex(1)
     else
        min=lstgrades.listindex(0)
     endif
     min=val(txtmin.text)
  next index
'calculating sum and average
dim index, total,sum as integer
dim gradeletter as string
dim average as single
for index= 0 to lstgrades.listcount
   total=lstgrades.listindex(0)+total
next index
sum=val(txttotal.text)
average=val(sum/lstgrades.listcount+1)
 
'calculating gradeletter
 if average<40 then 
   gradeletter ="F"
 elseif average>=40 and<=60 then
   gradeletter="C"
 elseif average>60 and <=80 then 
   gradeletter="B"
 elseif average>80 and<=100 then 
    gradeletter="A"
 else 
    gradeletter= "grade error"
 endif
endif
endif
endif
gradeletter=str(txtgradeletter.text)


'lstgrades is the name of the list box

Kingwang, sarama... DO YOU REALIZE THAT THIS THREAD IS 4 YEARS OLD!!!!

hi its nice of u i guess i didn't check the date. but no big deal i still wanted to do it. see u around.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.