# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Resizing form and textboxes ...

## nottheboss

Can someone tell me how to get the text box to resize along with the form?  I also have 2 command buttons under the text box which I want to stay centered and at the bottom of the form when I resize it.  

Right now, the textbox stays the same size when I resize the Main Form and the command buttons stay in the same place directly under the textbox.

I attached the form here.

Thanks,

Eric

----------


## MartinLiss

```
Option Explicit

Private Type CtrlProportions
    HeightProportions As Single
    WidthProportions As Single
    TopProportions As Single
    LeftProportions As Single
End Type

Dim ProportionsArray() As CtrlProportions

Sub InitResizeArray()

    Dim I As Integer
    
    On Error Resume Next
    
    ReDim ProportionsArray(0 To Controls.Count - 1)
    
    For I = 0 To Controls.Count - 1
        With ProportionsArray(I)
            .HeightProportions = Controls(I).Height / ScaleHeight
            .WidthProportions = Controls(I).Width / ScaleWidth
            .TopProportions = Controls(I).Top / ScaleHeight
            .LeftProportions = Controls(I).Left / ScaleWidth
        End With
    Next I
    
End Sub

Sub ResizeControls()

    On Error Resume Next
    
    Dim I As Integer
    
    For I = 0 To Controls.Count - 1
        With ProportionsArray(I)
            ' move and resize controls
            Controls(I).Move .LeftProportions * ScaleWidth, _
            .TopProportions * ScaleHeight, _
            .WidthProportions * ScaleWidth, _
            .HeightProportions * ScaleHeight
        End With
    Next I
    
End Sub

'Form initialize event
Private Sub Form_Initialize()

    InitResizeArray
    
End Sub

'Form resize event
Sub Form_Resize()

    ResizeControls
    
End Sub
```

----------


## PWNettle

If you want controls on your form to resize along with the form you'll need to add some code to accomplish this in the Form_Resize event.  In your case you could determine the size of the textboxes in proportion to the dimensions of the form and center the command buttons based on their width(s) and the width of the form.

Good luck,
Paul

----------


## nottheboss

Thanks Martin....  

You certainly could not have made that any easier.   At first I thought .. ok.. How will I work this in to my program?  

Then, after reading it, I realized that i just had to paste it in!

Wow, that made my life easy.

Again,

Thank you very much.

 :Smilie:   :Smilie:   :Smilie:   :Smilie:   :Smilie:   :Smilie:   :Smilie:

----------


## MartinLiss

You're welcome. You may find one problem and that is that comboboxes of style 2 don't resize properly.

----------


## MartinLiss

I'd like to take credit for the code, but I can't. All I did was to correct a couple of mistakes in a piece of code I found in this forum a couple of years ago. Here is the originallink

----------


## Hack

You could have kept that a secret Martin.   I wouldn't have told anyone!  :Big Grin:

----------


## MarkT

Martin

I'm looking at your code here and it appears that the problem you are having with the combo boxes not resizing is quite simple. The height property of a combo box is read only. If you try to use the move method on a combo box it will cause an error. Since you have resume next, it will just skip that control. Something like this might be better.

VB Code:
Public Sub ResizeControls()
    Dim I As Integer
    
    On Error Resume Next
     For I = 0 To Controls.Count - 1
        ' move and resize controls
        Controls(I).Left = ProportionsArray(I).LeftProportions * Me.ScaleWidth
        Controls(I).Top = ProportionsArray(I).TopProportions * Me.ScaleHeight
        Controls(I).Width = ProportionsArray(I).WidthProportions * Me.ScaleWidth
        Controls(I).Height = ProportionsArray(I).HeightProportions * Me.ScaleHeight
    Next I    
End Sub

----------


## MartinLiss

Thanks for the reply, but I knew the reason for the problem and I was just alerting people to it. My On Error Resume Next code, while avoiding an error when the combobox is encountered was actually put there for menu items etc. that don't have a Move method. BTW, what do you see as the advantage of your code over mine?

----------


## MarkT

> BTW, what do you see as the advantage of your code over mine?


The only real advantage is it will reposition combo boxes instead of leaving them in the same place.

----------


## Achichincle

That code works great Martin!

I was able to put it to use in one of my projects and added a couple of simple mods to it that I thought others might find use for.

I wrapped it up into a class and added another method to be able to specify whether or not you want a particular control (or controls) to move or resize.

Hope this is of use to someone...

Here's the class module code:

VB Code:
Option Explicit
 Private Type CtrlProportions
    Name As String
    Move As Boolean
    Resize As Boolean
    HeightProportions As Single
    WidthProportions As Single
    TopProportions As Single
    LeftProportions As Single
End Type
 Private mProportionsArray() As CtrlProportions
 Public Sub InitResizeArray(objForm As Form)
     Dim I As Integer
    
    On Error Resume Next
    
    ReDim mProportionsArray(0 To objForm.Controls.Count - 1)
    
    For I = 0 To objForm.Controls.Count - 1
        With mProportionsArray(I)
            .Name = objForm.Controls(I).Name
            .Move = True
            .Resize = True
            .HeightProportions = objForm.Controls(I).Height / objForm.ScaleHeight
            .WidthProportions = objForm.Controls(I).Width / objForm.ScaleWidth
            .TopProportions = objForm.Controls(I).Top / objForm.ScaleHeight
            .LeftProportions = objForm.Controls(I).Left / objForm.ScaleWidth
        End With
    Next I
    
End Sub
 Public Sub ResizeControls(objForm As Form)
     On Error Resume Next
    
    Dim I As Integer
    
    For I = 0 To objForm.Controls.Count - 1
        With mProportionsArray(I)
            ' move and resize objcontrols
            If .Move Then
                objForm.Controls(I).Left = .LeftProportions * objForm.ScaleWidth
                objForm.Controls(I).Top = .TopProportions * objForm.ScaleHeight
            End If
            If .Resize Then
                objForm.Controls(I).Width = .WidthProportions * objForm.ScaleWidth
                objForm.Controls(I).Height = .HeightProportions * objForm.ScaleHeight
            End If
        End With
    Next I
    
End Sub
 Public Sub SetControlProperties(strName As String, flgMove As Boolean, flgResize As Boolean)
     Dim I As Integer
    
    For I = 0 To UBound(mProportionsArray())
        If mProportionsArray(I).Name = strName Then
            mProportionsArray(I).Move = flgMove
            mProportionsArray(I).Resize = flgResize
        End If
    Next I
            
End Sub

To use it inside a form:

VB Code:
Option Explicit
 Private mobjResizer As Resizer
 Private Sub Form_Resize()
     mobjResizer.ResizeControls Me
 End Sub
 Private Sub Form_Load()
     Set mobjResizer = New Resizer
    
    mobjResizer.InitResizeArray Me
     ' move but don't resize the Ok button
    mobjResizer.SetControlProperties "cmdOk", True, False
     ' resize but don't move the Close button
    mobjResizer.SetControlProperties "cmdClose", False, True
 End Sub

----------


## MartinLiss

I've recently added the following modification to that code that adjusts the font size within the controls.

VB Code:
[b]Dim dblFontSize As Double[/b]
    
    ' 8310 is the default ScaleHeight of my form
    dblFontSize = 8.5 * (frmMain.ScaleHeight / 8310)
     For intIndex = 0 To frmMain.Controls.Count - 1
        With typCtrlProportions(intIndex)
            ' move and resize frmmain.Controls
            [b]frmMain.Controls(intIndex).FontSize = dblFontSize[/b]
            Select Case True
 ' etc.

----------


## BruceG

I just found this while doing a search; works great! Bravo, Martin.

----------


## MikeGarvin

Also hiting the maximise button does not resize the form..

----------


## MartinLiss

Mike, please don't post your responses in this thread because people won't know what you are referring to. I created a new Resizing Controls thread with your other comments.

----------


## MikeGarvin

Ok, will do. The thing is I use dozens of controls and potentially two or three hundred on the same form depending how the user configures his form. I realise this is puting a great burden on the system.
Would you have a suggestion as to a why to the SSTab issue. I will adapt your code and see what comes out. Thanks.

----------


## MikeGarvin

Yes the form does get maximised. Sorry my mistake.

----------


## MikeGarvin

Please Martin, if you have a second can you look at the screenshots for my app using your resize code. As you can see, what i did was to maximise then minimise the window. We see that on tab one all three lines of controls appear correctly but then on tab 3 the same three lines appear when they really belong to tab 3. also on the last shot the frames are all wrong. Any ideas as to why this is and how to remedy it? Thanks again.

----------


## MartinLiss

Gotta go. I'll take a look later today.

----------


## MikeGarvin

Ok. Thanks. I got the tab #'s wrong, it should read:

We see that on tab one all three lines of controls appear correctly but then on tab 3 the same three lines appear when they really belong to tab 1.

----------


## VB_Captain

Well done all,
specail thanks goes to Martinliss and MarkTMarkT  and   Achichincle  .

Only mixture of all of these gives a neat solution to the problem.

----------


## MartinLiss

> _Originally posted by MikeGarvin_ 
> *Ok. Thanks. I got the tab #'s wrong, it should read:
> 
> We see that on tab one all three lines of controls appear correctly but then on tab 3 the same three lines appear when they really belong to tab 1.*


 How did you create those controls that you want to be on tab 3? Did you draw (or copy/paste) them on that tab, or did you just double-click the control in the toolbar? The latter doesn't work properly for a container control like the tabbed dialog. To see if that is your problem do something like this:

MsgBox MyControlThatsOnATab.Container.Tab

and see what tab it's actually on. If that shows you have a problem then do the following if you want the control on tab 3.

MyControlThatsOnATab.Container.Tab = 3

----------


## MikeGarvin

Thanks Martin. The controls are created at run time from the first line, which is there at design time. I simply create new lines of controls from what the user chooses.

I will try what you suggest. Thanks.

Would you know why the frames don't appear on the other sstabs. I believe they are there but are offset to the left completely as explained in my initial post. Cheers.

----------


## MikeGarvin

http://www.vbforums.com/showthread.p...ppear+controls

Have found a link with exactly the problem I have. Cheers. Mike.

----------


## MartinLiss

Does that resolve your problem?

----------


## MikeGarvin

The flickering was not very nice before but now with 6*24 frames on my app it's just bad . I find it incredible the left property on tabs should not just stay to a the value you assign it at design time. VB, eh, some language!

Would you have a better way?

----------


## jhermiz

> _Originally posted by MartinLiss_ 
> *Does that resolve your problem?*


Sorry to re-open this thread...but whats the final code here?

Should this just be added to a public module and called in any resize event of any form?

Thanks,
Jon

----------


## jhermiz

> _Originally posted by MartinLiss_ 
> *Does that resolve your problem?*


Sorry to re-open this thread...but whats the final code here?

Should this just be added to a public module and called in any resize event of any form?  Also I read in someone's response that this does not work when someone hits the maximize button.  Is this true?

Thanks,
Jon

----------


## MartinLiss

Yes that all you need to do and if you call it from the form's resize event it will work fine when you maximize the form. In my app I found that I need to do the following when the form is _minimized_ however.


VB Code:
On Error Resume Next ' Needed if the form is minimized
    If Me.Width < 900 Then
        Me.Width = 900
    End If
    If Me.Height < 900 Then
        Me.Height = 900
    End If
    
    ResizeControls

----------


## jhermiz

Hi Martin,

Sorry to be a bother...but in various places you have added code.

Anyway you can lump it all togethr with an end result of all the code in this LAST post  :Smilie: 

Thanks,
Jon

----------


## jhermiz

Is it me..or does this code totally not work for tabbed pane layout ?  The controls on all other tabs disappear.  The flex grids disappear.  The combo boxes dont resize.

I dont think im going to use this.

Jon

----------


## MartinLiss

Comboboxes do resize, there's a post in the thread that talks about it. Unfortunately I think I remember someone else saying that Tabs don't work right, sorry about that.

----------


## jhermiz

Figured that this was too good to be true  :Frown:  

Anyone know of any 3rd party software to help me re-scale everything?  Including tabs, combo boxes, and Grids?

Jon

----------


## DaveBo

Here's why controls on tab panes are so flukey.

From: Microsoft Knowledge Base Article - 187562
"HOWTO: Resize the Controls in SSTab When Form is Resized"

When a form is resized, the controls present in the form can be resized to fit into the form by using the Move method. However, it is not straightforward to resize the controls present inside an SSTab control because the Left property values of controls present in the inactive tabs are less than 0. This article describes a way to resize the controls present in an SSTab control.

The SSTab control hides the controls present in the inactive tabs by setting their Left property values less by 75000. Hence, changing the Left property of a control on an inactive tab in the form's resize event may cause that control to stop showing in a particular tab.
=======================================

I made some Rube Goldberg changes to Achichincle's class above, see "SSTab" in code:


VB Code:
'To use, put the dozen or so lines below inside your form:
'
Private mobjResizer As Resizer
 Private Sub Form_Resize()
    mobjResizer.ResizeControls Me
End Sub
 Private Sub Form_Load()
     Set mobjResizer = New Resizer
    mobjResizer.InitResizeArray Me
    ' Optional: move but don't resize the Ok button
    mobjResizer.SetControlProperties "cmdOk", True, False
    ' Optional: resize but don't move the Close button
    mobjResizer.SetControlProperties "cmdClose", False, True
End Sub
 '===========================================================
  ' Put everything below into a class module named "Resizer"
Private Type CtrlProportions
    Name As String
    Move As Boolean
    Resize As Boolean
    HeightProportions As Single
    WidthProportions As Single
    TopProportions As Single
    LeftProportions As Single
End Type
 Private mProportionsArray() As CtrlProportions
  Public Sub InitResizeArray(objForm As Form)
     Dim I As Integer
    Dim ObjLeft As Long
    Dim ObjParentTyp As String
    Dim LeftOffset As Long
     On Error Resume Next
    
    ReDim mProportionsArray(0 To objForm.Controls.Count - 1)
    
    For I = 0 To objForm.Controls.Count - 1
        With mProportionsArray(I)
            .Name = objForm.Controls(I).Name
            .Move = True
            .Resize = True
             .HeightProportions = objForm.Controls(I).Height / objForm.ScaleHeight
            .WidthProportions = objForm.Controls(I).Width / objForm.ScaleWidth
            .TopProportions = objForm.Controls(I).Top / objForm.ScaleHeight
             ' Have to be a little careful with .Left here. Controls on an SSTab
            ' control have -75000 added to their .Left prop to make them invisible.
            LeftOffset = 0
            ObjLeft = objForm.Controls(I).Left
            ObjParentTyp = TypeName(objForm.Controls(I).Container)
            If ObjParentTyp = "SSTab" And ObjLeft < 0 Then LeftOffset = 75000
            .LeftProportions = (ObjLeft + LeftOffset) / objForm.ScaleWidth
        End With
    Next I
 End Sub
  Public Sub ResizeControls(objForm As Form)
     On Error Resume Next
    
    Dim I As Integer
    Dim ObjParentTyp As String
    Dim LeftOffset As Long
     For I = 0 To objForm.Controls.Count - 1
        With mProportionsArray(I)
            ' move and resize objcontrols
            If .Move Then
                LeftOffset = 0
                ObjParentTyp = TypeName(objForm.Controls(I).Container)
                If ObjParentTyp = "SSTab" And objForm.Controls(I).Left < 0 Then LeftOffset = 75000
                 objForm.Controls(I).Left = .LeftProportions * objForm.ScaleWidth - LeftOffset
                objForm.Controls(I).Top = .TopProportions * objForm.ScaleHeight
            End If
            If .Resize Then
                objForm.Controls(I).Width = .WidthProportions * objForm.ScaleWidth
                objForm.Controls(I).Height = .HeightProportions * objForm.ScaleHeight
            End If
        End With
    Next I
    
End Sub
  Public Sub SetControlProperties(strName As String, flgMove As Boolean, flgResize As Boolean)
     Dim I As Integer
    
    For I = 0 To UBound(mProportionsArray())
        If mProportionsArray(I).Name = strName Then
            mProportionsArray(I).Move = flgMove
            mProportionsArray(I).Resize = flgResize
        End If
    Next I
            
End Sub

----------


## DaveBo

I think Achichincle's class scheme has the advantage of working with multiple forms, i.e. they'll each have their own instance and won't interfere.

I was wondering how this would handle dynamically created controls at run-time ...
By just adding a call to

VB Code:
mobjResizer.InitResizeArray Me
after creating them it all seems to work beautifully.

----------


## jhermiz

Even for combo boxes?

Jon

----------


## veryjonny

Would someone *pls* post which of the codes is the *final* one.

Thank you.

----------


## Cimperiali

in resize event of a form:

VB Code:
On Error Resume Next ' Needed if the form is minimized'  
  ' for the minimizing: better code:
   if me.windowstate<>vbMinimized then 
      'as you do not need to size non visible controls....
   
      If Me.Width < 900 Then
         Me.Width = 900
         Exit Sub  '<---Do not forget this, or you will execute the
                       'remaining code more times than needed
      End If
      If Me.Height < 900 Then
         Me.Height  = 900
         Exit Sub  '<---Do not forget this, or you will execute the
                       'remaining code more times than needed
       End If
    
      ResizeControls
    End If

----------


## jhermiz

Marty,

I noticed y ou made some changes including font size.
Can you post the entire code...so I get a clean copy.

Thanks,
Jon

----------


## MartinLiss

Sorry, all I have is what is in the thread.

----------


## ahara

Attached is a complete version (for my purposes anyway) after playing around with everyone's code submitted in this thread.  Most of it is actually DaveBo's last class submitted with Marty's fontsize bit added to it.  I just added an optional argument to the resizing method that represents the default ScaleWidth of the form.  Pass this, and the fonts resize as well.  Combos and SSTabs are good too from the testing I did. cheers :wave:

----------


## jhermiz

> _Originally posted by ahara_ 
> *Attached is a complete version (for my purposes anyway) after playing around with everyone's code submitted in this thread.  Most of it is actually DaveBo's last class submitted with Marty's fontsize bit added to it.  I just added an optional argument to the resizing method that represents the default ScaleWidth of the form.  Pass this, and the fonts resize as well.  Combos and SSTabs are good too from the testing I did. cheers*


Hmm I was not gonna use the class...I just wanted the methods though.  You guys got it working for SSTabs and combos too ????

Should I just use the class...I did not want to but if there isn't just the methods I guess I'll have to use the class?

Marty ?

Jon

----------


## ahara

as a test, I just copied the methods into a form and simply called them - still works.  I only wanted it wrapped up in a class because I plan on reusing this frequently.  And yes it does work for combos and SSTabs (it won't change the height of the combo, but it adjusts the width)

----------


## jhermiz

> _Originally posted by ahara_ 
> *as a test, I just copied the methods into a form and simply called them - still works.  I only wanted it wrapped up in a class because I plan on reusing this frequently.  And yes it does work for combos and SSTabs (it won't change the height of the combo, but it adjusts the width)*


ya i was playing with it.
seems to have done the trick..this thread got better  :Smilie:  :Alien Frog:

----------


## jhermiz

I've noticed when using this code with the SmartMenu control www.vbsmart.com.  That it fails...well it works but it hides the smartmenu...just an FYI.

Jon

----------


## mike2

> _Originally posted by ahara_ 
> *Attached is a complete version (for my purposes anyway) after playing around with everyone's code submitted in this thread.  Most of it is actually DaveBo's last class submitted with Marty's fontsize bit added to it.  I just added an optional argument to the resizing method that represents the default ScaleWidth of the form.  Pass this, and the fonts resize as well.  Combos and SSTabs are good too from the testing I did. cheers*


Ummm...
Sorry for doing like a n00b, but how do i use that? :Confused:

----------


## ahara

slap this into any form that has controls (after importing the class into the project)


VB Code:
Private mobjResizer As FormResizer
 Private Sub Form_Resize()
    Dim defScaleWidth As Integer
    defScaleWidth = Me.ScaleWidth
    mobjResizer.ResizeControls Me, defScaleWidth
 End Sub
 Private Sub Form_Load()
     Set mobjResizer = New FormResizer
    mobjResizer.InitResizeArray Me
    
    
End Sub

cheers

----------


## Jon_G

this seems to make my text smaller when form is max and text bigger when form gets smaller.... ?? how come?

----------


## dy1jaw

I've tried using the class and incorporating it into my program but i kept on getting this error message.

____________________________
-Compile error:                        -
-User-defined type not defined.  -
_____________________________

what should i do?

_________________________________
Public Sub InitResizeArray(objForm As Form)

    Dim I As Integer
    Dim LeftOffset As Long

    On Error Resume Next

    ReDim mProportionsArray(0 To objForm.Controls.Count - 1)

    For I = 0 To objForm.Controls.Count - 1
        With mProportionsArray(I)
            .Name = objForm.Controls(I).Name
            .Move = True
            .Resize = True

            .HeightProportions = objForm.Controls(I).Height / objForm.ScaleHeight
            .WidthProportions = objForm.Controls(I).Width / objForm.ScaleWidth
            .TopProportions = objForm.Controls(I).Top / objForm.ScaleHeight

            ' Have to be a little careful with .Left here. Controls on an SSTab
            ' control have -75000 added to their .Left prop to make them invisible.
            LeftOffset = 0
*If TypeOf objForm.Controls(I).Container Is SSTab _
                And objForm.Controls(I).Left < 0 Then LeftOffset = 75000*            .LeftProportions = (objForm.Controls(I).Left + LeftOffset) / objForm.ScaleWidth
        End With
    Next I

End Sub________________________________________

----------


## Merri

You don't have SSTab control included into your project, thus you get the error. I guess you could remove that line without having any trouble.

----------


## dy1jaw

i'm sorry but what is SSTab?

thanks

----------


## MartinLiss

> i'm sorry but what is SSTab?
> 
> thanks


It is the _Microsoft Tabbed Dialog Control 6.0_ found under Project|Components.

----------


## shragel

Or just add this control to your form.

----------


## Mark Gambo

> Or just add this control to your form.



I would try your control but unfortunately I don't run complied projects without looking at the code first. Sorry  :Frown:

----------


## shragel

So dont use it

----------


## Mark Gambo

> So dont use it



There are other alternatives

----------


## shragel

The problem with the alternative is it doesn't handle combo boxes, and the sstab control.

----------


## Mark Gambo

> The problem with the alternative is it doesn't handle combo boxes, and the sstab control.



Maybe you should take a look at this post

Here is the link to the entire post

----------


## MarkT

Attached is an OCX (source included) I made a while back. I haven't tested it with an SS tab but aside from that, it seems to have work in every other situation I have tried. Just compile it and drop it on a form. It will do the rest.

----------


## Hack

This one I will take a look at as it includes source.   Thanks MarkT   :Smilie:

----------


## sanjaylimbikai

Just for an idea, it is wiser to make a form scrollable rather than making controls tiny when you resize a form a bit small after a certain level of scale. It may also be a challenge, I use scrollable forms in my projects.

----------


## lilwupster

i know this is an older thread but why won't this code work for images?

----------


## lilwupster

o, i see, i need the stretch property of the image set to true

----------


## wai1985

Dont know if anybody still replies to this post. First of all, I want the form to expand maximum when the form first loads. That possible? How?

Second, are there any explanation to what the code means? I dont understand how it works at all. Here is the code, although I integrated some other code into it about the combo box problem:



```
Option Explicit


Private Type CtrlProportions
    HeightProportions As Single
    WidthProportions As Single
    TopProportions As Single
    LeftProportions As Single
End Type

Dim ProportionsArray() As CtrlProportions

Sub InitResizeArray()

    Dim I As Integer
    
    On Error Resume Next
    
    ReDim ProportionsArray(0 To Controls.Count - 1)
    
    For I = 0 To Controls.Count - 1
        With ProportionsArray(I)
            .HeightProportions = Controls(I).Height / ScaleHeight
            .WidthProportions = Controls(I).Width / ScaleWidth
            .TopProportions = Controls(I).Top / ScaleHeight
            .LeftProportions = Controls(I).Left / ScaleWidth
        End With
    Next I
    
End Sub
Public Sub ResizeControls()
Dim I As Integer
On Error Resume Next
For I = 0 To Controls.Count - 1
' move and resize controls
Controls(I).Left = ProportionsArray(I).LeftProportions * Me.ScaleWidth
Controls(I).Top = ProportionsArray(I).TopProportions * Me.ScaleHeight
Controls(I).Width = ProportionsArray(I).WidthProportions * Me.ScaleWidth
Controls(I).Height = ProportionsArray(I).HeightProportions * Me.ScaleHeight
Next I

End Sub
'Form initialize event
Private Sub Form_Initialize()

    InitResizeArray
    
End Sub

'Form resize event
Sub Form_Resize()

    ResizeControls
    End Sub
```

----------


## aikidokid

Does this ocx work with MDI forms and child forms?

If so a quick explanation would be great how to impliment this.  :Big Grin:

----------


## randem

Ok, I have been using MartinLiss's Form Resize code for a long time and lately have encountered a few issues that I have corrected. Most users may never see the issue I have encountered. These issues were due to subclassing controls on the form being resized. Here are the new code changes:

VB Code:
Option Explicit Private Type CtrlProportions    HeightProportions As Single    WidthProportions As Single    TopProportions As Single    LeftProportions As SingleEnd Type Dim ProportionsArray() As CtrlProportions ' 20080302RJ Dim ArrayInitialized As BooleanDim FormScaleMode As Integer  Private Sub mInitResizeArray(frm As Object)' Called from Form_Initialize Dim I As Integer        On Error Resume Next        ' 20080302RJ    ' The form's Load and Resize events will be invoked after execution of this statement        ReDim ProportionsArray(0 To frm.Controls.Count - 1)        ' We will return here after the form's Load and Resize events have been executed.    ' Since the Form's Resize event will call mResizeControls we had to take precautions in that    ' sub not to do anything till we have finished this routine. So if ArrayInitialized = false    ' the mResizeControls will just exit without attempting to do anything and we will return here.        ' Save the form's scale mode because if the app does subclassing the scale mode will change to pixels    ' then when we attempt to move it we will mess up the form if we don't change it back to the    ' original value that the proportion calculation were made with.        FormScaleMode = frm.ScaleMode        For I = 0 To frm.Controls.Count - 1            With ProportionsArray(I)            .HeightProportions = frm.Controls(I).Height / frm.ScaleHeight            .WidthProportions = frm.Controls(I).Width / frm.ScaleWidth            .TopProportions = frm.Controls(I).Top / frm.ScaleHeight            .LeftProportions = frm.Controls(I).Left / frm.ScaleWidth        End With            Next I        ArrayInitialized = True    End Sub Private Sub mResizeControls(frm As Object)Dim I As LongDim ctl As Control' Called from Form_Resize     ' 20080302RJ    ' Added to insure that we don't attempt to process the form before it is loaded.    ' When mInitResizeArray is called in the form's Initialize event the first time it accesses    ' any object/method of the form then Form's Load and Resize Event are invoked before we finish the    ' mInitResizeArray sub and we will get here before we have initialized the ProportionsArray array.    ' This causes weird effects on the calling form in certain instances because the array will be full    ' of zeros and all controls will be relocated to the top of the container they are in if we allow this    ' routine to operate.        If Not ArrayInitialized Then Exit Sub ' Return to mInitResizeArray sub        On Error Resume Next            ' Return the original scale mode of the form because this is the mode that was used    ' to calculate the proportions.        frm.ScaleMode = FormScaleMode        For I = LBound(ProportionsArray) To UBound(ProportionsArray) ' 20080302RJ'    For I = 0 To frm.Controls.Count - 1            Set ctl = frm.Controls(I)                ' 20060403RJ        ' if we don't want a control to be resized it's tag field will contain "NoResize"                If LCase(ctl.Tag) <> LCase("NoResize") Then             With ProportionsArray(I)                ' move and resize controls                ctl.Move CLng(.LeftProportions * frm.ScaleWidth), _                CLng(.TopProportions * frm.ScaleHeight), _                CLng(.WidthProportions * frm.ScaleWidth), _                CLng(.HeightProportions * frm.ScaleHeight)            End With         End If      Next I           Set ctl = Nothing     End Sub Public Sub InitResizeArray(frm As Object)    mInitResizeArray frmEnd Sub Public Sub ResizeControls(frm As Object)    mResizeControls frmEnd Sub

----------


## shragel

This is a control.

Here is the code.



vb Code:
Private Declare Function LockWindowUpdate Lib "user32" _    (ByVal hwnd As Long) As Long Dim lpara As LongPrivate Const CB_SETITEMHEIGHT = &H153 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _(ByVal hwnd As Long, ByVal wMsg As Long, _ByVal wParam As Long, lParam As Any) As Long  Public Reset As Boolean ' if True, also fonts are resized'Public ResizeFont As Boolean' if True, form's height/width ratio is preservedPublic KeepRatio As Boolean Private Type TControlInfo    ctrl As Control    Left As Single    Top As Single    Width As Single    Height As Single    FontSize As SingleEnd Type ' this array holds the original position' and size of all controls on parent formDim Controls() As TControlInfo ' a reference to the parent formPrivate WithEvents ParentForm As Form' parent form's size at load timePrivate ParentWidth As SinglePrivate ParentHeight As Single' ratio of original height/widthPrivate HeightWidthRatio As Single'Default Property Values:Const m_def_ResizeFont = 0'Property Variables:Dim m_ResizeFont As Boolean Sub LockWindow(hwnd As Long)    LockWindowUpdate hwndEnd Sub Sub UnlockWindow()    LockWindowUpdate 0End Sub Private Sub ParentForm_Activate()On Error Resume NextParentForm.Left = GetSetting(App.EXEName, ParentForm.Name, "Left", ParentForm.Left)ParentForm.Top = GetSetting(App.EXEName, ParentForm.Name, "Top", ParentForm.Top)ParentForm.Height = GetSetting(App.EXEName, ParentForm.Name, "Height", ParentForm.Height)ParentForm.Width = GetSetting(App.EXEName, ParentForm.Name, "Width", ParentForm.Width)End Sub Private Sub ParentForm_Load()    ' the ParentWidth variable works as a flag    ParentWidth = 0    ' save original ratio    HeightWidthRatio = ParentForm.Height / ParentForm.Width End SubPrivate Sub ParentForm_Unload(Cancel As Integer) If Not ParentForm.WindowState = vbMinimized Then     If ParentForm.Left > -1000 Then SaveSetting App.EXEName, ParentForm.Name, "Left", ParentForm.Left     If ParentForm.Top > -1000 Then SaveSetting App.EXEName, ParentForm.Name, "Top", ParentForm.Top     If ParentForm.Height > 1000 Then SaveSetting App.EXEName, ParentForm.Name, "Height", ParentForm.Height     If ParentForm.Width > 1000 Then SaveSetting App.EXEName, ParentForm.Name, "Width", ParentForm.WidthEnd If On Error Resume Next If Reset = True Then DeleteSetting (App.EXEName)End Sub Private Sub UserControl_ReadProperties(PropBag As PropertyBag)     m_ResizeFont = PropBag.ReadProperty("ResizeFont", m_def_ResizeFont)   If Ambient.UserMode = False Then Exit Sub    ' store a reference to the parent form and    ' start receiving events    Set ParentForm = ParentEnd Sub Private Sub UserControl_Resize()    ' refuse to resize    Image1.Move 0, 0    UserControl.Width = Image1.Width    UserControl.Height = Image1.HeightEnd Sub ' trap the parent form's Resize event' this include the very first resize event' that occurs soon after form's load Private Sub ParentForm_Resize()If ParentForm.WindowState = 1 Then Exit Sub    If ParentWidth = 0 Then        Rebuild    Else        Refresh    End IfEnd Sub' save size and position of all controls on parent form' you should manually invoke this method each time you add a new control' to the form (through Load method of a control array)Sub Rebuild()' rebuild the internal tableDim i As Integer, ctrl As Control' this is necessary for controls that don't support' all properties (e.g. Timer controls)On Error Resume Next If Ambient.UserMode = False Then Exit Sub' save a reference to the parent form, and its initial sizeSet ParentForm = UserControl.ParentParentWidth = ParentForm.ScaleWidthParentHeight = ParentForm.ScaleHeight' read the position of all controls on the parent formReDim Controls(ParentForm.Controls.Count - 1) As TControlInfoDim Ctl As ControlOn Error Resume Next' Put the code here to resize the controls For i = 0 To ParentForm.Controls.Count - 1     With Controls(i)         '        Set .ctrl = ctrl         If ParentForm.Controls(i).Left < -1000 Then  'For controls on diferent tab        Debug.Print ParentForm.Controls(i).Name            .Left = ParentForm.Controls(i).Left + 75000        Else            .Left = ParentForm.Controls(i).Left        End If         .Top = ParentForm.Controls(i).Top        .Width = ParentForm.Controls(i).Width        .Height = ParentForm.Controls(i).Height        .FontSize = ParentForm.Controls(i).Font.Size     End With Next End Sub' update size and position of controls on parent formSub Refresh()Dim i As Integer, ctrl As ControlDim widthFactor As Single, heightFactor As SingleDim minFactor As Single' inhibits recursive calls if KeepRatio = TrueStatic executing As Boolean If executing Then Exit Sub If Ambient.UserMode = False Then Exit Sub If KeepRatio And ParentForm.WindowState = 0 Then    executing = True    ' we must keep original ratio    ParentForm.Height = HeightWidthRatio * ParentForm.Width    executing = FalseEnd If ' this is necessary for controls that don't support' all properties (e.g. Timer controls)On Error Resume NextwidthFactor = ParentForm.ScaleWidth / ParentWidthheightFactor = ParentForm.ScaleHeight / ParentHeight' take the lesser of the two If widthFactor < heightFactor Then    minFactor = widthFactorElse    minFactor = heightFactorEnd If Dim Ctl As ControlLockWindow (ParentForm.hwnd)' this is a regular resize For i = 0 To ParentForm.Controls.Count - 1     With Controls(i)         ' the change of font must occur *before* the resizing        ' to account for companion scrollbar of listbox        ' and other similar controls         If ResizeFont And (TypeOf ParentForm.Controls(i) Is TDBGrid) = False Then            ParentForm.Controls(i).Font.Size = .FontSize * minFactor        End If         ' move and resize the controls - we can't use a Move        ' method because some controls do not support the change        ' of all the four properties (e.g. Height with comboboxes)         If ParentForm.Controls(i).Left < -1000 Then        Debug.Print ParentForm.Controls(i).Name            ParentForm.Controls(i).Left = .Left * widthFactor            ParentForm.Controls(i).Left = ParentForm.Controls(i).Left - 75000        Else            ParentForm.Controls(i).Left = .Left * widthFactor        End If         ParentForm.Controls(i).Top = .Top * heightFactor        ParentForm.Controls(i).Width = .Width * widthFactor         If TypeOf ParentForm.Controls(i) Is ComboBox Then            lpara = ((.Height * heightFactor) \ Screen.TwipsPerPixelX) - 6            Call SendMessage(ParentForm.Controls(i).hwnd, CB_SETITEMHEIGHT, -1&, ByVal lpara)        Else            ParentForm.Controls(i).Height = .Height * heightFactor        End If     End With Next UnlockWindowEnd Sub 'Initialize Properties for User ControlPrivate Sub UserControl_InitProperties()'    m_ResizeFont = m_def_ResizeFont    m_ResizeFont = m_def_ResizeFontEnd Sub 'Write property values to storagePrivate Sub UserControl_WriteProperties(PropBag As PropertyBag)    Call PropBag.WriteProperty("ResizeFont", m_ResizeFont, m_def_ResizeFont)End Sub 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!'MemberInfo=0,0,0,1Public Property Get ResizeFont() As Boolean    ResizeFont = m_ResizeFontEnd Property Public Property Let ResizeFont(ByVal New_ResizeFont As Boolean)    m_ResizeFont = New_ResizeFont    PropertyChanged "ResizeFont"End Property

----------


## gabbvn

Thanks Martin verry much.
 But with Sreadsheet which rows and column fixed wich a litte is not zoom full in spread.
Only Spread zoom fullscreen. Rows and column not fixed in Spread zoom mode.

Can you help me. Thanks very much.

Soon hope you reply me answer.

----------


## storkath

Super Tool. 
Great, help me a lot.

Very good tool.

----------


## aztrix

Great tool MarkT fantastic job.

i just have one question for the OCX control. im using a Main form that has a picturebox on it into which i load the "sub" forms at runtime. if i add the OCX control to the main form, how would i get the current visible "sub" form to also re-size?

im using this method to load the "sub" form into the Main forms picturebox



```
        UnloadForms
        Load frmRulesMain
        SetParent frmRulesMain.hwnd, picDock.hwnd
        frmRulesMain.Move 0, 0, 500, 1500
        frmRulesMain.Visible = True
```

i have attempted to solve this issue by firstly creating a String called stActiveWindow in the Main Form, this string is populated by the "sub" forms Name property when it is loaded. then under the Main Forms resize event i had;



```
Private Sub Form_Resize()
    Dim frm As Form
    For Each frm In Forms
        If frm.Name = stActiveWindow Then
            frm.WindowState = 2
        End If
    Next
End Sub
```

in an attempt to try and make the "sub" form now maximise to the Main form. However this is not the case and the "sub" form stays at its normal size. Is anyone able to advise me of a solution to make the "sub" form resize at the same time as the Main form resize?

additionally this only problem occurs to the current visible "sub" form, if i resize the Main form then open another "sub" form, the new "sub" form WILL maximise correctly. What im trying to achieve is when the Main form is resized the visible/loaded "sub" form resizes at the same time.

Keep Smiling

----------


## V_Beginner

I know this is an old post but I just want to say thanks. I added this to my program and it works a treat  :Smilie:

----------


## IHappenToBe'

I know it's a very old post, but since it's not marked as resolved, and last entry was 2012, and my question is usefull: here I go.
I continued this post here: http://www.vbforums.com/showthread.p...65#post4568065
saying (with attached images) that since MarkT OCX contrl works perfectly for adapting your form to a bigger screen, it has a problem adapting a form to a smaller screen, basically because VB6 itself resize the form to 'fit' the screen, and part of it is lost. (If you want, follow the link and comment there).

Sorry for reopen an old post and thank you!

----------

