Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically resizing controls when screen resized 2

Status
Not open for further replies.

thesleepylizard

Programmer
Mar 18, 2002
41
AU
As the subject suggests, I want to be able to dynamically move and resize controls whenever a window gets resized. So if the window is made bigger, the contents will become bigger to fill out the space.

I have done this before simply by hard coding it in - editing .Top, .Height, .Left, and .Width for each control that needs to be moved, but for a somewhat complex screen this means lots of mucking around and it's not very flexible if I want to add new controls or move them on the screen.

So, I'm wondering if there's some standard technique, module, ActiveX control, API functons or macros that allow you to do this very easily and let you avoid setting .Top .Height etc... manually each time.

Thanks for your time,
-Sleepy

 
The algorythm is fairly straightforward, but there are a number of pitfalls that you need to be aware of.

First, calculate a HeightFactor and a WidthFactor.

dim HeightFactor as Single
dim WidthFactor as Single

HeightFactor = NewWindowHeight / OldWindowHeight
WidthFactor = NewWindowWidth / OldWindowWidth

Loop thru your controls collection, and for each that apply, call the following subroutine passing the control and a byref parameter.

dim TheControl as Control

For Each TheControl in Me.Controls
SizeThisControl ThisControl
Next

Private Sub SizeThisControl(ControlID As Control)

On Error GoTo HandleError

Dim NewWidth As Long
Dim NewTop As Long
Dim NewLeft As Long
Dim NewHeight As Long

With ControlID
NewTop = Int((.Top * gHeightFactor) + 0.5)
NewHeight = Int((.Height * gHeightFactor) + 0.5)
NewLeft = Int((.Left * gWidthFactor) + 0.5)
NewWidth = Int((.Width * gWidthFactor) + 0.5)
.Top = pLng_NewTop
.Left = pLng_NewLeft
.Width = pLng_NewWidth
.Height = pLng_NewHeight
End With

Exit Sub

HandleError:

Resume Next

End Sub

Some of the pitfalls that you need to be aware of:

1. Watch out for FontSize - you may or may not want to adjust the .FontSize property, but not all controls have that property.

2. If you controls are on tabs, then if would not be a good idea to adjust the size and/or position of that control unless you are actually on that tab.

3. You may have events triggered on the controls as they are moved and resized, so you need to handle those.

4. Be sure to keep the error handler because of such things as "Left Property cannot be read a run-time" -- this will happen if your control happens to be a timer.
Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
Cajun you outdo yourself... Craig, mailto:sander@cogeco.ca

Remember not to name the Lambs...
It only makes the chops harder to swallow
 
Try this one if you have an extra $200.00 bucks around there product works very well.

The problem I had when trying to write my own resizing code was that some controls on the form did not resize right, some would not resize at all. And some just did strang things..

The company abover used to have a product VSOCX was a verson you could d/l and use free for yourself - had to pay if selling products... I still use the old verson. if you can find that it works great too. "The beauty of the second amendment is, that it will not be needed until they try to take it." - Thomas Jefferson

WebMaster:
 
Try this one if you have an extra $200.00 bucks around their product works very well.

The problem I had when trying to write my own resizing code was that some controls on the form did not resize right, some would not resize at all. And some just did strange things..

The company above used to have a product VSOCX was a verson you could d/l and use free for yourself - had to pay if selling products... I still use the old verson. if you can find that it works great too. I also understand that the new .NET version of VBhas built in resizing abilties.. if one can afford to buy it. "The beauty of the second amendment is, that it will not be needed until they try to take it." - Thomas Jefferson

WebMaster:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top