Results 1 to 6 of 6

Thread: For Each Question....

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Posts
    217
    How would i make all the textboxes in my form become disabled?? Am I on the right track?? thank you

    Private Sub Command1_Click()
    Dim text As TextBox
    For Each text In ???
    text.Enabled = False
    Next
    End Sub

  2. #2
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Cool Very close!

    This should work

    Private Sub Command1_Click()
    For Each TextBox In Me
    TextBox.Enabled = Fasle
    Next
    End Sub
    Matt

  3. #3
    Guest
    There are probably better ways to do it, but one way to do it would be to loop through the forms controls collection examing each control to see if it's a textbox (I use a custom function here based on the textbox's MultiLine property - there's gotta be a better way but I couldn't find it in my limited search) and disabling it if it is a textbox. I'm checking the MultiLine property to see if a control is a textbox as I don't think any other controls have this property.

    The code would look something like this for the loop:

    Code:
      Dim ctl As Control
      
      For Each ctl In Me.Controls
        If IsTextBox(ctl) Then
          ctl.Enabled = False
        End If
      Next
    And the IsTextBox() code would read:

    Code:
    Public Function IsTextBox(ctl As Control) As Boolean
      Dim blnReturn As Boolean
      
      blnReturn = False  '  Default value.
      
      On Error GoTo Error_Handler
      
      blnReturn = (ctl.MultiLine = ctl.MultiLine)
      
    Error_Handler:
    
      IsTextBox = blnReturn
    
    End Function
    This code has been tested and does work.

    Paul

  4. #4
    Guest
    Why do you need that function? It's much easier to use the TypeOf statement.
    Code:
    Dim ctl As Control
                                
    For Each ctl In Controls
        If TypeOf ctl Is TextBox Then
            ctl.Enabled = False
        End If
    Next ctl

  5. #5
    Guest
    Like I said Megatron, I knew there was a better way, I just couldn't remember/find it! Thanks for the insight.

    Paul

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Posts
    217
    thanks for all the comments..

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