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

Page 1 of 1

## 8 Replies - 23584 Views - Last Post: 20 April 2015 - 05:04 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=262750&amp;s=07ebcb1a5f192e96516a86a9557695d6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 unkn0wnone

Reputation: 0
• 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)

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

• Critical Section

Reputation: 1824
• Posts: 4,627
• 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.

### #3 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• 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.

### #4 unkn0wnone

Reputation: 0
• 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 ")
counter = counter + 1
Do Until numbers = -1
Loop

End Sub
End Class
```

### #5 smohd

• Critical Section

Reputation: 1824
• Posts: 4,627
• 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
counter = counter + 1
End If
Loop Until number = -1

' Calculate average and display it
```

### #6 unkn0wnone

Reputation: 0
• 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
```

### #7 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• 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.

### #8 smohd

• Critical Section

Reputation: 1824
• Posts: 4,627
• 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
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

```

### #9 Kirto

Reputation: 0
• Posts: 1
• Joined: 20-April 15

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

Posted 20 April 2015 - 05:04 AM

Herobrine said: