How do you set the Z order on a control in VB6? There doesn't seem to be a property for it...

It seems as though I have no control over which control takes the foreground :( I suspect it is based on the order they are declared in the .frm file (stuff you can't see in the IDE) but I want to do it programatically.

Recommended Answers

All 2 Replies

ZOrder is actually a method, not a property. You can only move a control all the way to the back:

me.myControl.ZOrder 1

or all the way to the front:

me.myControl.ZOrder 0

So, as you can guess, if you want specific controls to overlap in a specific order, you have to issue a ZOrder method against each overlapping control, in the proper order. What a pain.

Here's a little science-experiment code snippet that demonstrates how ZOrder works. Please note that you can alter this to suit your own needs:

Private Sub cmdChangeZOrderB2F_Click()
Me.Text1.ZOrder 0
Me.Text2.ZOrder 0
Me.Text3.ZOrder 0

End Sub

Private Sub cmdChangeZOrderF2B_Click()
Me.Text1.ZOrder 1
Me.Text2.ZOrder 1
Me.Text3.ZOrder 1
End Sub

This assumes three overlapping TextBox controls on a form, and two command buttons. Alternately clicking the command buttons will cause the overlap of the controls to flip. You could also use the same ZOrder parameter, but invert the order, like so:

Me.Text3.ZOrder 0
Me.Text2.ZOrder 0
Me.Text1.ZOrder 0

Hope this answers your question! Good luck!

commented: Good help +9

ZOrder is actually a method, not a property. You can only move a control all the way to the back:

me.myControl.ZOrder 1

or all the way to the front:

me.myControl.ZOrder 0

So, as you can guess, if you want specific controls to overlap in a specific order, you have to issue a ZOrder method against each overlapping control, in the proper order. What a pain.

Here's a little science-experiment code snippet that demonstrates how ZOrder works. Please note that you can alter this to suit your own needs:

Private Sub cmdChangeZOrderB2F_Click()
Me.Text1.ZOrder 0
Me.Text2.ZOrder 0
Me.Text3.ZOrder 0

End Sub

Private Sub cmdChangeZOrderF2B_Click()
Me.Text1.ZOrder 1
Me.Text2.ZOrder 1
Me.Text3.ZOrder 1
End Sub

This assumes three overlapping TextBox controls on a form, and two command buttons. Alternately clicking the command buttons will cause the overlap of the controls to flip. You could also use the same ZOrder parameter, but invert the order, like so:

Me.Text3.ZOrder 0
Me.Text2.ZOrder 0
Me.Text1.ZOrder 0

Hope this answers your question! Good luck!

Ouch! I'll tack this onto to the long list of reasons why VB6 sucks. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.