7 Replies - 6676 Views - Last Post: 12 January 2012 - 11:14 PM Rate Topic: -----

#1 unkn0wnone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-January 12

How to use inputBox to add items to listbox and find average of all

Posted 12 January 2012 - 04:49 PM

^ I am trying to make an application that would use an inputbox that will enter numbers until the user enters -1 after the numbers were entered and the user entered -1 the numbers would display within the listbox and the average would be in a label displaying "The average number is" & averagenumber in my current application i have it from 1-5 my question is how would i allow it to enter an unknown amount of numbers and create an counter for it,

Public Class Form1
    Private counter As Integer
    Private item As Double
    Private average As Double
    Private numbers As Double


    Private Sub xEnterNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xEnterNumbers.Click
        For i As Integer = 1 To 5
            numbers = InputBox(" Enter Number " & i)
            item = xNumbers.Items.Add(numbers)

            average = numbers / 5

            Me.xAverageDisplayLabel.Text = "The Average Number is " & average



Is This A Good Question/Topic? 0
  • +

Replies To: How to use inputBox to add items to listbox and find average of all

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How to use inputBox to add items to listbox and find average of all

Posted 12 January 2012 - 04:53 PM

You can use list(Of Integer) to store numbers entered, you can use a while loop to loop until the input is -1.
Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 563
  • View blog
  • Posts: 2,690
  • Joined: 24-July 11

Re: How to use inputBox to add items to listbox and find average of all

Posted 12 January 2012 - 04:53 PM

Dim counter As Integer
Dim overall As Integer
Dim current As Integer = InputBox("Enter number")
If current = -1 Then
Label1.Text = (overall/counter).ToString()
Else
overall += current
Current = 0
Counter += 1
End If


That's how I envisioned it.
Was This Post Helpful? 0
  • +
  • -

#4 unkn0wnone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-January 12

Re: How to use inputBox to add items to listbox and find average of all

Posted 12 January 2012 - 05:01 PM

Did this but it crashes

Public Class Form1
    Private counter As Integer
    Private item As Double
    Private average As Double
    Private numbers As Double


    Private Sub xEnterNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xEnterNumbers.Click
        numbers = InputBox(" Enter Number ")
        item = xNumbers.Items.Add(numbers)
        counter = counter + 1
        Do Until numbers = -1
        Loop

    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How to use inputBox to add items to listbox and find average of all

Posted 12 January 2012 - 05:10 PM

That loops will loop forever because nothing in it to chane the value, or wont loop anymore.
You can use a do while and have something like:
Do
  numbers = InputBox(" Enter Number ")
  If number <> -1 Then
    xNumbers.Items.Add(numbers)
    counter = counter + 1
  End If
Loop Until number = -1

' Calculate average and display it

Was This Post Helpful? 1
  • +
  • -

#6 unkn0wnone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-January 12

Re: How to use inputBox to add items to listbox and find average of all

Posted 12 January 2012 - 05:29 PM

I Did it would show "The Average Number" but not the number is it because i declared my numbers as double?

        average = numbers / counter
        Me.xAverageDisplayLabel.Text = "The Average Number Is " & average.ToString

Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 563
  • View blog
  • Posts: 2,690
  • Joined: 24-July 11

Re: How to use inputBox to add items to listbox and find average of all

Posted 12 January 2012 - 05:56 PM

You now need to loop thought the contents of a ListBox, add them to an Integer value and then calculate the average.
Dim alltotal As Integer
For i As Integer = 0 To ListBox1.Items.Count - 1
alltotal += ListBox1.Items(i)
Next
average = alltotal / counter


Cheers.
Was This Post Helpful? 1
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How to use inputBox to add items to listbox and find average of all

Posted 12 January 2012 - 11:14 PM

Or simply you can use the same one loop to add numbers like:
Dim total As Integer = 0 
Do
  numbers = InputBox(" Enter Number ")
  If number <> -1 Then
    xNumbers.Items.Add(numbers)
    counter = counter + 1
    total += number ' add number entered to the total
  End If
Loop Until number = -1

'then here just use total / counter to find your average
' Calculate average and display it


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1