Results 1 to 13 of 13

Thread: adding controls in collection

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    adding controls in collection

    Hi,

    I have this resiz code for moving and resizing controls on a form. What I'm trying to do is to use initially the tag property of each control to gather its size, position, etc... and then call up a resizer function by passing each control to it. How do I reference each control to pass it to this function:

    VB Code:
    1. Private Sub Form_Resize()
    2.  
    3.     Dim Ctl As Control
    4.  
    5.     Set Ctl = Frame1
    6.     Call Resizer(Ctl) 'but this passes the caption to the resizer function. Maybe I could use a collection as I have dozens of controls and could just loop them through. Thanks.
    7.  
    8. End Sub

  2. #2
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    ok, first, you don't really need to use the tag porperty because when you pass the reference to a control to your function, you will also get all those information since your functin will have access to all of the control's properties... not just the caption.

    To reference all the control to your function, just use a for each loop on the control collection of the form,

    VB Code:
    1. dim Ctl as Control
    2.  
    3. for each Ctl in form1.controls
    4.  
    5.     resizer(Ctl)
    6.  
    7. next

    then, in your resizer function, use the .move method of the control to move and resize it like you want

    VB Code:
    1. private function Resizer(Ctl as control)
    2.  
    3.     Ctl.move x,y,width,height
    4.  
    5. end function

    Hopes this will help you.

    - Valkan
    'You keep creatures in cages and release them to fight? That's sick!'

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Thanks. And to add a list of controls to a collection I would do this:

    [Highlight=VB]

    Dim col As New Collection

    Set Ctrl = Command6
    col.Add Ctrl

    Correct? This way I can access all properties for those controls that are in the collection.. Thanks.

    Mike.

  4. #4
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    If you want to use your own collection you can also just do something like col.add command6 to add a reference to the control. And yes, you will have access to all the properties of the referenced control.

    - Valkan
    'You keep creatures in cages and release them to fight? That's sick!'

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Many thanks Valkan.

    Now I have another problem: when I use control arrays, the collection does not get populated with all the items from the array, rather only the first one does get in the collection:

    VB Code:
    1. For n = 0 To 24
    2.         Frame001(n).Tag = Frame001(n).Left & " " & Frame001(n).Top & " " & Frame001(n).Width & " " & Frame001(n).Height & " " & Frame001(n).FontSize
    3.         Set ctrl = Frame001(n)
    4.         col.Add ctrl
    5.     Next

    I get 24 instances of the first control in the array. How can I fix this?

    Thanks a mil, again.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    *bump*

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    The frames are on 5 tabs and there are 5 SSTabs in all. So you have 5 tabs on each of the 5 SSTab. Hence the upper number of 24 in the for loop. All frames are located exactly at the same place on each SSTab and individual tabs.

    When I iterate all the items in the collection with a debug.print statement this is what I get:

    Frame001/4680 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/4680 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/4680 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/4680 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/4680 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75
    Frame001/-70320 360 2775 5295 9.75

    As you can see when the index hits a tab that is located as the first tab on each SSTab, the Left position is returned correctly. But then as we move down into subsequent tabs the Left property thows up some incorrect number when it should rerally be the same as all frames are at the same location on all tabs.. Would anyone know what is going on. Looks like it might be to do with the visibility of each frame, ie the visible property of each first frame on each SSTab. Thanks to anyone able to help.

  8. #8
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    hum... i'm not really sure what can cause this. I've tried adding ellement from a control collection to my own collection in a loop and it seemed to work. Could you post your project so I could take a look at your code?

    - Valkan
    'You keep creatures in cages and release them to fight? That's sick!'

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Here goes. I only posted the part of the code that concerns the resize bit.

    I tried hardcoding the left property of the frames only but the controls you see such as the maber button or the text boxes disappear completely from view. It is the most extraordinary thing.

    VB Code:
    1. For n = 0 To 24
    2.         Frame001(n).Left = 190
    3.         Frame001(n).Tag = Frame001(n).Left & " " & Frame001(n).Top & " " & Frame001(n).Width & " " & Frame001(n).Height & " " & Frame001(n).FontSize
    4.         'Debug.Print Frame001(n).Tag
    5.         'ReDim Preserve col(n + 2)
    6.         'Set col(n + 1) = Frame001(n)
    7.         Set ctrl = Frame001(n)
    8.         col.Add ctrl
    9.     Next

    Many thanks.
    Attached Files Attached Files

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    maber, read: amber

  11. #11
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    ok, if i got it right, you're using 1 collection for each type of control in your project so you can resize them properly according to what type of control they are. However, this could be done a lot simpler with less coding and less unecessery collection. Since you seem to use all controls in your form, it would be easier to use the form's control collection and then using the TypeName function to find the type of control. It could be something like this:
    VB Code:
    1. Private Sub Form_Resize()
    2.  
    3. dim ctrl as control
    4.  
    5.     for each ctrl in form1.controls 'Change form1 to the name of your form
    6.         Resizer(Ctrl)
    7.  
    8.     next ctrl
    9.  
    10. end sub
    11.  
    12. private sub Resizer(Ctrl as control)
    13.  
    14.     Select case TypeName(ctrl)
    15.  
    16.         Case "PictureBox":
    17.        
    18.             'Your code to resize PictureBox
    19.  
    20.         case "SSTab":
    21.  
    22.             'etc...
    23.  
    24.     End Select
    25.  
    26. end sub

    Also, you don't need to use the .Tag property. In the Resizer function, just use 'Ctrl.left, Ctrl.width etc...'

    - Valkan
    'You keep creatures in cages and release them to fight? That's sick!'

  12. #12

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Yes, but I believe i need to use initial values in the code that i have as using the current values of a given control causes the controls to go haywire when resized.

    Also, the problem persists, as the amber dot and greyish text boxes disappear.

    Will play with yout code further.

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