Results 1 to 8 of 8

Thread: For..Each next loops

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Columbus Ohio
    Posts
    217

    Unhappy

    What exactly is wrong with this code?
    Private Sub VScroll1_Change()
    For Each TextBox In frmAdd
    Let TextBox.Top = TextBox.Top + -1 * (VScroll1.Value + 10)
    Next
    End Sub

    its saying the top property cannot be read at runtime
    Chris

    [email protected]
    Windows XP RC2 B2526
    Visual Studio.Net Beta 2
    C++, VB, VB.Net, ASP, PHP

  2. #2
    Guest
    The code works fine for me. Do you have textboxes on your form for the scrollbar to move them?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Columbus Ohio
    Posts
    217

    yes i do

    do the text boxes have to be named textbox in this code? Sorry ive never tried for each loops before. Thanks matthew, wow a guru helping me
    Chris

    [email protected]
    Windows XP RC2 B2526
    Visual Studio.Net Beta 2
    C++, VB, VB.Net, ASP, PHP

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Well I didn't get it to work as is:

    But,

    Code:
    Private Sub VScroll1_Change()
      Dim aControl As Control
      For Each aControl In frmAdd.Controls
        If TypeName(aControl) = "TextBox" Then
          Let aControl.Top = aControl.Top + -1 * (VScroll1.Value + 10)
        End If
      Next
    End Sub
    works.

    If there is another way without the test, then let me know Matthew (or anyone).

    Regards

    Paul Lewis

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'I didn't get it to work either
    'but this does
    Option Explicit
    
    Public Sub SetTop()
    '
    'mycontrol = control and increment is increment for forms
    'some situtations use more than one form.
    '
        Dim ctlMyControl As Control
        Dim intIncrement As Integer
    '
            For intIncrement = 0 To Forms.Count - 1
                For Each ctlMyControl In Forms(intIncrement).Controls
    '
        If TypeOf ctlMyControl Is textbox Then
    '
                ctlMyControl.Top = ctlMyControl.Top + (-1 * (VScroll1.Value + 10))
    '
        End If
    '
            Next ctlMyControl
    '
                Next intIncrement
    '
    End Sub
    
    Private Sub VScroll1_Change()
        Call SetTop
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Columbus Ohio
    Posts
    217

    thanks but why wont my first code work?

    i have 2 textboxes and the scrollbar
    Chris

    [email protected]
    Windows XP RC2 B2526
    Visual Studio.Net Beta 2
    C++, VB, VB.Net, ASP, PHP

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Talking Because you are fibbing :)

    You have more than 2 text boxes and a vscroll bar on your form. I guarantee it.

    Your code is attempting to move EVERY control on the form. I have assumed you declared your variable "TextBox" as a Variant, or that you do not have Option Explicit at the head of your module.

    In either case, "TextBox" in your code is merely a variable name like "a" or "b".

    In my code and HeSaidJoe's we explicitly check what sort of control we have and then move it.

    I hope that explains it to you?

    It would have helped you if you had Option Explicit in your module. This would help because you would have recieved an error in your code which would have hinted to you that TextBox needs to be decalred. Then you would have thought about what type of data it will be...

    Anyhow enough preaching

    Cheers
    Paul Lewis

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Columbus Ohio
    Posts
    217

    Talking Thanks ;)

    Thanks I understand what I was missing now.
    Chris

    [email protected]
    Windows XP RC2 B2526
    Visual Studio.Net Beta 2
    C++, VB, VB.Net, ASP, PHP

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