Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a textbox at runtime, using 2-dimensional array. In that there are 10 columns and no of rows depends on user input.

The problem is: I created all textbox at load time. Now in the second column user will input integer value that i should read and compute(add) with 3rd column then result will update inside 4th column. When i try to access the value its giving an error as "Index is out of bound" in the below code.

VB
' CREATED TEXTBOX IN RUN TIME
 For i = 1 To 10
            For j = 1 To v
                txt(i, j) = New TextBox
                txt(i, j).Size = New Size(60, 60)
                txt(i, j).Location = New Point(i * 80 + 5, j * 30 + 100)
                Controls.Add(txt(i, j))
            Next j
        Next i

'.............
' here eval is a method that wil be called wen button click..

Public Sub eval()
        Dim txtb As New TextBox()
        For m = 4 To 4
            For n = 1 To v
                P = td(n) Mod 10
                If (td(n) <= 10) Then
                    td1(n) = 1
                    txt(m, n) = New TextBox
                    ' MessageBox.Show("var ==>" + td(n).ToString())
                    txt(m, n).Text = td10(n) + td1(n)
                    MessageBox.Show("txt ==>" + txt(m, n).Text.ToString())
                End If

                td1(n) = n1 \ 10
                If P <> 0 Then
                    td1(n) = td1(n) + 1
                End If
                'txt(m, n).Text = td10(n) + td1(n)
            Next
        Next
    End Sub


here eval is a method that will be called when button clicks.
Can anybody help me out how exactly i should solve this?
Posted
Updated 16-Jun-10 7:47am
v2

can the runtime creating textbox be in function? then you called the function and get the value from your textbox
 
Share this answer
 
For anyone to be able to answer your question they would have to be a genius, or psychic.

Taking just this short section of your code:
VB
P = td(n) Mod 10
 If (td(n) <= 10) Then
     td1(n) = 1
     txt(m, n) = New TextBox
     ' MessageBox.Show("var ==>" + td(n).ToString())
     txt(m, n).Text = td10(n) + td1(n)


there are 4 (at least) variables that are unknowable

what is P
what is td()
what is td1()
what is td10()

please edit your question to include the declarations of these.
 
Share this answer
 
It looks like you are not referencing the original textbox collection in your eval function.

If I understand your question correctly I think you want to do something like this...

    ' CREATE A CLASS/MODULE LEVEL VARIABLE TO HOLD A REFERENCE TO YOUR TEXTBOXES FOR LATER USE
    Private _textboxes(10, 1) As TextBox

' ADD YOUR CONTROLS TO YOUR FORM AND THE _textboxes ARRAY
    Public Sub CreateControlArray()
        Dim rowCount As Int32 = 10

        ' ASSIGN SIZE BASED ON USER INPUT FOR ROW COUNT
        ReDim _textboxes(10, rowCount)

        For col = 1 To 10 ' each column
            For row = 1 To rowCount ' each row
                Dim txt As TextBox = New TextBox

                txt.Size = New Size(60, 60)
                txt.Location = New Point(col * 80 + 5, row * 30 + 100)

                ' test code : add test input value to second and third columns
                If col = 2 Then
                    txt.Text = row * 5
                ElseIf col = 3 Then
                    txt.Text = row * 30
                End If
                ' end test code

                _textboxes(col, row) = txt ' ADD TEXTBOX TO CONTROL ARRAY
                Controls.Add(txt) ' add to form
            Next row
        Next col

    End Sub

    Public Sub Eval()
        Dim rowCount As Int32 = 10

        For col As Int32 = 4 To 4
            For row As Int32 = 1 To rowCount

                ' REFERENCE THE TEXTBOXES USING THE CLASS/MODULE LEVEL VARIABLE YOU STORED THEM IN DURING THE CreateControlArray() ROUTINE
                Dim txtUserInput As TextBox = _textboxes(col - 2, row)
                Dim txtOtherValue As TextBox = _textboxes(col - 1, row)
                Dim txtProduct As TextBox = _textboxes(col, row)

                ' UPDATE FOURTH COLUMN TEXTBOXES
                If IsNumeric(txtUserInput.Text) AndAlso IsNumeric(txtOtherValue.Text) Then
                    txtProduct.Text = CType(txtUserInput.Text, Single) * CType(txtOtherValue.Text, Single)
                End If

            Next
        Next


    End Sub
 
Share this answer
 

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