diddy
diddy

Reputation: 1

Nested loops and inputting results into listbox

I'm new to VB and I need to make a program that displays a bar chart of @ symbols into a list box for every 100 that is entered into the input box. This is repeated 4 times and are rounded so that 440 would give 4 @ symbols, and 450 would give 5 @ symbols. Here is my code for retrieving the attendance of each game, I just need help forming a loop that will display the results into the list box named lstChart.

    Private Sub btnChart_Click(sender As Object, e As EventArgs) Handles btnChart.Click
    'Retrieve attendance for game 1
    Dim strAttendance1 As Integer
    strAttendance1 = InputBox("Enter Attendance for Game #1.", "Chart Input")
    If strAttendance1 < 0 Then
        MessageBox.Show("Attendance must be greater than zero.", "Error")
    End If

    'Retrieve attendance for game 2
    Dim strAttendance2 As Integer
    strAttendance2 = InputBox("Enter Attendance for Game #2.", "Chart Input")
    If strAttendance2 < 0 Then
        MessageBox.Show("Attendance must be greater than zero.", "Error")
    End If

    'Retrieve attendance for game 3
    Dim strAttendance3 As Integer
    strAttendance3 = InputBox("Enter Attendance for Game #3.", "Chart Input")
    If strAttendance3 < 0 Then
        MessageBox.Show("Attendance must be greater than zero.", "Error")
    End If

    'Retrieve attendance for game 4
    Dim strAttendance4 As Integer
    strAttendance4 = InputBox("Enter Attendance for Game #4.", "Chart Input")
    If strAttendance4 < 0 Then
        MessageBox.Show("Attendance must be greater than zero.", "Error")
    End If

Upvotes: 0

Views: 290

Answers (1)

Mark
Mark

Reputation: 8160

You can use the constructor for String to repeat a character x times. The only tricky part is that .NET's default rounding is banker's rounding, so if you always want .5 to round up, you need to be specific:

' This could be simplified by using an array (or List) for the values.
' Also, interesting use of Hungarian notation, since these are Integers!
For Each attendance In {strAttendance1, strAttendance2, strAttendance3, strAttendance4}
    Dim x = Math.Round(attendance / 100, MidpointRounding.AwayFromZero)
    lstChart.Items.Add(New String("@"c, x))
Next

Upvotes: 1

Related Questions