473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

need help with listbox items

18
I am trying to calculate the class average of student's grades. the student names are displayed in a list box, while their corresponding grade is displayed in a separate list box. calculate the average: # of students/ sum of their grades.(grades are numeric)

EX: John 90
sally 88
mike 75

I can't get it to add the sum of the grades i.e. (90+88+75). Also, I won't know how many students and grades the user will enter into the list. so it has to be able to add any number of grades.
any help would be great. thanks.
Nov 12 '08 #1
8 2551
QVeen72
1,445 Expert 1GB
Hi,

You can Loop through and find the Average:

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim TMarks As Long
  3. TMarks =0 
  4. txtAvg.Text = "0.00"
  5. For i = 0 To lstMarks.ListCount-1
  6.     TMarks = TMarks + Val(lstMarks.List(i))
  7. Next
  8. If lstMarks.ListCount >0 Then
  9.     txtAvg.Text = Format((TMarks/lstMarks.ListCount),"0.00")
  10. End If
  11.  
Regards
Veena
Nov 12 '08 #2
jac130
18
Thanks a lot, that helped. I also am having trouble with one other aspect. once the names and grades are displayed (in separate list boxes), there is a button, prompting the user to a certain grade, I must display a message box listing all of the student names below that certain grade. I've looped it to ask the user/ validate input, but it crashes before displaying the message box with all the names. Any thoughts? Here is a portion of my code:

Function GetBelowGrade() As Double
Dim onegrade As String = InputBox("please enter a positive number for the grade.")
Do While Not IsPositiveNumber(onegrade)
onegrade = InputBox("please enter a positive number for the grade.")
Loop
Dim i As Integer
For i = 0 To lstGrades.Items.Count - 1
Next

If lstGrades.Items(i) < onegrade Then
Return (MsgBox(lstStudents.Items(lstGrades.Items(i))))
End If
End Function

Private Sub btnBelowGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBelowGrade.Click
GetBelowGrade()

End Sub
End Class

any help would be great
Nov 13 '08 #3
debasisdas
8,127 Expert 4TB
why using inputbox twice ?

your variable is a string ,which you are using to compare using < perator without any conversion.
Nov 13 '08 #4
jac130
18
I changed it to this. but it says "onegrade" and "i" are used before they are assigned a value and could result in a null reference exception. Does that not matter?
i changed it to a string and it still crashes.
Function GetBelowGrade() As Double
Dim onegrade As String
Do While Not IsPositiveNumber(onegrade)
onegrade = InputBox("please enter a positive number for the grade.")
Loop
Dim i As String = lstGrades.Items(i)
If lstGrades.Items(i) < onegrade Then
Return MsgBox(lstGrades.Items(lstStudents.Items(i)))
End If

If lstGrades.Items(i) < onegrade Then
Return (MsgBox(lstStudents.Items(lstGrades.Items(i))))
End If
End Function
Nov 13 '08 #5
jac130
18
there would be two list boxes with names and grades: ex sally 90
mike 85

i click the getbelowgrade button, and am prompted to enter a grade, say 88. it should display Mike's name (he's below 88), but it crashes and highlights the Return line of the function, saying value 88 is not valid for index.
I'm stuck, here is my the portion of my code. Thanks.

Function GetBelowGrade() As String
Dim aGrade As String = InputBox("please enter a positive number for the grade.")
Do While Not IsPositiveNumber(aGrade)
aGrade = InputBox("please enter a positive number for the grade.")
Loop
Dim i As Integer
For i = 0 To lstGrades.Items.Count - 1 Step lstGrades.Items(i) < aGrade
Return MsgBox(lstStudents.Items(lstGrades.Items(i)))
Next
End Function


Private Sub btnBelowGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBelowGrade.Click
GetBelowGrade()

End Sub
End Class
Nov 13 '08 #6
QVeen72
1,445 Expert 1GB
Hi,

You may have One or more Students below the Average Grade, you need to Build a String and then Return List of the names..
Also, you are not returning anything, You can do with a Sub-Procedure
Check This Code:

Expand|Select|Wrap|Line Numbers
  1. Sub GetBelowGrade() As String
  2. Dim aGrade As String = InputBox("please enter a positive number for the grade.")
  3. Do While Not IsPositiveNumber(aGrade)
  4.     aGrade = InputBox("please enter a positive number for the grade.")
  5. Loop
  6. Dim NGrade As Integer 
  7. Dim strList As String =""
  8. NGrade = Val(aGrade)
  9. Dim i As Integer
  10. '
  11. For i = 0 To lstGrades.Items.Count - 1 
  12.     If Val(lstGrades.Items(i)) < NGrade Then    
  13.         strList = strList & lstStudents.Items(i) & ", "
  14.     End If
  15. Next
  16. MsgBox strList
  17. End Sub
  18.  
Regards
Veena
Nov 15 '08 #7
Am so sorry am using this medium to get answers am working on a similar projects but unlike him am displaying my datas in a table and i need to calculate the value for just one of the column units to calculate the total units of all the courses registered by a student also my connection to the database cannot be accessed cos its said to be in a private dim even tho i decleared it in a public sub . am new to programming and i need all possible assistance
Nov 15 '08 #8
jac130
18
thanks. I did try that code and it helped greatly. Although, I did create it as a function, i was required to. i just returned a message box with the string list.
If i wanted to eliminate just the listbox of the grades (not the student names) from the interface, and store the grades into an array, then use the array in the functions,
I'm just lookin for adivce on how to start.for example.. in my current code i have:

dim grade as string=inputbox("please enter grades")
lstgrades.items.add(grades)

I have to delete the list box, how do i get those grades into an array? do i create a class level array: dim grades() as integer ? should i not specifiy a max index? or something like: dim grades() as integer = val(grade) ?
or create an array for each function. ?

any thoughts/adivce would be great
Nov 15 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
2
by: bill yeager | last post by:
I need to get the SelectedValue of each item selected in a multi-selection listbox. I have the following code, but it just returns me the SelectedValue of ONLY the first item selected in the list:...
1
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the...
7
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if...
7
by: Ohad Asor | last post by:
Hello all, I have an ASP.NET page I've written using VS.NET2003, which have a ListBox in it. When I press a button in the form, I try to get the selected item in the list by calling...
9
by: James Radke | last post by:
Hello, I would like to create a sorted listbox which has one property that I can set to true or false to show if the listbox should be sorted (I may potentially add another one for the sort...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
2
by: DartmanX | last post by:
I have a form that does the following: - User inputs a serial number - VBA code does a lookup (SQL Query) and populates a listbox with results (4 columns in the Listbox) - Start Over (Listbox...
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
2
by: ProgrammerChicago | last post by:
I'm not sure if this is the result of the postback behavior or my own code, but for some reason my onclick function is not detecting listbox selections (It's meant to delete files uploaded to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.