Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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)

Posted on 2011-03-16
9
Medium Priority
?
433 Views
Last Modified: 2012-05-11
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
Comment
Question by:goldfingerpunk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35153359
try:
 If gradesListBox.Text = "" Or gradesListBox.Text = 0 Then
0
 

Author Comment

by:goldfingerpunk
ID: 35153444
I tried that and now i get this error message : Conversion from string "" to type 'Double' is not valid.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35153452
On the If statement line?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:goldfingerpunk
ID: 35153465
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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35153472
what happens if you change the first If to :
If gradesListBox.Text = 0 Then
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35153485
Actually what you really need is
If gradesListBox.Count = 0 Then
0
 

Author Comment

by:goldfingerpunk
ID: 35153527
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
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 2000 total points
ID: 35153628
sorry that should be :
If gradesListbox.Items.Count = 0 Then
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35153743
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question