Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    query = "SELECT * from month"
    FillComboBox(Me.cmbMonth, query, "month", "monthDesc", "monthNo")
    Me.WindowState = FormWindowState.Maximized
    For Each tb As TextBox In Me.Controls.OfType(Of TextBox)()
        If tb.Name.Contains("txtp") Then
            AddHandler tb.TextChanged, AddressOf txtp_TextChanged
            p.Add(tb.Text)
        End If
    Next
End Sub
Private Sub txtp_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtp1.TextChanged
    Dim Currenttxtp As TextBox = DirectCast(sender, TextBox)
    Dim Index As Integer = CType(Val(Currenttxtp.Name.Split("p"c)(1)), Integer)
    Dim tempptotal As Double = 0.0
    If Not Double.TryParse(Currenttxtp.Text, tempptotal) Then
        Currenttxtp.Text = "0"
        Currenttxtp.SelectAll()
    Else
        p(Index - 1) = Currenttxtp.Text
        Dim txtt As TextBox = DirectCast(Me.Controls("txtt" + Index.ToString), TextBox)
        txtt.Text = Currenttxtp.Text
        ptotal += tempptotal
        grandtotal = ptotal
        Me.txtItemTotal.Text = ptotal.ToString
        Me.txtGrandTotal.Text = grandtotal.ToString
    End If
End Sub



it says:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


help please.. :(
Posted
Comments
Surendra Adhikari SA 6-Jun-13 2:59am    
in which line of code do you get error??
Ms.Shen 6-Jun-13 5:53am    
here sir:

p(Index - 1) = Currenttxtp.Text

1 solution

Assume you change the text, and teh value is zero (either because it failed the double.TryParse test last time, of becasu ethe user entered zero.
What value is in Index?
Answer: zero.

So what element are you accessing with this line:
VB
p(Index - 1) = Currenttxtp.Text
 
Share this answer
 
Comments
Ms.Shen 6-Jun-13 5:53am    
I'm trying to access textboxes.. :(
Ms.Shen 6-Jun-13 5:55am    
textboxes with names containing "txtp" plus the incrementing 1-50.

because I have textboxes named txtp1 - txtp50
OriginalGriff 6-Jun-13 6:03am    
Yes, but is there a textbox at index 0-1?
Hint: no.
Ms.Shen 6-Jun-13 7:42am    
thank you. :)

let's say I'm a noob.

so, can you help me again with this?

how can I store this in a variable?

ptotal = Double.Parse(Me.txtt1.Text) + Double.Parse(Me.txtt2.Text) + Double.Parse(Me.txtt3.Text) + _
Double.Parse(Me.txtt4.Text) + Double.Parse(Me.txtt5.Text) + Double.Parse(Me.txtt6.Text) + _
Double.Parse(Me.txtt7.Text) + Double.Parse(Me.txtt8.Text) + Double.Parse(Me.txtt9.Text) + _
Double.Parse(Me.txtt10.Text) + Double.Parse(Me.txtt11.Text) + Double.Parse(Me.txtt12.Text) + _
Double.Parse(Me.txtt13.Text) + Double.Parse(Me.txtt14.Text) + Double.Parse(Me.txtt15.Text) + _
Double.Parse(Me.txtt16.Text) + Double.Parse(Me.txtt17.Text) + Double.Parse(Me.txtt18.Text) + _
Double.Parse(Me.txtt19.Text) + Double.Parse(Me.txtt20.Text) + Double.Parse(Me.txtt21.Text) + _
Double.Parse(Me.txtt22.Text) + Double.Parse(Me.txtt23.Text) + Double.Parse(Me.txtt24.Text) + _
Double.Parse(Me.txtt25.Text) + Double.Parse(Me.txtt26.Text) + Double.Parse(Me.txtt27.Text) + _
Double.Parse(Me.txtt28.Text) + Double.Parse(Me.txtt29.Text) + Double.Parse(Me.txtt30.Text) + _
Double.Parse(Me.txtt31.Text) + Double.Parse(Me.txtt32.Text) + Double.Parse(Me.txtt33.Text) + _
Double.Parse(Me.txtt34.Text) + Double.Parse(Me.txtt35.Text) + Double.Parse(Me.txtt36.Text) + _
Double.Parse(Me.txtt37.Text) + Double.Parse(Me.txtt38.Text) + Double.Parse(Me.txtt39.Text) + _
Double.Parse(Me.txtt40.Text) + Double.Parse(Me.txtt41.Text) + Double.Parse(Me.txtt42.Text) + _
Double.Parse(Me.txtt43.Text) + Double.Parse(Me.txtt44.Text) + Double.Parse(Me.txtt45.Text) + _
Double.Parse(Me.txtt46.Text) + Double.Parse(Me.txtt47.Text) + Double.Parse(Me.txtt48.Text) + _
Double.Parse(Me.txtt49.Text) + Double.Parse(Me.txtt50.Text)
OriginalGriff 6-Jun-13 8:08am    
"how can I store this in a variable?"
By preference, a long, long way away from my code...:sigh:

This may seem of topic, but...why on earth do you have fifty textboxes called "txttn"? Are you going to remember which one s which next week? Next month? Is the user going to have any idea what he should do with them?
Is 50 a "magic number" here? What happens if that becomes 75, or 100?
I would strongly suggest that you would be better off looking at using a Table based input - a DataGridView or similar would be an idea - and throw those text boxes away!

What happens if the user enters "Hello" in txtt17? Your program crashes. Or txtt46? Again, you crash. and if I had just entered 47 different numbers I'd probably be rather pissed off with you if they just disappeared because I pressed the wrong key...

As to your question: you did, in your code. "ptotal" is a variable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900