Results 1 to 9 of 9

Thread: form resize problem/question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I have a form with 4 buttons at the bottom. 3 buttons are aligned to the right of the form and the other button is aligned to the left of the form.

    I was able to figure out how to write the appropriate code to move the buttons if the user resizes the form.. That works fine.. But what I don't know how to do is to handle the situation when the user resizes the form small enough to where the 3 right buttons run into the button on the left. What I have seen other programs do in this situation is the buttons will actually rearrange themselves in a stack like formation to where all buttons are still visible..

    What code would I use to make sure the buttons to run into each other and if they do, how to rearrange them in a stack like formation? An alternative that I would accept would be to prevent the user from resizing the form too small.. Is that possible whiles still allowing the user to resize the form?

    Any suggestions would be appreciated..

    Dan

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Several answers

    1) You can use the Form_Resize event to trap whenever the user attempts to resize the form. If need be, you can override the new settings the user has chosen.

    2) There are controls already available that do this and many other form resizing functions for you. Personally I haven't used them so I can't say where they are

    3) You get free functionality if you drop your buttons onto a PictureBox which has the Align property set. When you resize the form, the pictureboxes automatically move/resize in relation to the form so alot of free functionality is gained in this way.

    Hopefully someone will have time to give you some concrete examples.

    Regards
    Paul Lewis

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    'use this it works great. Set your min size inside your app
    '
    Code:
    'this goes in a class module  NOT bas ( A CLASS MODULE)
    '
    Option Explicit
    
    ' Title:    Elastic Forms
    ' Author:   Leigh Bowers
    ' Email:    [email protected]
    ' WWW:      http://www.esheep.freeserve.co.uk/compulsion
    ' Version:  1.01
    ' Date:     19th June 1999
    ' Requires: N/A
    ' License:  Freely Distributable (non-commercial use)
    
    Private fForm As Form
    
    Private lOriginalWidth As Long
    Private lOriginalHeight As Long
    
    Private lMinWidth As Long
    Private lMinHeight As Long
    
    Private Type udtControl
        lLeft As Long
        lTop As Long
        lWidth As Long
        lHeight As Long
    End Type
    Private aControls() As udtControl
    
    Public Property Let Form(ByVal fPassForm As Form)
        
    Dim iCount As Integer
    Dim cControl As Control
    
        Set fForm = fPassForm
        
        ' Store form's original Width & Height
        
        lOriginalWidth = fForm.Width
        lOriginalHeight = fForm.Height
    
        ' Use error trapping to ignore components that don't
        ' support certain properties being read at run-time
    
        On Error Resume Next
    
        ' Store the form's component's properties
    
        iCount = 0
        ReDim aControls(fForm.Controls.Count)
    
        For Each cControl In fForm.Controls
            iCount = iCount + 1
            With aControls(iCount)
                If TypeOf cControl Is Line Then
                    .lLeft = cControl.X1
                    .lTop = cControl.Y1
                    .lWidth = cControl.X2
                    .lHeight = cControl.Y2
                Else
                    .lLeft = cControl.Left
                    .lTop = cControl.Top
                    .lWidth = cControl.Width
                    .lHeight = cControl.Height
                End If
            End With
        Next ' Each
    
    End Property
    
    Public Sub FormResize()
    
        ' v1.01 (19/06/1999)
        '
        ' bDisableResize:
        ' Used to avoid unnecessary *recursive* resizing
        '
        ' lPreviousWidth/Height:
        ' Used to avoid unnecessary resizing
    
        ' Resize the form's controls
    
    Dim iCount As Integer
    Dim cControl As Control
    Dim iTaskBarHeight As Integer
    Dim sOriginalWidthUnit As Single
    Dim sOriginalHeightUnit As Single
    Static bDisableResize As Boolean
    Static lPreviousWidth As Long
    Static lPreviousHeight As Long
    
        If fForm Is Nothing Or bDisableResize Then Exit Sub
    
        ' Don't process minimized forms
        
        If fForm.WindowState = vbMinimized Then Exit Sub
    
        ' Check form size against minimums
        
        bDisableResize = True
        If fForm.Width < lMinWidth Then fForm.Width = lMinWidth
        If fForm.Height < lMinHeight Then fForm.Height = lMinHeight
        bDisableResize = False
        
        ' Ensure form size has changed
        
        If lPreviousWidth = fForm.Width And lPreviousHeight = fForm.Height Then Exit Sub
        lPreviousWidth = fForm.Width
        lPreviousHeight = fForm.Height
    
        ' Perform calculations in advance (speed increase)
    
        iTaskBarHeight = 28 * Screen.TwipsPerPixelY ' Standard height
        sOriginalWidthUnit = lOriginalWidth / fForm.Width
        sOriginalHeightUnit = (lOriginalHeight - iTaskBarHeight) / (fForm.Height - iTaskBarHeight)
    
        ' Use error trapping to ignore components that don't
        ' support certain properties being set at run-time
    
        On Error Resume Next
    
        ' Do the resize...
        
        iCount = 0
    
        For Each cControl In fForm.Controls
            iCount = iCount + 1
            With cControl
                If TypeOf cControl Is Line Then
                    .X1 = Int(aControls(iCount).lLeft / sOriginalWidthUnit)
                    .Y1 = Int(aControls(iCount).lTop / sOriginalHeightUnit)
                    .X2 = Int(aControls(iCount).lWidth / sOriginalWidthUnit)
                    .Y2 = Int(aControls(iCount).lHeight / sOriginalHeightUnit)
                Else
                    .Left = Int(aControls(iCount).lLeft / sOriginalWidthUnit)
                    .Top = Int(aControls(iCount).lTop / sOriginalHeightUnit)
                    .Width = Int(aControls(iCount).lWidth / sOriginalWidthUnit)
                    .Height = Int(aControls(iCount).lHeight / sOriginalHeightUnit)
                End If
            End With
        Next ' Each
    
    End Sub
    
    
    Private Sub Class_Terminate()
    
        Set fForm = Nothing
    
    End Sub
    
    
    
    
    Public Property Let MinWidth(ByVal lPassMinWidth As Long)
    
        lMinWidth = lPassMinWidth
    
    End Property
    Public Property Let MinHeight(ByVal lPassMinHeight As Long)
    
        lMinHeight = lPassMinHeight
    
    End Property
    
    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
    'this if your form code for every form in project
    
    Option Explicit
    
    Private clsElastic As clsElasticForms
    
    Private Sub Form_Load()
    
        Set clsElastic = New clsElasticForms
        clsElastic.Form = Me
        clsElastic.MinHeight = 4000
        clsElastic.MinWidth = 4000
    
    End Sub
    
    
    Private Sub Form_Resize()
    
        clsElastic.FormResize
    
    End Sub
    
    
    Private Sub Form_Unload(Cancel As Integer)
    
        Set clsElastic = Nothing
    
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    You can keep size above minimum.

    You can prevent User from making form less than some minimum values, but be careful. Your Resize Sub will be called recursively.
    Code:
    Private Sub Form_resize()
    If frmMyform.Width < MinWidth then
         frmMyform.Width = MinWidth
         Exit Sub
      End If
    If frmMyform.Height < MinHeight then
         frmMyform.Height = MinHeight
         Exit Sub
      End If
    'Put other From-Sizing code here.
    End Sub
    I am not sure what happens to control flow for the above, but it will be okay. I think you get jerked out and put right back into the Sub as soon as you change its size. Then after your "Other Form-Sizing Code" executes, control flow will Exit & reenter just before the "Exit Sub", causing final exit. If both width & Height get changed, you probably exit and reenter an extra time.

    I am not certain that the "Exit Sub" statements are required, but it worked for me. Perhaps without them you would repeat your Form-Sizing code unnecessarily.

    At any rate, when you change form size in the Resize Sub, think about it carefully: You could create an infinite loop.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  5. #5
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    I've used this simple function to prevent resizing or allowing resizing. Depends what you want to do.
    Code:
    'in the declaration part of a form
    Dim lngHeight, lngWidth As Long
    'After the declarations
    Private Sub Form_Resize()
        Resizer lngHeight, lngWidth 'Call the resize function
    End Sub
    
    Private Function Resizer(ByVal lngHi As Long, ByVal lngWid As Long)
    If WindowState < 1 Or WindowState > 2 Then  'VB will call the resize event when minimizing
                                                'or Maximizing; you cannot resize a form in these
                                                'states, so skip
        Form1.Height = lngHi
        Form1.Width = lngWid
    End If
    You set the variables to whatever you want. Can prevent resizing, or you can add
    more code to adjust the variables
    to do anything you want. Has no recursive side-effects (that I know of!)
    Donald Sy - VB (ab)user

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Thanks for all the help.. I think I have decided to just prevent the user from shrinking the form passed a certain size.. I tried the following suggestions but had problems with each:

    Code from dsy5: Did not allow resizing at all.. That's not want I'm trying to accomplish.. What I'm trying to do is to allow a sizeable form but not allow resizing smaller than say 500 x 400.

    Code from Guv: Does not seem to do anything..

    Code from HeSaidJoe: Did not even try.. Looked way to complicated..

    Help from PaulLewis: How would I go about implementing your suggestion #1? That sounds like what I'm trying to do: prevent user from shrinking form past a certain size, but allowing to resize anything above that size...

    Thanks again guys for your feedback..

    Dan

  7. #7
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    dbassettt74
    The code will not allow resizing unless you want it to! You can set the parameters with code.
    Donald Sy - VB (ab)user

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    It's not complicated at all.
    All you do is open a Class Module and paste the class module into it and save it.
    Then in your project paste the form code and add the
    module.
    It works like a charm.
    Email me and I'll email you a sample project or email me your project and I'll add the resizing to it for you and send it back.

    Don't be afraid of things...everything is hard at first.

    Wayne
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Guv's example does it

    Dan, I think Guv's example is the simplest form of what my suggestion #1 was all about.

    HeSaidJoe's example looks more comprehensive as far as moving/resizing controls go, and it would make a good lesson if you were interested in learning about classes, user defined types, arrays, controls ...umm yeah it is quite complicated isn't it. Still, nothing comes without paying for it, and on here, the price you pay is measured in time and patience.

    Regards
    Paul Lewis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width