dcsimg
Results 1 to 16 of 16

Thread: [RESOLVED] Cascading Controls On A Form...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    109

    Resolved [RESOLVED] Cascading Controls On A Form...

    Is there a way to cascade controls on a form? Not necessarily stacking them, per se, but making sure the controls closest to the top of the form are behind and the ones closest to the bottom are in front.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,500

    Re: Cascading Controls On A Form...

    Define "closest to the bottom":
    the upper edge (Top-Property) or the bottom edge (Top+Height)?
    get the height of your form (or the client-area), collect your controls, sort them by Top (or Top+Height), and then set the Z-Order
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,573

    Re: Cascading Controls On A Form...

    This is from MSDN:
    Three graphical layers are associated with forms andcontainers. The back layer is the drawing space where the results of the graphics methods are displayed. Next is the middle layer where graphical objects and Label controls are displayed. The front layer is where all nongraphical controls like CommandButton, CheckBox, or ListBox are displayed. Anything contained in a layer closer to the front covers anything contained in the layer(s) behind it. ZOrder arranges objects only within the layer where the object is displayed.
    SO, if you have 'different' controls (for example, shapes and labels AND commandbuttons and listboxes, you might have a little difficult time doing this with setting ZOrder (yes, you can do it using "Send To Back/Front" in the IDE, but at runtime using ZOrder, you can't 'overlap' layers (see quote).

    For example...using this code:
    Code:
    Private Sub Form_Load()
        Command1.ZOrder (0)
        List1.ZOrder (0)
        Option1.ZOrder (0)
        Text1.ZOrder (0)
        Command2.ZOrder (0)
        Shape1.ZOrder (0)
        Label1.ZOrder (0)
    End Sub
    Name:  Capture.JPG
Views: 69
Size:  17.9 KB[/CODE]

    where the 'Send to Back/Front" was randomly done in the IDE, you will note that the last two (closest to bottom) are in relation to each other (label1 (light green) is 'in front of' shape1 (orange), yet shape1 is behind command2 (blue)). The reason is that labels and shapes fall into a different layer (middle) than those other controls (command1-dark green, list1-magenta, option1-red, text1=pukey green, command2-blue), which are in the front layer.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    109

    Re: Cascading Controls On A Form...

    This is what I'm looking to make happen. All of the controls will be the same, not various different controls. I am generating arrays of the control at runtime, therefore I cannot state that all arrays are ZOrder 0. Imagine if a random number of arrays were made from a control at runtime and they were splashed all over the form in random places. Starting from the top and working your way to the bottom, each control would need to be in front of the last, as pictured below.

    Name:  cascade.png
Views: 51
Size:  3.2 KB

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    109

    Re: Cascading Controls On A Form...

    This is what I'm looking to make happen. All of the controls will be the same, not various different controls. I am generating arrays of the control at runtime, therefore I cannot state that all arrays are ZOrder 0. Imagine if a random number of arrays were made from a control at runtime and they were splashed all over the form in random places. Starting from the top and working your way to the bottom, each control would need to be in front of the last, as pictured below.

    Name:  cascade.png
Views: 51
Size:  3.2 KB

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    109

    Re: Cascading Controls On A Form...

    Not sure why that posted twice.

  7. #7
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,223

    Re: Cascading Controls On A Form...

    Code:
        Dim iTop() As Long
        Dim iCtl() As Control
        Dim c As Long
        
        ReDim iTop(Me.Controls.Count - 1)
        ReDim iCtl(Me.Controls.Count - 1)
        
        On Error Resume Next ' some controls doesn't have Top property at run time
        For c = 0 To Me.Controls.Count - 1
            iTop(c) = Me.Controls(c).Top
            Set iCtl(c) = Me.Controls(c)
        Next
        
        OrderVector iTop, iCtl
        
        For c = 0 To Me.Controls.Count - 1
            iCtl(c).ZOrder
        Next c
    I use OrderVector. It is a procedure to order a vector and can accept a secondary vector (or several) and order them according to the primary vector.
    You can use your own rountime for ordering. In this case I used my component available here.

    The ZOrder method of the controls brings that particular control to the front. This routine orders the controls by their Top property and brings each control to the front starting from top to bottom.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    109

    Re: Cascading Controls On A Form...

    Eduardo: Sorry, but that routine doesn't do what I'm asking. Your routine switches those that are in the back to the front and vice-versa. Take a look at the picture I provided above. The one on the left is where it is now. The one on the right is what I need it to do. I'm also looking for something that doesn't require an external control.

  9. #9
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,223

    Re: Cascading Controls On A Form...

    Quote Originally Posted by mcoulter876 View Post
    Eduardo: Sorry, but that routine doesn't do what I'm asking.
    Why?

    Quote Originally Posted by mcoulter876 View Post
    Your routine switches those that are in the back to the front and vice-versa.
    No.

    Quote Originally Posted by mcoulter876 View Post
    Take a look at the picture I provided above. The one on the left is where it is now. The one on the right is what I need it to do.
    It does exactly that.

    Quote Originally Posted by mcoulter876 View Post
    I'm also looking for something that doesn't require an external control.
    Use your own ordering routine. You can use Bubblesort, Quicksort, Combosort, Etc.
    You have several here.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    109

    Re: Cascading Controls On A Form...

    I literally copied your code and tested it. The controls that were in the back came to the front and the controls that were in the front went to the back. Not sure what you're needing here, but when testing that code, that's exactly what happened. Don't know what to tell you.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    109

    Re: Cascading Controls On A Form...

    Got it to work by throwing all of the control.top numbers into a list and sorting in numerically (lowest number at the top) and then going through said list and applying the ZOrder.

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,677

    Re: [RESOLVED] Cascading Controls On A Form...

    mcoulter876,

    I think what Eduardo was trying to suggest is that you must sort your tops after you've gathered them, but he didn't provide any sorting code for you.

    Here's his code reworked with a down-and-dirty sort algorithm thrown into the middle of it. I tested with a few textboxes and it seemed to work fine.

    Code:
    
    Option Explicit
    
    Private Sub Form_Click()
        Dim iTop() As Long
        Dim iCtl() As Control
        Dim c As Long
    
        ReDim iTop(Me.Controls.Count - 1)
        ReDim iCtl(Me.Controls.Count - 1)
    
        On Error Resume Next ' some controls doesn't have Top property at run time
        For c = 0 To Me.Controls.Count - 1
            iTop(c) = Me.Controls(c).Top
            Set iCtl(c) = Me.Controls(c)
        Next
    
    
    
        Dim i As Long
        Dim j As Long
        Dim ctl As Control
        Dim tmp As Long
        For i = LBound(iTop) To UBound(iTop) - 1
            For j = i + 1 To UBound(iTop)
                If iTop(i) > iTop(j) Then
                    Set ctl = iCtl(i): Set iCtl(i) = iCtl(j): Set iCtl(j) = ctl
                        tmp = iTop(i):     iTop(i) = iTop(j):     iTop(j) = tmp
                End If
            Next
        Next
    
    
    
        For c = 0 To Me.Controls.Count - 1
            iCtl(c).ZOrder
        Next c
    End Sub
    
    

    Good Luck,
    Elroy

    EDIT1: Also, if it were me, I'd build an "IfHasTop" function to test while adding things to the array, rather than using "On Error Resume Next". The way the code is, that could cause problems. You'd also need to track a counter and adjust the sizes of your array when you were done gathering.

    EDIT2: Just to be specific about one of the problems "On Error Resume Next" could cause, you're potentially re-ordering the z-order of any/all your lines, possibly making them overlap differently. Besides a line, I'm not sure what else wouldn't have a top property.
    Last edited by Elroy; Nov 10th, 2018 at 12:54 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  13. #13
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,223

    Re: Cascading Controls On A Form...

    Quote Originally Posted by mcoulter876 View Post
    I literally copied your code and tested it. The controls that were in the back came to the front and the controls that were in the front went to the back. Not sure what you're needing here, but when testing that code, that's exactly what happened. Don't know what to tell you.
    You must have a bug in the ordering routine. Please post your sample project where you tested it and I'll check what happens.

    Edit:

    Quote Originally Posted by mcoulter876 View Post
    Got it to work by throwing all of the control.top numbers into a list and sorting in numerically (lowest number at the top) and then going through said list and applying the ZOrder.
    Ah, OK.

    Edit2:
    You could also use Elroy's code for ordering.
    Last edited by Eduardo-; Nov 10th, 2018 at 12:47 PM.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    109

    Re: [RESOLVED] Cascading Controls On A Form...

    Thanks, Elroy. That worked perfectly and much better than what I was doing.

  15. #15
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,223

    Re: [RESOLVED] Cascading Controls On A Form...

    Quote Originally Posted by Elroy View Post
    I think what Eduardo was trying to suggest is that you must sort your tops after you've gathered them, but he didn't provide any sorting code for you.
    I saw this question yesterday and I didn't answer, waiting to see if someone else answered it.
    Today seeing that nobody answered, I did. But I didn't provide full code, but just the part that I thought was important: the idea of how to do it (it was easy anyway).
    Sorting algoryms are everywhere, and I didn't have one at hand.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  16. #16
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,500

    Re: Cascading Controls On A Form...

    Quote Originally Posted by mcoulter876 View Post
    Got it to work by throwing all of the control.top numbers into a list and sorting in numerically (lowest number at the top) and then going through said list and applying the ZOrder.
    Err... and this is different to my Answer how?
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width