Container for programmatically positioning group of controls on form

  • Thread starter JohnM77 via AccessMonster.com
  • Start date
J

JohnM77 via AccessMonster.com

I have a form on which I have a group of associated controls (textboxes,
comboboxes and checkboxes) that I wish to programmatically position. VB6 used
to allow using a frame control in which the programmer could add various
controls, and then simply position the frame (top and left properties),
instead of having to position each individual control. It seems that the tab
control would be suitable, but programmatically setting the top/left
properties does not change the tab control's position.

Is there a better control for this use, or am I simply using the wrong
properties to position the tab control?

Thanks,
John
 
M

Marshall Barton

JohnM77 said:
I have a form on which I have a group of associated controls (textboxes,
comboboxes and checkboxes) that I wish to programmatically position. VB6 used
to allow using a frame control in which the programmer could add various
controls, and then simply position the frame (top and left properties),
instead of having to position each individual control. It seems that the tab
control would be suitable, but programmatically setting the top/left
properties does not change the tab control's position.


I don't remember the details, but I ran into several issues
when I trid to use a tab control for something similar. I
eventually gave up on the tab control idea and went back to
the old tried and true way of managing a group of controls.

Set the Tag property to something like G to identify the
controls in your "group". Then the code to reposition the
controls would be along these lines:

Dim ctl As Control
For Each ctl In Me.Comtrols
If ctl.Tag = "G" Then
ctl.Left = x
ctl.Top = y
End If
Next ctl

If the form has a lot of other controls that are not in the
"group", you should consider using the form's Open event to
initialize a modul level collection with just the controls
in the "group". Then the loop won't waste time going
through all the controls in the form or testing the Tag.
 
J

JohnM77 via AccessMonster.com

Thanks for your reply. I've about exhausted my googling for info on this one.
It seems that the contained controls do not move with the tab control, and
therefore the tab control serves no purpose for this particular use.

I could definitely see your method working well for a very ordered array of
controls, but since my controls are not in a perfectly orderly array, I'm
thinking the best method of positioning the controls is to code each control
individually.

Marshall said:
I have a form on which I have a group of associated controls (textboxes,
comboboxes and checkboxes) that I wish to programmatically position. VB6 used
[quoted text clipped - 3 lines]
control would be suitable, but programmatically setting the top/left
properties does not change the tab control's position.

I don't remember the details, but I ran into several issues
when I trid to use a tab control for something similar. I
eventually gave up on the tab control idea and went back to
the old tried and true way of managing a group of controls.

Set the Tag property to something like G to identify the
controls in your "group". Then the code to reposition the
controls would be along these lines:

Dim ctl As Control
For Each ctl In Me.Comtrols
If ctl.Tag = "G" Then
ctl.Left = x
ctl.Top = y
End If
Next ctl

If the form has a lot of other controls that are not in the
"group", you should consider using the form's Open event to
initialize a modul level collection with just the controls
in the "group". Then the loop won't waste time going
through all the controls in the form or testing the Tag.
 
M

Marshall Barton

I don't understand what you mean re "a very ordered array of
controls". The approach I used does not rely on any
relationship among the controls other than they all move to
the same position relative to each other.

Doing them all individually can be a lot of tedious coding,
but might(?) be appropriate if you are shuffling the positon
of a list of controls. But, if you were doing that, a tab
control would be pretty useless too.
--
Marsh
MVP [MS Access]

Thanks for your reply. I've about exhausted my googling for info on this one.
It seems that the contained controls do not move with the tab control, and
therefore the tab control serves no purpose for this particular use.

I could definitely see your method working well for a very ordered array of
controls, but since my controls are not in a perfectly orderly array, I'm
thinking the best method of positioning the controls is to code each control
individually.

Marshall said:
I have a form on which I have a group of associated controls (textboxes,
comboboxes and checkboxes) that I wish to programmatically position. VB6 used
[quoted text clipped - 3 lines]
control would be suitable, but programmatically setting the top/left
properties does not change the tab control's position.

I don't remember the details, but I ran into several issues
when I trid to use a tab control for something similar. I
eventually gave up on the tab control idea and went back to
the old tried and true way of managing a group of controls.

Set the Tag property to something like G to identify the
controls in your "group". Then the code to reposition the
controls would be along these lines:

Dim ctl As Control
For Each ctl In Me.Comtrols
If ctl.Tag = "G" Then
ctl.Left = x
ctl.Top = y
End If
Next ctl

If the form has a lot of other controls that are not in the
"group", you should consider using the form's Open event to
initialize a modul level collection with just the controls
in the "group". Then the loop won't waste time going
through all the controls in the form or testing the Tag.
 
A

Albert D. Kallal

Consider using a sub-form and placing the controls in that group....

at the end of the day, you likely want to avoid designs that have to move
controls around..but a sub-form is a possible workaround...
 
D

Dale Fye

Another option would be to add a rectangle to your form, set it's visible
property to False and send it to back. Put your other controls over it, like
you would have with a frame.

Then, create a subroutine which you pass the name of the rectangle, and the
new top and left positions.

Inside the subroutine, identify the rectangles initial top, left, height and
width positions, and loop through the controls to identify which controls
have a Top/Left position that is within the original rectangle. Then move
them to a new position that shifts their top and left coordinates based on
the difference in the rectangles original and new top/left positions.

-----
HTH
Dale



JohnM77 via AccessMonster.com said:
Thanks for your reply. I've about exhausted my googling for info on this one.
It seems that the contained controls do not move with the tab control, and
therefore the tab control serves no purpose for this particular use.

I could definitely see your method working well for a very ordered array of
controls, but since my controls are not in a perfectly orderly array, I'm
thinking the best method of positioning the controls is to code each control
individually.

Marshall said:
I have a form on which I have a group of associated controls (textboxes,
comboboxes and checkboxes) that I wish to programmatically position. VB6 used
[quoted text clipped - 3 lines]
control would be suitable, but programmatically setting the top/left
properties does not change the tab control's position.

I don't remember the details, but I ran into several issues
when I trid to use a tab control for something similar. I
eventually gave up on the tab control idea and went back to
the old tried and true way of managing a group of controls.

Set the Tag property to something like G to identify the
controls in your "group". Then the code to reposition the
controls would be along these lines:

Dim ctl As Control
For Each ctl In Me.Comtrols
If ctl.Tag = "G" Then
ctl.Left = x
ctl.Top = y
End If
Next ctl

If the form has a lot of other controls that are not in the
"group", you should consider using the form's Open event to
initialize a modul level collection with just the controls
in the "group". Then the loop won't waste time going
through all the controls in the form or testing the Tag.
 
D

Dale Fye

Here is some sample code. I used the detail sections MouseUp event to call
the subroutine in my test. You will obviously need to use some other method
to do this.

Public Sub MoveControls(MyBox As Rectangle, NewLeft As Double, NewTop As
Double)

Dim frm As Form
Dim OldTop As Double, OldLeft As Double
Dim OldRight As Double, OldBottom As Double
Dim ShiftTop As Double, ShiftLeft As Double

Dim ctrl As Control

Set frm = MyBox.Parent

OldTop = MyBox.Top
OldLeft = MyBox.Left
OldBottom = OldTop + MyBox.Height
OldRight = OldLeft + MyBox.Width

frm.InsideWidth = fnMax(frm.InsideWidth, NewLeft + (OldRight - OldLeft))
frm.InsideHeight = fnMax(frm.InsideHeight, NewTop + (OldBottom - OldTop))

MyBox.Top = NewTop
MyBox.Left = NewLeft

ShiftTop = NewTop - OldTop
ShiftLeft = NewLeft - OldLeft
For Each ctrl In frm.Controls
If ctrl.Left >= OldLeft And ctrl.Left <= OldRight And _
ctrl.Top >= OldTop And ctrl.Top <= OldBottom Then
ctrl.Top = ctrl.Top + ShiftTop
ctrl.Left = ctrl.Left + ShiftLeft
End If
Next

End Function

----
HTH
Dale



Dale Fye said:
Another option would be to add a rectangle to your form, set it's visible
property to False and send it to back. Put your other controls over it, like
you would have with a frame.

Then, create a subroutine which you pass the name of the rectangle, and the
new top and left positions.

Inside the subroutine, identify the rectangles initial top, left, height and
width positions, and loop through the controls to identify which controls
have a Top/Left position that is within the original rectangle. Then move
them to a new position that shifts their top and left coordinates based on
the difference in the rectangles original and new top/left positions.

-----
HTH
Dale



JohnM77 via AccessMonster.com said:
Thanks for your reply. I've about exhausted my googling for info on this one.
It seems that the contained controls do not move with the tab control, and
therefore the tab control serves no purpose for this particular use.

I could definitely see your method working well for a very ordered array of
controls, but since my controls are not in a perfectly orderly array, I'm
thinking the best method of positioning the controls is to code each control
individually.

Marshall said:
I have a form on which I have a group of associated controls (textboxes,
comboboxes and checkboxes) that I wish to programmatically position. VB6 used
[quoted text clipped - 3 lines]
control would be suitable, but programmatically setting the top/left
properties does not change the tab control's position.

I don't remember the details, but I ran into several issues
when I trid to use a tab control for something similar. I
eventually gave up on the tab control idea and went back to
the old tried and true way of managing a group of controls.

Set the Tag property to something like G to identify the
controls in your "group". Then the code to reposition the
controls would be along these lines:

Dim ctl As Control
For Each ctl In Me.Comtrols
If ctl.Tag = "G" Then
ctl.Left = x
ctl.Top = y
End If
Next ctl

If the form has a lot of other controls that are not in the
"group", you should consider using the form's Open event to
initialize a modul level collection with just the controls
in the "group". Then the loop won't waste time going
through all the controls in the form or testing the Tag.
 
D

Dale Fye

forgot the code for fnMax, which will resize the form if the new position
will cause the rectangle to overlap the original form size.

Public Function fnMax(ParamArray ValList() As Variant) As Variant

Dim intLoop As Integer
Dim myVal As Variant

For intLoop = LBound(ValList) To UBound(ValList)
If Not IsNull(ValList(intLoop)) Then
If IsEmpty(myVal) Then
myVal = ValList(intLoop)
ElseIf ValList(intLoop) > myVal Then
myVal = ValList(intLoop)
End If
End If
Next
fnMax = myVal

End Function

----
HTH
Dale



Dale Fye said:
Here is some sample code. I used the detail sections MouseUp event to call
the subroutine in my test. You will obviously need to use some other method
to do this.

Public Sub MoveControls(MyBox As Rectangle, NewLeft As Double, NewTop As
Double)

Dim frm As Form
Dim OldTop As Double, OldLeft As Double
Dim OldRight As Double, OldBottom As Double
Dim ShiftTop As Double, ShiftLeft As Double

Dim ctrl As Control

Set frm = MyBox.Parent

OldTop = MyBox.Top
OldLeft = MyBox.Left
OldBottom = OldTop + MyBox.Height
OldRight = OldLeft + MyBox.Width

frm.InsideWidth = fnMax(frm.InsideWidth, NewLeft + (OldRight - OldLeft))
frm.InsideHeight = fnMax(frm.InsideHeight, NewTop + (OldBottom - OldTop))

MyBox.Top = NewTop
MyBox.Left = NewLeft

ShiftTop = NewTop - OldTop
ShiftLeft = NewLeft - OldLeft
For Each ctrl In frm.Controls
If ctrl.Left >= OldLeft And ctrl.Left <= OldRight And _
ctrl.Top >= OldTop And ctrl.Top <= OldBottom Then
ctrl.Top = ctrl.Top + ShiftTop
ctrl.Left = ctrl.Left + ShiftLeft
End If
Next

End Function

----
HTH
Dale



Dale Fye said:
Another option would be to add a rectangle to your form, set it's visible
property to False and send it to back. Put your other controls over it, like
you would have with a frame.

Then, create a subroutine which you pass the name of the rectangle, and the
new top and left positions.

Inside the subroutine, identify the rectangles initial top, left, height and
width positions, and loop through the controls to identify which controls
have a Top/Left position that is within the original rectangle. Then move
them to a new position that shifts their top and left coordinates based on
the difference in the rectangles original and new top/left positions.

-----
HTH
Dale



JohnM77 via AccessMonster.com said:
Thanks for your reply. I've about exhausted my googling for info on this one.
It seems that the contained controls do not move with the tab control, and
therefore the tab control serves no purpose for this particular use.

I could definitely see your method working well for a very ordered array of
controls, but since my controls are not in a perfectly orderly array, I'm
thinking the best method of positioning the controls is to code each control
individually.

Marshall Barton wrote:
I have a form on which I have a group of associated controls (textboxes,
comboboxes and checkboxes) that I wish to programmatically position. VB6 used
[quoted text clipped - 3 lines]
control would be suitable, but programmatically setting the top/left
properties does not change the tab control's position.

I don't remember the details, but I ran into several issues
when I trid to use a tab control for something similar. I
eventually gave up on the tab control idea and went back to
the old tried and true way of managing a group of controls.

Set the Tag property to something like G to identify the
controls in your "group". Then the code to reposition the
controls would be along these lines:

Dim ctl As Control
For Each ctl In Me.Comtrols
If ctl.Tag = "G" Then
ctl.Left = x
ctl.Top = y
End If
Next ctl

If the form has a lot of other controls that are not in the
"group", you should consider using the form's Open event to
initialize a modul level collection with just the controls
in the "group". Then the loop won't waste time going
through all the controls in the form or testing the Tag.
 
J

JohnM77 via AccessMonster.com

Thanks, Dale! That works pretty slick!

Dale said:
forgot the code for fnMax, which will resize the form if the new position
will cause the rectangle to overlap the original form size.

Public Function fnMax(ParamArray ValList() As Variant) As Variant

Dim intLoop As Integer
Dim myVal As Variant

For intLoop = LBound(ValList) To UBound(ValList)
If Not IsNull(ValList(intLoop)) Then
If IsEmpty(myVal) Then
myVal = ValList(intLoop)
ElseIf ValList(intLoop) > myVal Then
myVal = ValList(intLoop)
End If
End If
Next
fnMax = myVal

End Function

----
HTH
Dale
Here is some sample code. I used the detail sections MouseUp event to call
the subroutine in my test. You will obviously need to use some other method
[quoted text clipped - 93 lines]
 
D

Dale Fye

John,

Glad I could help.

Generally, I agree with Albert, and have never even considered moving
controls around on a form programmatically.

But sometimes I like to play during lunch.

----
HTH
Dale



JohnM77 via AccessMonster.com said:
Thanks, Dale! That works pretty slick!

Dale said:
forgot the code for fnMax, which will resize the form if the new position
will cause the rectangle to overlap the original form size.

Public Function fnMax(ParamArray ValList() As Variant) As Variant

Dim intLoop As Integer
Dim myVal As Variant

For intLoop = LBound(ValList) To UBound(ValList)
If Not IsNull(ValList(intLoop)) Then
If IsEmpty(myVal) Then
myVal = ValList(intLoop)
ElseIf ValList(intLoop) > myVal Then
myVal = ValList(intLoop)
End If
End If
Next
fnMax = myVal

End Function

----
HTH
Dale
Here is some sample code. I used the detail sections MouseUp event to call
the subroutine in my test. You will obviously need to use some other method
[quoted text clipped - 93 lines]
in the "group". Then the loop won't waste time going
through all the controls in the form or testing the Tag.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top