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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resizing forms and controls.. 1

Status
Not open for further replies.

Lothster

Programmer
Nov 30, 2002
8
US
Just wondering what the easiest way to resize a form and have the controls resize with it properly..

Don't know if I'll get much of a reply with this one, but any suggestions would be great.
Thanks
Lothster
 
There are two parts to this code..
one is in the form itself and the other is in a module
Be Aware that this code will resize all the controls except a check box,combobox,listbox and your text size. However with a little manipulation I am sure you can write a routine for the text..if you do..let me know, I am still trying to do that.

put this code into the form you want to resize

************Start Form Code***************
Option Explicit
Private mintSaveWidth As Integer
Private mintSaveHeight As Integer

'additional vars for min and max
Dim intMinHeight As Integer, intMinWidth As Integer



Private Sub Form_Activate()
'Save Last height and width
mintSaveWidth = Me.Width
mintSaveHeight = Me.Height
End Sub

Private Sub Form_Load()
'Get the form min height and width
'Assume the programmer correctly sized the form initially
intMinHeight = Me.Height
intMinWidth = Me.Width
End Sub

Private Sub Form_Resize()
If (Me.Height >= intMinHeight) And (Me.Width >= intMinWidth) Then
ResizeControls Me, mintSaveWidth, mintSaveHeight
Else
'Force the form back to its last size
Me.Width = mintSaveWidth
Me.Height = mintSaveHeight
End If

End Sub
***************End Form Code*********************
then put this code into your module..then modify as you wish

**************Start Module Code******************
Option Explicit

Public Sub ResizeControls(frm As Form, oldWidth As Integer, _
oldHeight As Integer)
'Resize and readjust positions of all controls on the form

'Don't try to adjust minimized form.
If oldWidth < 1 Or oldHeight < 1 Then Exit Sub

Dim ratioW As Single, ratioH As Single
ratioW = frm.Width / oldWidth
ratioH = frm.Height / oldHeight

On Error Resume Next
Dim ctrl As Control
For Each ctrl In frm.Controls
With ctrl
.Width = .Width * ratioW
.Left = .Left * ratioW
.Height = .Height * ratioH
.Top = .Top * ratioH
End With
Next ctrl

oldWidth = frm.Width
oldHeight = frm.Height

End Sub

*****************End Module Code****************

zgtrman
 
That depends on your form's design. Here's how I handle it, 2 ways:

1) Use constant margin/s in between controls, horizontally and vertically. When resizing horizontally, move/resize controls on the rightmost first so you can use its Left property to use for the next control. When resizing vertically, move/resize controls on the bottom first so to be able to use the Top property.

2) Resizing by percent, not exceeding 100%, useful for grid controls.

One setback though, the more controls you resize, you'll experience refresh delays when resizing your form.
As always, that depends on how you designed your form. As to what event, under Form_Resize() event (by zgtrman)
Hope this helps.

[peace]
 
As you resize, you may need to watch for fontsizes as well.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
At least at a glance, the code from zgtrman shouldn't do anything (at least re re-sizing form/controls), unless the USER manually re-sizes the Form by dragging the boarder.

In the activale and load procedures, only the current dimensions are saved to local variables.

The resize event code doesn't activate unless / untill the form is manually (drag the boarder) re-sized.

While Lothster did not specify the process for the re-sizing &quot;event&quot;, a more common approach would be to 'assume' the goal is to re-size all forms to the screen resoloution available on a specific machine, to have the same (viewing) capability on all machines running the app.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Yes, John -BUT, without some value to re-size to nothing will happen

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
That worked perfectly... almost..
I also have a sstab control in there, but only the controls on the currently selected tab get resized properly... the rest of them on the other tabs just dissapear.
I'm currently working with it and should be able to figure it out, but any shortcuts would be greatly appreciated once again. Thanks.
Lothster.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top