Professional Web Applications Themes

Object looping ? - ASP.NET General

Good evening, I have a panel with a number of labels and textboxes. I want to set the text boxes to readonly = false. This is what I have; Dim objTxtBox As New TextBox For Each objTxtBox In pnl_FB_Details.Controls objTxtBox.ReadOnly = False Next I get err msg: cast not valid. What am I doing wrong ? Thanks Deasun...

  1. #1

    Default Object looping ?

    Good evening,

    I have a panel with a number of labels and textboxes.
    I want to set the text boxes to readonly = false.
    This is what I have;

    Dim objTxtBox As New TextBox
    For Each objTxtBox In pnl_FB_Details.Controls
    objTxtBox.ReadOnly = False
    Next

    I get err msg: cast not valid.
    What am I doing wrong ?

    Thanks
    Deasun
    Deasun Guest

  2. #2

    Default Re: Object looping ?

    You are assuming every object is a Textbox - which it isn't. So the cast
    fails when you encounter something that's not an object. Additionally,
    there is no need to initialize objTxtBox to a new instance of textbox -
    since you never actually use it.

    Something like this should work:

    For Each ctl as Control In pnl_FB_Details.Controls
    If typeof(ctl) Is TextBox
    ctl.ReadOnly = False
    End If
    Next

    "Deasun O'Donnachadha" <com> wrote in message
    news:020001c365c3$aa3d2d70$gbl... 


    Marina Guest

  3. #3

    Default Re: Object looping ?

    You're doing a couple of things wrong, one of which you're not aware of:
     

    That won't throw an Exception, but why instantiate a new TextBox when you're
    going to do nothing with it but replace it with TextBoxes in the Form?
    That's a waste of processing and memory. Do this instead:
     

    Tip: Don't use coding shortcuts unless you understand them.

    Second error: A Control's collection is a collection of Controls, not
    TextBoxes. You have to cast each control that is a TextBox to the type
    TextBox. Example:

    Dim intCt As Integer
    For intCt = 0 to pnl_FB_Details.Controls.Count - 1
    CType(pnl_FB_Details.Controls(intCt), TextBox).ReadOnly = False
    Next

    - or -

    Dim objControl As Control
    For Each objControl in pnl_FB_Details.Controls
    CType(objControl, TextBox).ReadOnly = False
    Next


    One more thing: Make sure that if you use the above code, there are no
    Controls on that Panel that are NOT TextBoxes. Otherwise, you may have to
    use Reflection to determine whether any given Control is a TextBox.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Neither a follower nor a lender be.

    "Deasun O'Donnachadha" <com> wrote in message
    news:020001c365c3$aa3d2d70$gbl... 


    Kevin Guest

Similar Threads

  1. Replies: 2
    Last Post: June 6th, 04:02 PM
  2. Looping
    By Ronsweb in forum Macromedia ColdFusion
    Replies: 4
    Last Post: May 5th, 02:57 PM
  3. looping over a query object
    By fu-meng in forum Coldfusion - Advanced Techniques
    Replies: 6
    Last Post: March 24th, 04:06 PM
  4. Replies: 3
    Last Post: September 29th, 03:25 AM
  5. Looping or what?
    By Zac Hillier - Net Affectors in forum PHP Development
    Replies: 0
    Last Post: July 9th, 01:12 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139