Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error in if statement 2

Status
Not open for further replies.

lydro

Programmer
Mar 7, 2005
36
CA
I'm trying to remove textbox controls, it gave me error..the error is in the if statement, but I dont' know why. the Panel1.Controls.Count = 42, and when it count to Panel1.Controls.Count(21), it said index 21 is out of range.
any idea?

Dim ctrl As Control
Dim txtBox As TextBox
For i As Integer = 0 To Panel1.Controls.Count - 1
If Panel1.Controls(i).GetType().Equals(GetType(TextBox)) Then
Panel1.Controls.RemoveAt(i)
End If
Next
 
I think - note think - that when you remove a control, the index positions are recalculated.

You could try
Code:
For Each ctrl As Control In Panel1.Controls
  If TypeOf ctrl Is TextBox Then
    Panel1.Controls.Remove(ctrl)
   End If
Next ctrl

(Not tested)

Alternatively, if you want to use RemoveAt, you might try counting backwards, since this wont affect the index positions.

Hope this helps.
 
As you remove controls, decrement the value of i to account for the changing index position (yes earthandfire, you are right), and throw a test of the value of i to exit the loop if it is greater than the panel's Controls.Count value:

Dim ctrl As Control
Dim txtBox As TextBox
For i As Integer = 0 To Panel1.Controls.Count - 1
If i > Panel1.Controls.Count - 1 Then Exit For
If Panel1.Controls(i).GetType().Equals(GetType(TextBox)) Then
Panel1.Controls.RemoveAt(i)
i = i - 1
End If
Next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
earthandfire, your code is for vb 6, it doesn't work in vb.net.

jonbatts, here is the msg:
Specified argument was out of the range of valid values.
Parameter name: Index 21 is out of range.
 
Jebenson, you are so awesome, you saved me..thank you very much...^_^....
 
Thanks lydro, but actually I'm a dummy. Now that I think about it some more, there's no need for the extra code I added. All you really need to do is loop through the controls backwards:

For i As Integer = Panel1.Controls.Count - 1 To 0 Step -1
If Panel1.Controls(i).GetType().Equals(GetType(TextBox)) Then
Panel1.Controls.RemoveAt(i)
End If
Next

As the controls are removed the Controls.Count value decreases, but so does the value of i. The code is much cleaner this way.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
lydro, I beg to differ. My code is for .NET. As I said it wasn't tested. In fact it removes all but one text box from the tests that I have just done. As I suggested and jebenson demonstrated - counting backwards with RemoveAt.
 
thanks all your help..you guys just make my day more happy...^_^_^_^^_^.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top