Help Trying to add numbers in a listbox then get the average of those

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 14905 Views - Last Post: 27 October 2008 - 03:33 PM Rate Topic: -----

#1 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 06:53 AM

Hello all, I'm new to VB and I'm working on a project. I'm trying to get the sum of 6 numbers that I am putting into a listbox and then get the average. I am having a couple of problems.
1. when I select the number to input from another lisbox it puts the number in 6 times. I am trying to select 6 different numbers.

actually I think that's the only thing I'm having a problem w/. any help would be greatly appreciated. I am working in visual studio 2008.

below is the code of what I have.

ListBoxJudges.Items.Remove(ListBoxJudges.SelectedItem)
		'variables
		Dim inputscore As Integer
		Dim scoreaccumulator As Integer
		Dim scorecounter As Integer, scoreaverage As Decimal

		lblAverageScoreFinal.Visible = False

		scoreaccumulator = 0
		scorecounter = 1

		Do While scorecounter <= 6
			inputscore = Convert.ToInt32(ListBoxScores.SelectedItem)
			Integer.TryParse(ListBoxScores.SelectedItem.ToString, inputscore)
			ListBoxJudgeScore.Items.Add(inputscore)
			scoreaccumulator = scoreaccumulator + inputscore
			scorecounter = scorecounter + 1
		Loop
		
		scoreaverage = scoreaccumulator \ 6



		lblAverageScoreFinal.Text = scoreaverage.ToString
		lblAverageScoreFinal.Visible = True


Is This A Good Question/Topic? -1
  • +

Replies To: Help Trying to add numbers in a listbox then get the average of those

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 07:03 AM

Moved to VB.NET.
Was This Post Helpful? 0
  • +
  • -

#3 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 08:46 AM

i wouldnt use a do while for this scenario. try something a little more like this using a for loop

Dim i as int32
For i = 0 To ListBox.items.Count - 1
  scoreaccumulator = scoreaccumulator + ListBox.Items(i)
Next i



If you do something like that you can loop through each row, and use i as your row index to reference each row and accumulate the scores that way, instead of using you own variable to count the number of rows. You might have to fool with the Listbox.items(i) because im not 100% sure that is the exact syntax to get the row value based on the index, but i think you will get the idea.
Was This Post Helpful? 0
  • +
  • -

#4 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 08:51 AM

Ya you basically averaging and adding the one selected item 6 times. Are you trying to move the selected item from one list box to another then average them? Or are you trying to select multiple items from the list box and move them all over and average them?
Was This Post Helpful? 0
  • +
  • -

#5 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 09:11 AM

View Postmagicmonkey, on 27 Oct, 2008 - 08:51 AM, said:

Ya you basically averaging and adding the one selected item 6 times. Are you trying to move the selected item from one list box to another then average them? Or are you trying to select multiple items from the list box and move them all over and average them?


I've got 1 listbox that has numbers from 1-10. and when I hit the enter button it puts the selected number into the 2nd listbox. I'm selecting 6 different numbers from the 1st listbox, then adding them and averaging the total. I wanted to have the numbers go into a label and do the adding and dividing from there but I didn't know how. I'm so new to this and am just a little confused I think but am trying. this took me all weekend to try to do till i figured to look for help.

This post has been edited by markjnj: 27 October 2008 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 609
  • Joined: 01-October 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 09:19 AM

@bfl - i don't think your code will even compile though i do agree with the direction of your post. something like this

		ListBox2.Items.Clear()
		ListBox2.Items.Add(1)
		ListBox2.Items.Add(2)
		ListBox2.Items.Add(3)
		ListBox2.Items.Add(42)
		ListBox2.Items.Add("A")

		Dim total As Integer = 0, lbNUM As Integer = 0
		For idx = 0 To ListBox2.Items.Count - 1
			'make sure it is a number
			If Integer.TryParse(ListBox2.Items.Item(idx).ToString, lbNUM) Then
				total += lbNUM
			Else
				Stop 'not a number
			End If
		Next



View Postbflosabre91, on 27 Oct, 2008 - 10:46 AM, said:

i wouldnt use a do while for this scenario. try something a little more like this using a for loop

Dim i as int32
For i = 0 To ListBox.items.Count - 1
  scoreaccumulator = scoreaccumulator + ListBox.Items(i)
Next i



If you do something like that you can loop through each row, and use i as your row index to reference each row and accumulate the scores that way, instead of using you own variable to count the number of rows. You might have to fool with the Listbox.items(i) because im not 100% sure that is the exact syntax to get the row value based on the index, but i think you will get the idea.

This post has been edited by dbasnett: 27 October 2008 - 09:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 09:31 AM

View Postdbasnett, on 27 Oct, 2008 - 09:19 AM, said:

@bfl - i don't think your code will even compile though i do agree with the direction of your post. something like this

		ListBox2.Items.Clear()
		ListBox2.Items.Add(1)
		ListBox2.Items.Add(2)
		ListBox2.Items.Add(3)
		ListBox2.Items.Add(42)
		ListBox2.Items.Add("A")

		Dim total As Integer = 0, lbNUM As Integer = 0
		For idx = 0 To ListBox2.Items.Count - 1
			'make sure it is a number
			If Integer.TryParse(ListBox2.Items.Item(idx).ToString, lbNUM) Then
				total += lbNUM
			Else
				Stop 'not a number
			End If
		Next





View Postbflosabre91, on 27 Oct, 2008 - 10:46 AM, said:

i wouldnt use a do while for this scenario. try something a little more like this using a for loop

Dim i as int32
For i = 0 To ListBox.items.Count - 1
  scoreaccumulator = scoreaccumulator + ListBox.Items(i)
Next i



If you do something like that you can loop through each row, and use i as your row index to reference each row and accumulate the scores that way, instead of using you own variable to count the number of rows. You might have to fool with the Listbox.items(i) because im not 100% sure that is the exact syntax to get the row value based on the index, but i think you will get the idea.



indeed, i was not expecting my code to compile, just wanted to give the OP a better direction of how he should go about accomplishing his goal. you seem to have done all the work for him :)
Was This Post Helpful? 0
  • +
  • -

#8 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 09:34 AM

Nah this is doing all the work for him!

        'ListBoxJudges.Items.Remove(ListBoxJudges.SelectedItem) 'Don't think you want this
        'variables
        Dim inputscore As Integer
        Dim scoreaccumulator As Integer
        Dim scorecounter As Integer, scoreaverage As Decimal

        lblAverageScoreFinal.Visible = False

        scoreaccumulator = 0
        scorecounter = 0 'Should be 0 i think
        listboxscores.Items.Clear() 'Clear listboxscores
        For Each selItem As Object In listboxjudges.SelectedItems
            If Integer.TryParse(selItem.ToString, inputscore) Then
                scoreaccumulator = scoreaccumulator + inputscore
                scorecounter = scorecounter + 1
                listboxscores.Items.Add(selItem) 'Add selected item to listbox scores
            End If
        Next

        scoreaverage = scoreaccumulator \ 6
        lblAverageScoreFinal.Text = scoreaverage.ToString
        lblAverageScoreFinal.Visible = True


Was This Post Helpful? 0
  • +
  • -

#9 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 09:58 AM

Hey Thanks so much. I think I'm getting closer to what I'm trying to get done. an I reallly appreciate the help. I'm still having a problem, so now I'm able to get each number I select into the listboxjudgescore, but it doesn't seem to be adding the numbers. I must be missing something. I guess check out the changes below. what am i missing?

View Postmagicmonkey, on 27 Oct, 2008 - 09:34 AM, said:

Nah this is doing all the work for him!

        ListBoxJudges.Items.Remove(ListBoxJudges.SelectedItem) 'i want to keep this b/c when I select a number in listboxscore I want it to delete the selected name in that listbox.
        'variables
        Dim inputscore As Integer
        Dim scoreaccumulator As Integer
        Dim scorecounter As Integer, scoreaverage As Decimal

        lblAverageScoreFinal.Visible = False

        scoreaccumulator = 0
        scorecounter = 0 'i tried putting a 1 in here also but i didn't notice any difference
        listboxscores.Items.Clear() 'Clear listboxscores
        For Each selItem As Object In listboxjudges.SelectedItems
            If Integer.TryParse(selItem.ToString, inputscore) Then
                scoreaccumulator = scoreaccumulator + inputscore
                scorecounter = scorecounter + 1
                listboxscores.Items.Add(selItem) 'Add selected item to listbox scores
            End If
        Next

        scoreaverage = scoreaccumulator \ 6
        lblAverageScoreFinal.Text = scoreaverage.ToString
        lblAverageScoreFinal.Visible = True


This post has been edited by markjnj: 27 October 2008 - 10:15 AM

Was This Post Helpful? 0
  • +
  • -

#10 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 11:39 AM

Doh... left your hardcoded 6 in average

'scoreaverage = scoreaccumulator \ 6
scoreaverage = scoreaccumulator \ scorecounter



Hopefully that gets it working...
Was This Post Helpful? 0
  • +
  • -

#11 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 11:53 AM

still not working. it's still not tallying the numbers up. So say if I select 10 6x's it should come out to 60, then I should be able to divide that and get the average. right now it doesn't appear to be doing that. It's not adding up the numbers and I don't know what it's deviding it by? sorry to be such a pain...

View Postmagicmonkey, on 27 Oct, 2008 - 11:39 AM, said:

Doh... left your hardcoded 6 in average

'scoreaverage = scoreaccumulator \ 6
scoreaverage = scoreaccumulator \ scorecounter



Hopefully that gets it working...

Was This Post Helpful? 0
  • +
  • -

#12 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 12:02 PM

I must of miss read your code, I thought you had like 10 numbers in listboxjudges, you would select say 6 numbers from there then press the enter button and they would move over to listboxscores. You better post more of your code, I need to see how you populate the listboxes with the scores and the enter button click event code.
Was This Post Helpful? 0
  • +
  • -

#13 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 12:11 PM

Sorry about that, but yes, Its a prog. where there are 6 judges, and they choose a score from 1-10 in the list box, after they hit enter their name disappears, and the number they selected goes into the 2nd list box. and the same for the remaining 5. so there should be 6 numbers in the 2nd list box now and that's what I've been trying to get tallyed up and then divided by 6 to get the average score.

Here's the whole code w/the changes your changes:

Public Class MainForm

	Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		'add names to ListBoxJudges
		ListBoxJudges.Items.Add("Henderson, Harry")
		ListBoxJudges.Items.Add("Jones, John")
		ListBoxJudges.Items.Add("Smith, Mike")
		ListBoxJudges.Items.Add("Young, Janet")
		ListBoxJudges.Items.Add("Imus, Don")
		ListBoxJudges.Items.Add("Wright, Ken")

		'add items to ListBoxScores
		Dim Score As Integer
		For Score = 1 To 10
			ListBoxScores.Items.Add(Score.ToString)
		Next Score

		'select default listBox items
		ListBoxJudges.SelectedIndex = 0
		ListBoxScores.SelectedItem = "1"
	End Sub


	Private Sub btnEnter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnter.Click
		ListBoxJudges.Items.Remove(ListBoxJudges.SelectedItem)

		Dim inputscore As Integer
		Dim scoreaccumulator As Integer
		Dim scorecounter As Integer, scoreaverage As Decimal

		lblAverageScoreFinal.Visible = False

		scoreaccumulator = 0
		scorecounter = 0 'Should be 0 i think  
		'Clear listboxscores  
		For Each selItem As Object In ListBoxScores.SelectedItems
			If Integer.TryParse(selItem.ToString, inputscore) Then
				scoreaccumulator = scoreaccumulator + inputscore
				scorecounter = scorecounter + 1
				ListBoxJudgeScore.Items.Add(selItem) 'Add selected item to listbox scores  
			End If
		Next

		scoreaverage = scoreaccumulator \ scorecounter
		lblAverageScoreFinal.Text = scoreaverage.ToString
		lblAverageScoreFinal.Visible = True
	End Sub
End Class


View Postmagicmonkey, on 27 Oct, 2008 - 12:02 PM, said:

I must of miss read your code, I thought you had like 10 numbers in listboxjudges, you would select say 6 numbers from there then press the enter button and they would move over to listboxscores. You better post more of your code, I need to see how you populate the listboxes with the scores and the enter button click event code.

Was This Post Helpful? 0
  • +
  • -

#14 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 01:10 PM

Oh ya my code is all wrong then. Let me try to figure it out for you again.
Was This Post Helpful? 0
  • +
  • -

#15 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Help Trying to add numbers in a listbox then get the average of those

Posted 27 October 2008 - 01:21 PM

This will work...

 Private Sub btnEnter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        Dim inputscore As Integer
        Dim scoreaccumulator As Integer
        Dim scorecounter As Integer, scoreaverage As Decimal

        lblAverageScoreFinal.Visible = False

        'Remove Judge from list 
        ListBoxJudges.Items.Remove(ListBoxJudges.SelectedItem)

        'Add Selected Score to ListBoxJudgeScore
        ListBoxJudgeScore.Items.Add(ListBoxScores.SelectedItem)

        'Update Average
        scoreaccumulator = 0
        scorecounter = 0 'Should be 0 i think  
        For Each selItem As Object In ListBoxJudgeScore.Items
            If Integer.TryParse(selItem.ToString, inputscore) Then
                scoreaccumulator = scoreaccumulator + inputscore
                scorecounter = scorecounter + 1
            End If
        Next
        scoreaverage = scoreaccumulator \ scorecounter
        lblAverageScoreFinal.Text = scoreaverage.ToString
        lblAverageScoreFinal.Visible = True
    End Sub


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2