Results 1 to 5 of 5

Thread: I'm having problems with a For Each...Next loop

  1. #1

    Thread Starter
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265

    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:
    1. Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
    2.  
    3.         ' Clear textboxes and graphics
    4.         Dim Txt As System.Windows.Forms.TextBox                                    ' assign textboxes to object Txt
    5.         For Each Txt In gpbValues.Controls
    6.             Txt.Text = ""
    7.         Next Txt
    8.  
    9.  
    10.     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.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim objTextBox As Object
    3.         For Each objTextBox In Me.ActiveForm.Controls
    4.             If TypeOf objTextBox Is TextBox Then
    5.                 objTextBox.text = ""
    6.             End If
    7.         Next
    8.     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]

  3. #3

    Thread Starter
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265
    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.
    Nick.

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    here's a better way and you can leave Option Strict set to "ON"

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim x As Control
    3.         For Each x In Me.Controls
    4.             If TypeOf x Is TextBox Then
    5.                 x.Text = ""
    6.             End If
    7.         Next
    8.     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]

  5. #5

    Thread Starter
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265
    Perfect

    Thanks for all your help.
    Nick.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width