Object looping ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Deasun O'Donnachadha

    #1

    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.ReadO nly = False
    Next

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

    Thanks
    Deasun
  • Marina

    #2
    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" <tirnaog@mail.c om> wrote in message
    news:020001c365 c3$aa3d2d70$a60 1280a@phx.gbl.. .[color=blue]
    > 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.ReadO nly = False
    > Next
    >
    > I get err msg: cast not valid.
    > What am I doing wrong ?
    >
    > Thanks
    > Deasun[/color]


    Comment

    • Marina

      #3
      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" <tirnaog@mail.c om> wrote in message
      news:020001c365 c3$aa3d2d70$a60 1280a@phx.gbl.. .[color=blue]
      > 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.ReadO nly = False
      > Next
      >
      > I get err msg: cast not valid.
      > What am I doing wrong ?
      >
      > Thanks
      > Deasun[/color]


      Comment

      • Kevin Spencer

        #4
        Re: Object looping ?

        You're doing a couple of things wrong, one of which you're not aware of:
        [color=blue]
        > Dim objTxtBox As New TextBox[/color]

        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:
        [color=blue]
        > Dim objTxtBox As TextBox[/color]

        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_De tails.Controls( intCt), TextBox).ReadOn ly = False
        Next

        - or -

        Dim objControl As Control
        For Each objControl in pnl_FB_Details. Controls
        CType(objContro l, TextBox).ReadOn ly = 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
        All hotels in Benidorm. The best selection of Benidorm hotels with reviews and maps. Book in advance and save.

        Neither a follower nor a lender be.

        "Deasun O'Donnachadha" <tirnaog@mail.c om> wrote in message
        news:020001c365 c3$aa3d2d70$a60 1280a@phx.gbl.. .[color=blue]
        > 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.ReadO nly = False
        > Next
        >
        > I get err msg: cast not valid.
        > What am I doing wrong ?
        >
        > Thanks
        > Deasun[/color]


        Comment

        • Kevin Spencer

          #5
          Re: Object looping ?

          You're doing a couple of things wrong, one of which you're not aware of:
          [color=blue]
          > Dim objTxtBox As New TextBox[/color]

          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:
          [color=blue]
          > Dim objTxtBox As TextBox[/color]

          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_De tails.Controls( intCt), TextBox).ReadOn ly = False
          Next

          - or -

          Dim objControl As Control
          For Each objControl in pnl_FB_Details. Controls
          CType(objContro l, TextBox).ReadOn ly = 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
          All hotels in Benidorm. The best selection of Benidorm hotels with reviews and maps. Book in advance and save.

          Neither a follower nor a lender be.

          "Deasun O'Donnachadha" <tirnaog@mail.c om> wrote in message
          news:020001c365 c3$aa3d2d70$a60 1280a@phx.gbl.. .[color=blue]
          > 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.ReadO nly = False
          > Next
          >
          > I get err msg: cast not valid.
          > What am I doing wrong ?
          >
          > Thanks
          > Deasun[/color]


          Comment

          Working...