|
-
Jun 28th, 2003, 01:21 PM
#1
Thread Starter
PowerPoster
I'm having problems with a For Each...Next loop
Hi all,
I'm trying to clear a load of textboxes in one go like this:
VB Code:
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
' Clear textboxes and graphics
Dim Txt As System.Windows.Forms.TextBox ' assign textboxes to object Txt
For Each Txt In gpbValues.Controls
Txt.Text = ""
Next Txt
End Sub
I get a runtime error of InvalidCastException every time. Can anyone tell me what I'm doing wrong please?
Last edited by SuperSparks; Jun 28th, 2003 at 01:26 PM.
Nick.
-
Jun 28th, 2003, 02:17 PM
#2
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objTextBox As Object
For Each objTextBox In Me.ActiveForm.Controls
If TypeOf objTextBox Is TextBox Then
objTextBox.text = ""
End If
Next
End Sub
hope that helps.
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jun 28th, 2003, 02:55 PM
#3
Thread Starter
PowerPoster
That cleared all the textboxes nicely, but I had to turn Option Strict off because when I tried to enter objTextBox.Text = "" it complained about late binding. Still it's a lot better than my effort, thanks a lot.
-
Jun 28th, 2003, 03:27 PM
#4
here's a better way and you can leave Option Strict set to "ON"
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Control
For Each x In Me.Controls
If TypeOf x Is TextBox Then
x.Text = ""
End If
Next
End Sub
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jun 28th, 2003, 04:54 PM
#5
Thread Starter
PowerPoster
Perfect 
Thanks for all your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|