Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

vb.net - Index outside bounds in event handler

I'm having some trouble displaying the shipping price to the lblshipping.text

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain
    Private intMin() As Integer = {1, 11, 51, 101}
    Private intMax() As Integer = {10, 50, 100}
    Private dblShip() As Double = {15, 10, 5, 0}

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtordered_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtOrdered.KeyPress
        ' allows the text box to accept numbers and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtordered_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOrdered.TextChanged
        lblShipping.Text = String.Empty
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

        Dim intOrdered As Integer
        Integer.TryParse(txtOrdered.Text, intOrdered)

        For intIndex As Integer = 0 To 3
            If intOrdered <= 2 Then
                If intOrdered >= intMin(intIndex) And intOrdered <= intMax(intIndex) Then
                    lblShipping.Text = dblShip(intIndex).ToString("C2")
                End If
            End If
            If intIndex = 3 Then
                If intOrdered >= intMin(intIndex) Then
                    lblShipping.Text = dblShip(intIndex).ToString("C2")
                End If
            End If
        Next intIndex
    End Sub
End Class
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to delete this statements

Private Sub txtordered_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOrdered.TextChanged
    lblShipping.Text = String.Empty
End Sub

and try to change your for next statements

For intIndex As Integer = 0 To 3
        If intOrdered <= 2 Then
            If intOrdered >= intMin(intIndex) And intOrdered <= intMax(intIndex) Then
                lblShipping.Text = dblShip(intIndex).ToString("C2")
            End If
       elseIf intIndex = 3 Then
            If intOrdered >= intMin(intIndex) Then
                lblShipping.Text = dblShip(intIndex).ToString("C2")
            End If
        End If
    Next intIndex

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...