• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

how do i make a message box for if there is no numbers to divide (keep getting invalidargument=Value of '1' is not valid for 'index'. Parameter name: index error message)

hi
I need to know how to make a message box for if the grades in the listbox equal 0 (meaning no grades have been entered and how to get the message box from poppin up all the time. It either pops up and wont calculate the average of the grades or i get the invalidargument=Value of '1' is not valid for 'index'. Parameter name: index error message. here is what i have:
' initialization phase
      Dim total As Integer = 0
        Dim gradeCounter As Integer = 0
        Dim grade As Integer = 0
        Dim average As Double = 0



        If IsNumeric(gradesListBox.Text = "" Or 0) = True Then
            MsgBox("sorry there is no grade to add")


        Else



            Do
                grade = gradesListBox.Items.Item(gradeCounter)
                total += grade
                gradeCounter += 1
            Loop Until gradeCounter = gradesListBox.Items.Count
            If grade = 0 Then
                average = False
            End If

            average = total / gradeCounter
            averageResultLabel.Text = String.Format("{0:F}", average)
            addButton.Enabled = True
            gradeTextBox.Focus()
        End If
    End Sub ' averageButton_Click
End Class ' ClassAverageForm
0
goldfingerpunk
Asked:
goldfingerpunk
  • 5
  • 3
1 Solution
 
Paul JacksonSoftware EngineerCommented:
try:
 If gradesListBox.Text = "" Or gradesListBox.Text = 0 Then
0
 
goldfingerpunkAuthor Commented:
I tried that and now i get this error message : Conversion from string "" to type 'Double' is not valid.
0
 
Paul JacksonSoftware EngineerCommented:
On the If statement line?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
goldfingerpunkAuthor Commented:
well i guess there is two IF statment lines but i tried what you said on the first one. here is what part of my assignment says- Summing the grades in the ListBox. Modify code in the averageButton_ Click event handler so that gradeCounter increments until its equal to the number of grades entered. Use gradesListBox. Items. Count to determine the number of items in the ListBox. The number returned by the Count property will be zero if there are no grades entered. Use an If Then selection statement to avoid division by zero and display a message dialog to the user if there are no grades entered when the user clicks the Average Button.
0
 
Paul JacksonSoftware EngineerCommented:
what happens if you change the first If to :
If gradesListBox.Text = 0 Then
0
 
Paul JacksonSoftware EngineerCommented:
Actually what you really need is
If gradesListBox.Count = 0 Then
0
 
goldfingerpunkAuthor Commented:
same as first time with your first response and the second time i get count is not a member of systems.windows.forms.listbox
0
 
Paul JacksonSoftware EngineerCommented:
sorry that should be :
If gradesListbox.Items.Count = 0 Then
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you use a For...Next loop?
Private Sub averageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles averageButton.Click
        If gradesListBox.Items.Count = 0 Then
            MessageBox.Show("No grades to average!")
        Else
            Dim total As Integer = 0
            Dim average As Double = 0

            For gradeCounter As Integer = 0 To gradesListBox.Items.Count - 1
                total += gradesListBox.Items.Item(gradeCounter)
            Next

            average = total / gradesListBox.Items.Count
            averageResultLabel.Text = String.Format("{0:F}", average)

            addButton.Enabled = True
            gradeTextBox.Focus()
        End If
    End Sub

Open in new window


It still technically meets the requirement of making "gradeCounter increment until its equal to the number of grades entered".
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now