Control Z Order (1 Viewer)

Status
Not open for further replies.

ChrisO

Registered User.
Local time
Today, 10:22
Joined
Apr 30, 2003
Messages
3,202
G’day all.

The attached demo allows for *Controls to be moved in the Z order by command buttons, keyboard or drag and drop. It will leave the last selected or dropped Control at the front of the Z order for that type of Control.

Version……….........: Access 2003 (Access 2000 file format)
Tested with…….....: Windows XP and Windows 7
Screen resolution..: >= 1024 x 768
Regional settings...: English, French and German
References...........: None
Error handling….....: None

*Controls…..........: Bound Object Frames
Requirements……...: Control names must be the same as their Bound Field names
Objects supported.: Excel Worksheet, PowerPoint, Graph, Word Document, Clipart

Methods available to change runtime configuration and save:
Bulk move all; Top, Left, Right, Bottom
Button select.
Keyboard.
Drag, drop and resize.
Reset to design time configuration; not saved.


Forward:
As far as I know we can not change the Z order of controls at runtime. What we can do is change the properties and position on screen of controls to simulate a Z order change. This is the method used here.

I have no knowledge of the underlying order of controls in the Controls Collection so the information provided here has been obtained empirically. It is both a bit surprising and also useful for this need.

When we run through a Form’s Control Collection it appears that the returned Controls are found in Z order and the Z order is from back to front. If we have an incremental Index during the finding process then that Index is the Z order of that Control. It follows that if the find is restricted to a particular type of Control then the Index becomes the Z order of that particular type of Control in the Form’s Collection.

Now, if we create a Collection Object to maintain a Collection of a particular type of Class Module, then we can maintain a Collection of Controls within that Collection of Class Modules.

Collection initialization code behind a Form:-
Code:
Private colObjects As New Collection


Private Sub InitializeCollection()
    Dim lngIdx As Long
    Dim objCtl As Object
    Dim clsBOF As clsBoundObjectFrame
    
    For Each objCtl In Me
        If objCtl.ControlType = acBoundObjectFrame Then
            lngIdx = lngIdx + 1

            Set clsBOF = New clsBoundObjectFrame
            Set clsBOF.ThisBOF = objCtl

            clsBOF.Tag = lngIdx
            clsBOF.ControlName = objCtl.Name
            clsBOF.DefaultTop = objCtl.Top
            clsBOF.DefaultLeft = objCtl.Left
            clsBOF.DefaultWidth = objCtl.Width
            clsBOF.DefaultHeight = objCtl.Height
            clsBOF.SetEventHandlers = "[Event Procedure]"
            colObjects.Add Item:=clsBOF, Key:=CStr(lngIdx)
        End If
    Next objCtl
    
End Sub
Notes:
The Collection Object will take any type of Class Object.
Each Control found in the Form’s Controls Collection is filtered by acBoundObjectFrame.
Each filtered Control found is found by Z order. (Back to Front.)
On finding a filtered Control the lngIdx is incremented.
The lngIdx is then used as the Key for the instantiated Class Module in the Class Module Collection.
Since each instantiated Class Module contains a unique Bound Object Frame Control then each Bound Object Frame Control has a unique lngIdx.
That lngIdx is the design time Z order of that particular Bound Object Frame Control in the Form’s Control Collection.

Hence we have a saved reference to the Z order position of that particular Control in that particular type of Control collection as constructed in design mode. What that means is that we can revert, or reset, back to design mode Z order at runtime. Or we can use the Z order index to swap Control properties to simulate a change in the Z order at runtime.

Note if you are going to change the code:
When changing the Z order of the Controls, or loading the saved properties of the Controls, the Controls must not have the focus.


I hope this little explanation of the Z order of Controls helps.
If you have any questions, please ask them in the appropriate forum.


Version 3, 24-Nov-2011: Drag, drop and resize code cleanup.


Chris.
 

Attachments

  • DragAndDropWith_Z_Order_V3.zip
    72.1 KB · Views: 1,181
Last edited:
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom