I am having difficulty accessing modified data from a dynamic tab control in VB.net. In this case I have a client record with a "one-to-many" relationship to an address table. I have created code that successfully generates and populates the tabs as required. When I attempt to read the individual tabs for update to the database, the first tab read correctly. From that point on I receive an error stating that "Index -1 is out of range."

Any assistance that you can render would be most appreciated....Thank You....

Dim strArrAdrVal(11) As String

'TODO: Complete design and implementation of address UPDATE process

'Determine number of addresses present for this client

intAdrCount = tcCliAddress.TabCount
Dim i As Integer
Dim x As Integer


For i = 0 To intAdrCount

ReDim strArrAdrVal(11)

tcCliAddress.SelectedIndex = i


strArrAdrVal(0) = CLIUIDLabel1.Text

If i = 0 Then
strArrAdrVal(1) = "B"
Else
strArrAdrVal(1) = "S"
End If

strArrAdrVal(2) = "6"


x = tcCliAddress.TabPages(i).Controls.IndexOf(Street1TextBox)
strArrAdrVal(3) = tcCliAddress.TabPages(i).Controls.Item(x).ToString
x = tcCliAddress.TabPages(i).Controls.IndexOf(Street2TextBox)
strArrAdrVal(4) = tcCliAddress.TabPages(i).Controls.Item(x).ToString
x = tcCliAddress.TabPages(i).Controls.IndexOf(CityTextBox)
strArrAdrVal(5) = tcCliAddress.TabPages(i).Controls.Item(x).ToString
x = tcCliAddress.TabPages(i).Controls.IndexOf(StateTextBox)
strArrAdrVal(6) = tcCliAddress.TabPages(i).Controls.Item(x).ToString
x = tcCliAddress.TabPages(i).Controls.IndexOf(ZipMaskedTextBox)
strArrAdrVal(7) = tcCliAddress.TabPages(i).Controls.Item(x).ToString
x = tcCliAddress.TabPages(i).Controls.IndexOf(CountryComboBox)
strArrAdrVal(8) = tcCliAddress.TabPages(i).Controls.Item(x).ToString
x = tcCliAddress.TabPages(i).Controls.IndexOf(PostalTextBox)
strArrAdrVal(9) = tcCliAddress.TabPages(i).Controls.Item(x).ToString
strArrAdrVal(10) = ""



Next i

which line raises the exception?

First of all, there is a mistake in the start of the loop.

For i = 0 To intAdrCount

'should be
For i = 0 To intAdrCount - 1

Also, you are getting this error Index -1 is out of range because one of the controls you are accessing using Controls.IndexOf(...) is not on the tab that is referenced by tcCliAddress.TabPages(i).

If you are still unable to fix the problem, then posting the code that you are using to generate the dynamic tabs would be useful in trying to resolve this.

Good Morning and thank you for responding so quickly...

While stepping through the process I noted that the value of "x" in the following line of code is "-1".

x = tcCliAddress.TabPages(i).Controls.IndexOf(Street1TextBox)

The exception is thrown when the process reaches it's 2nd iteration of the following line of code:

strArrAdrVal(3) = tcCliAddress.TabPages(i).Controls.Item(x).ToString

As is stated previously, the first iteration of the process completes normally. It is only when the process cycles itself to the next tab that this problem occurs.

Thanks again...

Well "Index -1 is out of range." means something wasn't found -- and the only code you searched with is tcCliAddress.TabPages(i).Controls.IndexOf(Street1TextBox) . That text box doesn't exist on the tab page.

Can you post a sample project of how you are creating and reading the textboxes so we can examine the issue further?

Well "Index -1 is out of range." means something wasn't found -- and the only code you searched with is tcCliAddress.TabPages(i).Controls.IndexOf(Street1TextBox) . That text box doesn't exist on the tab page.
Can you post a sample project of how you are creating and reading the textboxes so we can examine the issue further?

Again, thank you for all of your help. Here is the code that I used to create the dynamic tabs.

For i = 1 To intAdrCount - 1


Dim tabpage(i) As TabPage
Dim textbox(i) As TextBox
Dim mtextbox(i) As MaskedTextBox
Dim label(i) As Label



tabpage(i) = New TabPage


tcCliAddress.TabPages.Add(tabpage(i))
tcCliAddress.TabPages(i).Name = "Tab" & i
tcCliAddress.TabPages(i).Font = New Font("Tahoma", 9.75!, FontStyle.Underline)
tcCliAddress.TabPages(i).Text = "Shipping Address " & (i)


For i2 = 1 To 8


Select Case i2


Case 1
Dim Street1TextBox As New TextBox
textbox(i) = New TextBox
label(i) = New Label


tcCliAddress.TabPages(i).Controls.Add(label(i))
label(i).Height = 13
label(i).Width = 47
label(i).Top = label(i).Top + 5
label(i).Left = label(i).Left + 3
label(i).Font = New Font("Tahoma", 8.25!, FontStyle.Regular)
label(i).Text = "Street1:"
tcCliAddress.TabPages(i).Controls.Add(textbox(i))
textbox(i).Name = "Street1TextBox"
textbox(i).Multiline = True
textbox(i).Font = New Font("Tahoma", 14.0!, FontStyle.Regular)
textbox(i).Top = textbox(i).Top + 21
textbox(i).Left = textbox(i).Left + 6
textbox(i).Height = 31
textbox(i).Width = 279
textbox(i).BorderStyle = BorderStyle.None
textbox(i).Text = dsAdr.Tables(0).Rows(i).Item(7).ToString


Case 2


textbox(i) = New TextBox
label(i) = New Label


tcCliAddress.TabPages(i).Controls.Add(label(i))
label(i).Height = 13
label(i).Width = 47
label(i).Top = label(i).Top + 5
label(i).Left = label(i).Left + 288
label(i).Font = New Font("Tahoma", 8.25!, FontStyle.Regular)
label(i).Text = "Street2:"
tcCliAddress.TabPages(i).Controls.Add(textbox(i))
textbox(i).Multiline = True
textbox(i).Font = New Font("Tahoma", 14.0!, FontStyle.Regular)
textbox(i).Top = textbox(i).Top + 21
textbox(i).Left = textbox(i).Left + 291
textbox(i).Height = 31
textbox(i).Width = 279
textbox(i).BorderStyle = BorderStyle.None
textbox(i).Text = dsAdr.Tables(0).Rows(i).Item(8).ToString


Case 3


mtextbox(i) = New MaskedTextBox
label(i) = New Label


tcCliAddress.TabPages(i).Controls.Add(label(i))
label(i).Height = 13
label(i).Width = 53
label(i).Top = label(i).Top + 56
label(i).Left = label(i).Left + 3
label(i).Font = New Font("Tahoma", 8.25!, FontStyle.Regular)
label(i).Text = "Zip Code:"
tcCliAddress.TabPages(i).Controls.Add(mtextbox(i))
mtextbox(i).Mask = "#####"
mtextbox(i).Font = New Font("Tahoma", 15.75!, FontStyle.Regular)
mtextbox(i).Top = mtextbox(i).Top + 72
mtextbox(i).Left = mtextbox(i).Left + 6
mtextbox(i).Height = 31
mtextbox(i).Width = 100
mtextbox(i).BorderStyle = BorderStyle.None
mtextbox(i).TextAlign = HorizontalAlignment.Center
mtextbox(i).Text = dsAdr.Tables(0).Rows(i).Item(11).ToString


Case 4


textbox(i) = New TextBox
label(i) = New Label


tcCliAddress.TabPages(i).Controls.Add(label(i))
label(i).Height = 13
label(i).Width = 30
label(i).Top = label(i).Top + 56
label(i).Left = label(i).Left + 111
label(i).Font = New Font("Tahoma", 8.25!, FontStyle.Regular)
label(i).Text = "City:"
tcCliAddress.TabPages(i).Controls.Add(textbox(i))
textbox(i).Multiline = True
textbox(i).Font = New Font("Tahoma", 14.0!, FontStyle.Regular)
textbox(i).Top = textbox(i).Top + 70
textbox(i).Left = textbox(i).Left + 112
textbox(i).Height = 31
textbox(i).Width = 303
textbox(i).BorderStyle = BorderStyle.None
textbox(i).Text = dsAdr.Tables(0).Rows(i).Item(9).ToString


Case 5


textbox(i) = New TextBox
label(i) = New Label


tcCliAddress.TabPages(i).Controls.Add(label(i))
label(i).Height = 13
label(i).Width = 30
label(i).Top = label(i).Top + 56
label(i).Left = label(i).Left + 111
label(i).Font = New Font("Tahoma", 8.25!, FontStyle.Regular)
label(i).Text = "City:"
tcCliAddress.TabPages(i).Controls.Add(textbox(i))
textbox(i).Multiline = True
textbox(i).Font = New Font("Tahoma", 14.0!, FontStyle.Regular)
textbox(i).Top = textbox(i).Top + 70
textbox(i).Left = textbox(i).Left + 112
textbox(i).Height = 31
textbox(i).Width = 303
textbox(i).BorderStyle = BorderStyle.None
textbox(i).Text = dsAdr.Tables(0).Rows(i).Item(9).ToString


Case 6



textbox(i) = New TextBox
label(i) = New Label


tcCliAddress.TabPages(i).Controls.Add(label(i))
label(i).Height = 13
label(i).Width = 37
label(i).Top = label(i).Top + 56
label(i).Left = label(i).Left + 418
label(i).Font = New Font("Tahoma", 8.25!, FontStyle.Regular)
label(i).Text = "State:"
tcCliAddress.TabPages(i).Controls.Add(textbox(i))
textbox(i).Multiline = True
textbox(i).Font = New Font("Tahoma", 14.0!, FontStyle.Regular)
textbox(i).Top = textbox(i).Top + 70
textbox(i).Left = textbox(i).Left + 421
textbox(i).Height = 31
textbox(i).Width = 37
textbox(i).BorderStyle = BorderStyle.None
textbox(i).Text = dsAdr.Tables(0).Rows(i).Item(10).ToString


Case 7


textbox(i) = New TextBox
label(i) = New Label


tcCliAddress.TabPages(i).Controls.Add(label(i))
label(i).Height = 13
label(i).Width = 50
label(i).Top = label(i).Top + 56
label(i).Left = label(i).Left + 461
label(i).Font = New Font("Tahoma", 8.25!, FontStyle.Regular)
label(i).Text = "Country:"
tcCliAddress.TabPages(i).Controls.Add(textbox(i))
textbox(i).Multiline = True
textbox(i).Font = New Font("Tahoma", 14.0!, FontStyle.Regular)
textbox(i).Top = textbox(i).Top + 70
textbox(i).Left = textbox(i).Left + 464
textbox(i).Height = 31
textbox(i).Width = 370
textbox(i).BorderStyle = BorderStyle.None
textbox(i).Text = dsAdr.Tables(0).Rows(i).Item(13).ToString


Case 8


textbox(i) = New TextBox
label(i) = New Label


tcCliAddress.TabPages(i).Controls.Add(label(i))
label(i).Height = 13
label(i).Width = 107
label(i).Top = label(i).Top + 56
label(i).Left = label(i).Left + 837
label(i).Font = New Font("Tahoma", 8.25!, FontStyle.Regular)
label(i).Text = "Foreign Postal Code:"
tcCliAddress.TabPages(i).Controls.Add(textbox(i))
textbox(i).Multiline = True
textbox(i).Font = New Font("Tahoma", 14.0!, FontStyle.Regular)
textbox(i).Top = textbox(i).Top + 70
textbox(i).Left = textbox(i).Left + 840
textbox(i).Height = 31
textbox(i).Width = 135
textbox(i).BorderStyle = BorderStyle.None
textbox(i).Text = dsAdr.Tables(0).Rows(i).Item(14).ToString


Case Else
Exit Select


End Select
Next i2


Next i


Exit Select


End Select

Thanks again...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.