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

How do people here deal with users resizing the windows? 1

Status
Not open for further replies.

JaeBrett

Programmer
May 5, 2003
196
0
0
CN
Obviously my application gets ugly when users resize... just curious how people code for this?
 
Code:
Form1.BorderStyle = 1

Of course, it's simpler to set this property in the object inspector.
 
harebrain ... made me laugh :)

You have to code your application so that it will resize the individual controls etc when the form resize, here's a little demo..
Make a new project, place a command button and a textbox onto the form

Private Sub Form_Load()
Text1.Top = 20
Text1.Left = 20
End Sub

Private Sub Form_Resize()
Text1.Width = Me.Width - 150
Text1.Height = Me.Height - 900
Command1.Left = Me.Width - 1400
Command1.Top = Me.Height - 800
End Sub
 
Hi,

Other things to consider / use ...

form.scalewidth
form.scaleheight
form.scaletop
form.scaleleft

These give you the "useable" portion of the Form. Use these when re-sizing stuff on the form.

As to what to do when the user resizes the form, this depends on what controls are on the form. If your form is a single richtextbox, then you can use

Code:
Private Sub Form_Resize()
    If frmMain.WindowState <> vbMinimized Then
        With Form1
            RTB.Move .ScaleLeft, .ScaleTop, .ScaleWidth, .ScaleHeight
        End With
    End If
End Sub

If you have a complex form with lots of stuff in it, (say a data entry screen in an accounting application) then you need to do more to make the screen re-size. There are a number of controls available for download - have a look on for example. However they all do the same thing - store the starting Left, Top, Width, Height in a property on the control (usually the TAG property), then when the form is resized, enumerate the controls on the form, examine the ratio between the form's current settings and the form's saved settings then apply the ratio change the the controls left, top, width, height properties based on the stored values.

Unfortunately, it doesnt stop there, as you have font issues to address as well. Do you resize a font? If so, you have to store the original font to know how to best re-set the font size. If you do not resize a font, then you need to ensure that all of the text in a text box is visible, so now you are setting fonts on the form and using form.textheight or form.textwidth to help in setting the font in a textbox. There are probably more things to worry about as well (such as how you store the original settings - probably something in form_load, and what if you are already using the TAG property for something else - you now need to re-write things so that both bits of information can co-exist, and if you dont use the TAG property, then maybe you are using array(s) to store the original information in). I suspect that screen resolution may also play havoc with this (and need to be catered for). Of course if you have grids (any variety) on the form, then you need to worry about fonts, row heights and column widths in the grid as well ... <sigh> or is that <yuk> ???

To boil this down into the proverbial nutshell, I quote harebrain (25/03/2003) who wrote
Code:
form1.BorderStyle = 1

Hope this helps ...
 
i third harebrain's response! Fixed windows are the way to go! (although i prefer borderstyle=4)
 
There are four options that I normally use.

1. Fixed window, as harebrain mentioned.

2. Use a sizeable border with the max button disabled. Then in the resize event I put in the original fixed height and width values. This will allow the form to be seen in the task bar.

3. Use WizeSizer to do the resizing for me. It is a free ocx ( This will allow you to keep the ratios of the controls the same and resize everything, including increased font sizes. I like this if the there is no scrollong on the form.

4. Manually resize the controls based on Scalewidth and Scaleheight. This I use for forms that display data in a grid format. I usually have a minimum size and anything larger just expands the grid so the user can see more data. The controls remain the same size.

Take Care,

zemp

&quot;Show me someone with both feet on the ground and I will show you someone who can't put their pants on.&quot;
 
I think I like the idea of BorderStyle = 1 (why would the user ever want to resize my perfect form anyway!). To be honest I don't really create that many applications where the user does need to resize the form so I haven't really looked into it before. I suppose you could do something to store the original values of each control (if you were even bothered about controls resizing?) and resize them based on the percentage change of the form.

e.g. Drop a few controls onto a form first
Code:
Option Explicit
Dim intDefaultHeight As Long
Dim intDefaultWidth As Long
Dim arrControls()
Dim i As Integer
Dim ctl As Control

Private Sub Form_Load()
Dim intControls As Integer

' Store original height and width of the form
intDefaultHeight = Me.Height
intDefaultWidth = Me.Width

' Find how many controls there are
For Each ctl In Me.Controls
    intControls = intControls + 1
Next

' set an array which is sized to the number of controls and with 3 elements (for height, width, top and left)
ReDim arrControls(intControls, 3)

' loop through each control and add the value to each element
i = 0
For Each ctl In Me.Controls
    arrControls(i, 0) = ctl.Height
    arrControls(i, 1) = ctl.Width
    arrControls(i, 2) = ctl.Top
    arrControls(i, 3) = ctl.Left
    i = i + 1
Next

End Sub

Private Sub Form_Resize()
Dim intHeight As Long
Dim intWidth As Long

' get the new height and width of the form
intHeight = Me.Height
intWidth = Me.Width

' loop through each control and assign a new value based on the % change that the user ahs applied
i = 0
For Each ctl In Me.Controls
    ctl.Height = arrControls(i, 0) * (intHeight / intDefaultHeight)
    ctl.Width = arrControls(i, 1) * (intWidth / intDefaultWidth)
    ctl.Top = arrControls(i, 2) * (intHeight / intDefaultHeight)
    ctl.Left = arrControls(i, 3) * (intWidth / intDefaultWidth)
    i = i + 1
Next

End Sub
suspect that most people won't actually need to resize the control, it will be more of where the controls are placed (and this is by no means complete code) but hopefully you get the idea...

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Thanks a lot ca8msm

I encounter an error 'Height' property is read-only

So, I just add some On Error GoTo and it works fine . ..

thanks again . . .

Please pardon the grammar.
Not good in english.
 
Here's a CLS I wrote to handle resizing/moving CTLs on a FRM:
Code:
'!! PLACE THE FOLLOWING CODE IN A FRM
Option Explicit

'!! THE CLASS IS USED IN THE FOLLOWING WAY
'!! Add the following CMDs to the FRM: cmdMatchWidth, cmdMatchHeight, cmdStayBottomRight
Private mclsFRMResizer As cFRMResizer
'

Private Sub Form_Load()
'===============================================================================
  '!! THE FOLLOW LINES ARE FOR THIS EXAMPLE
  '-- Setup FRM
  With Me
    .BorderStyle = 2: .Width = 4755: .Height = 3600
  End With
  
  '-- Setup CMDs
  With Me.cmdMatchWidth
    .Left = 60: .Top = 60: .Width = 4515: .Height = 495
  End With
  With Me.cmdMatchHeight
    .Left = 60: .Top = 600: .Width = 1455: .Height = 2535
  End With
  With Me.cmdStayBottomRight
    .Left = 3120: .Top = 2640: .Width = 1455: .Height = 495
  End With
  
  '-- Initialize and setup 'CFRMResizer'
  Set mclsFRMResizer = New cFRMResizer
  Call mclsFRMResizer.Setup(Me)
  With mclsFRMResizer
    ' ex.: .AddCTL [enuFRMResizeType_X], [enuFRMResizeType_Y}
    .AddCTL Me.cmdMatchWidth, ertGrow
    .AddCTL Me.cmdMatchHeight, , ertGrow
    .AddCTL Me.cmdStayBottomRight, ertMove, ertMove
  End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
'===============================================================================
  Set mclsFRMResizer = Nothing
End Sub


'!! PLACE THE FOLLOWING IN A MODULE
Option Explicit

'-- The following is used in 'CFRMResizer'
Public Enum enuFRMResizeTypes
  ertGrow = 1 '-- CTL's height/width should increase
  ertMove     '-- CTL's top/left should increase
End Enum


'!! PLACE THE FOLLOWING IN A CLASS MODULE WITH THE NAME 'cFRMResizer'
Option Explicit

Private Type typResizeCTL
  ctl                 As Control
  
  '-- Defines the X and Y behavior at resize time
  '   (height/top or width/left increased)
  enuFRMResizeType_X  As enuFRMResizeTypes
  enuFRMResizeType_Y  As enuFRMResizeTypes
  
  '-- Used internally for determining new height/top or width/left
  lngOrigCTL_X        As Long
  lngOrigCTL_Y        As Long
End Type

Private matypResizeCTLs()   As typResizeCTL
Private mintUBound          As Integer

Private mlngOrigFRMHeight   As Long
Private mlngOrigFRMWidth    As Long

Private WithEvents mfrm     As Form
'

Private Sub Class_Initialize()
'===============================================================================
  ReDim matypResizeCTLs(0)
End Sub

Private Sub Class_Terminate()
'===============================================================================
  Erase matypResizeCTLs()
  Set mfrm = Nothing
End Sub

Public Sub Setup(frm As Form)
'===============================================================================
  Set mfrm = frm
  
  '-- Store original FRM height and width
  With mfrm
    mlngOrigFRMHeight = .Height
    mlngOrigFRMWidth = .Width
  End With
End Sub

Public Sub AddCTL(ctl As Control, Optional enuFRMResizeType_X As enuFRMResizeTypes _
    , Optional enuFRMResizeType_Y As enuFRMResizeTypes)
'===============================================================================
  '-- If there aren't any elements
  If Not (mintUBound = 0 And matypResizeCTLs(0).ctl Is Nothing) Then
    '-- Increase array
    mintUBound = mintUBound + 1
    ReDim Preserve matypResizeCTLs(mintUBound)
  End If

  With matypResizeCTLs(mintUBound)
    Set .ctl = ctl
    
    '-- Store "X" resize type and determine which "X" value to store in lngOrigCTL_X
    .enuFRMResizeType_X = enuFRMResizeType_X
    Select Case enuFRMResizeType_X
      Case ertGrow: .lngOrigCTL_X = ctl.Width
      Case ertMove: .lngOrigCTL_X = ctl.Left
    End Select
    
    '-- Store "Y" resize type and determine which "Y" value to store in lngOrigCTL_Y
    .enuFRMResizeType_Y = enuFRMResizeType_Y
    Select Case enuFRMResizeType_Y
      Case ertGrow: .lngOrigCTL_Y = ctl.Height
      Case ertMove: .lngOrigCTL_Y = ctl.Top
    End Select
  End With
End Sub

Private Sub mfrm_Resize()
'===============================================================================
  Dim lngCounter          As Long
  Dim lngFRMHeight        As Long
  Dim lngFRMWidth         As Long
  Dim lngNewValue         As Long
  
  '-- Make sure height and width are not less than the original
  With mfrm
    If .Height < mlngOrigFRMHeight Then .Height = mlngOrigFRMHeight
    If .Width < mlngOrigFRMWidth Then .Width = mlngOrigFRMWidth
    
    lngFRMHeight = .Height
    lngFRMWidth = .Width
  End With
  
  For lngCounter = 0 To mintUBound
    With matypResizeCTLs(lngCounter)
      '-- If a resize type was saved for this CTL's "X"
      If .enuFRMResizeType_X > 0 Then
        '-- Get new value from mlngOrigFRMWidth and CTL's lngOrigCTL_X
        lngNewValue = lngFRMWidth - (mlngOrigFRMWidth - .lngOrigCTL_X)
        Select Case .enuFRMResizeType_X
          Case ertGrow: .ctl.Width = lngNewValue
          Case ertMove: .ctl.Left = lngNewValue
        End Select
      End If
      
      '-- If a resize type was saved for this CTL's "Y"
      If .enuFRMResizeType_Y > 0 Then
        '-- Get new value from mlngOrigFRMHeight and CTL's .lngOrigCTL_Y
        lngNewValue = lngFRMHeight - (mlngOrigFRMHeight - .lngOrigCTL_Y)
        Select Case .enuFRMResizeType_Y
          Case ertGrow: .ctl.Height = lngNewValue
          Case ertMove: .ctl.Top = lngNewValue
        End Select
      End If
    End With
  Next lngCounter
End Sub
 
maybe this is a bit off the thread but when my form window opens, it opens to its maximum size (takes up the entire screen). it was not designed to be viewed with that in mind, more like 3.25" wide by 2.5" high; admittedly, when you press the min button it shrinks to the intended dimensions, but is there no way to default those specifications from the git go?

“The philosophy of the school room in one generation will be the philosophy of government in the next.&quot; --- Abraham Lincoln
 
What's your WindowState property in design time? It might be "2 - Maximized".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top