Click to See Complete Forum and Search --> : Resizing form and textboxes ...
nottheboss
Jan 28th, 2002, 12:19 PM
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
Jan 28th, 2002, 12:21 PM
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
Jan 28th, 2002, 12:22 PM
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
Jan 28th, 2002, 12:28 PM
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.
:) :) :) :) :) :) :)
MartinLiss
Jan 28th, 2002, 12:32 PM
You're welcome. You may find one problem and that is that comboboxes of style 2 don't resize properly.
MartinLiss
Jan 28th, 2002, 03:56 PM
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 (http://www.vbforums.com/showthread.php?s=&threadid=13090&highlight=ProportionsArray)
Hack
Jan 28th, 2002, 07:37 PM
You could have kept that a secret Martin. I wouldn't have told anyone! :D
MarkT
Jan 29th, 2002, 11:54 AM
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.
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
Jan 29th, 2002, 12:33 PM
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
Jan 29th, 2002, 12:45 PM
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
Jun 4th, 2002, 03:25 PM
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:
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:
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
Jun 4th, 2002, 03:53 PM
I've recently added the following modification to that code that adjusts the font size within the controls.
Dim dblFontSize As Double
' 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
frmMain.Controls(intIndex).FontSize = dblFontSize
Select Case True
' etc.
BruceG
Jun 15th, 2002, 03:11 PM
I just found this while doing a search; works great! Bravo, Martin.
MikeGarvin
Dec 8th, 2002, 01:24 PM
Also hiting the maximise button does not resize the form..
MartinLiss
Dec 8th, 2002, 01:29 PM
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
Dec 8th, 2002, 01:35 PM
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
Dec 8th, 2002, 01:38 PM
Yes the form does get maximised. Sorry my mistake.
MikeGarvin
Dec 8th, 2002, 01:50 PM
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
Dec 8th, 2002, 02:12 PM
Gotta go. I'll take a look later today.
MikeGarvin
Dec 8th, 2002, 02:17 PM
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
Dec 8th, 2002, 07:25 PM
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
Dec 9th, 2002, 10:57 AM
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
Dec 9th, 2002, 11:32 AM
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
Dec 9th, 2002, 02:13 PM
http://www.vbforums.com/showthread.php?s=&threadid=67533&highlight=disappear+controls
Have found a link with exactly the problem I have. Cheers. Mike.
MartinLiss
Dec 9th, 2002, 03:41 PM
Does that resolve your problem?
MikeGarvin
Dec 9th, 2002, 03:57 PM
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
May 29th, 2003, 03:27 PM
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
May 29th, 2003, 03:27 PM
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
May 29th, 2003, 04:04 PM
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.
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
May 29th, 2003, 04:24 PM
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 :)
Thanks,
Jon
jhermiz
May 29th, 2003, 05:28 PM
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
May 29th, 2003, 05:36 PM
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
May 29th, 2003, 05:39 PM
Figured that this was too good to be true :(
Anyone know of any 3rd party software to help me re-scale everything? Including tabs, combo boxes, and Grids?
Jon
DaveBo
May 30th, 2003, 02:26 PM
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:
'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
May 30th, 2003, 02:56 PM
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
mobjResizer.InitResizeArray Me
after creating them it all seems to work beautifully.
jhermiz
May 30th, 2003, 03:03 PM
Even for combo boxes?
Jon
veryjonny
Feb 3rd, 2004, 04:06 AM
Would someone pls post which of the codes is the final one.
Thank you.
Cimperiali
Feb 3rd, 2004, 05:09 AM
in resize event of a form:
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
May 23rd, 2004, 12:34 AM
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
May 23rd, 2004, 12:40 AM
Sorry, all I have is what is in the thread.
ahara
May 23rd, 2004, 11:27 AM
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
May 23rd, 2004, 01:23 PM
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:wave:
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
May 23rd, 2004, 02:17 PM
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
May 23rd, 2004, 10:26 PM
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 :):afrog:
jhermiz
May 24th, 2004, 07:12 AM
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
May 24th, 2004, 09:25 AM
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:wave:
Ummm...
Sorry for doing like a n00b, but how do i use that?:confused:
ahara
May 24th, 2004, 10:22 AM
slap this into any form that has controls (after importing the class into the project)
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
Oct 22nd, 2004, 12:38 AM
this seems to make my text smaller when form is max and text bigger when form gets smaller.... ?? how come?
dy1jaw
Sep 15th, 2005, 11:04 PM
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
Sep 15th, 2005, 11:06 PM
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
Sep 15th, 2005, 11:13 PM
i'm sorry but what is SSTab?
thanks
MartinLiss
Sep 16th, 2005, 06:15 AM
i'm sorry but what is SSTab?
thanks
It is the Microsoft Tabbed Dialog Control 6.0 found under Project|Components.
shragel
Dec 29th, 2005, 10:11 PM
Or just add this control to your form.
Mark Gambo
Dec 29th, 2005, 10:20 PM
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 :(
shragel
Dec 29th, 2005, 11:23 PM
So dont use it
Mark Gambo
Dec 29th, 2005, 11:38 PM
So dont use it
There are other alternatives (http://vbforums.com/showthread.php?t=376757)
shragel
Dec 30th, 2005, 09:10 AM
The problem with the alternative is it doesn't handle combo boxes, and the sstab control.
Mark Gambo
Dec 30th, 2005, 09:12 AM
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 (http://vbforums.com/attachment.php?attachmentid=43969&stc=1)
Here is the link to the entire post (http://vbforums.com/showthread.php?t=376757)
MarkT
Dec 30th, 2005, 10:26 AM
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
Dec 30th, 2005, 10:33 AM
This one I will take a look at as it includes source. Thanks MarkT :)
sanjaylimbikai
Dec 31st, 2005, 12:56 AM
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
Mar 11th, 2006, 10:14 AM
i know this is an older thread but why won't this code work for images?
lilwupster
Mar 11th, 2006, 10:26 AM
o, i see, i need the stretch property of the image set to true
wai1985
Apr 18th, 2007, 12:35 PM
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
Sep 29th, 2007, 02:30 PM
Does this ocx work with MDI forms and child forms?
If so a quick explanation would be great how to impliment this. :D
randem
Mar 3rd, 2008, 01:36 AM
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:
Option Explicit
Private Type CtrlProportions
HeightProportions As Single
WidthProportions As Single
TopProportions As Single
LeftProportions As Single
End Type
Dim ProportionsArray() As CtrlProportions
' 20080302RJ
Dim ArrayInitialized As Boolean
Dim 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 Long
Dim 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 frm
End Sub
Public Sub ResizeControls(frm As Object)
mResizeControls frm
End Sub
shragel
Mar 3rd, 2008, 08:05 AM
This is a control.
Here is the code.
Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hwnd As Long) As Long
Dim lpara As Long
Private 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 preserved
Public KeepRatio As Boolean
Private Type TControlInfo
ctrl As Control
Left As Single
Top As Single
Width As Single
Height As Single
FontSize As Single
End Type
' this array holds the original position
' and size of all controls on parent form
Dim Controls() As TControlInfo
' a reference to the parent form
Private WithEvents ParentForm As Form
' parent form's size at load time
Private ParentWidth As Single
Private ParentHeight As Single
' ratio of original height/width
Private HeightWidthRatio As Single
'Default Property Values:
Const m_def_ResizeFont = 0
'Property Variables:
Dim m_ResizeFont As Boolean
Sub LockWindow(hwnd As Long)
LockWindowUpdate hwnd
End Sub
Sub UnlockWindow()
LockWindowUpdate 0
End Sub
Private Sub ParentForm_Activate()
On Error Resume Next
ParentForm.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 Sub
Private 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.Width
End 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 = Parent
End Sub
Private Sub UserControl_Resize()
' refuse to resize
Image1.Move 0, 0
UserControl.Width = Image1.Width
UserControl.Height = Image1.Height
End 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 If
End 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 table
Dim 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 size
Set ParentForm = UserControl.Parent
ParentWidth = ParentForm.ScaleWidth
ParentHeight = ParentForm.ScaleHeight
' read the position of all controls on the parent form
ReDim Controls(ParentForm.Controls.Count - 1) As TControlInfo
Dim Ctl As Control
On 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 form
Sub Refresh()
Dim i As Integer, ctrl As Control
Dim widthFactor As Single, heightFactor As Single
Dim minFactor As Single
' inhibits recursive calls if KeepRatio = True
Static 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 = False
End If
' this is necessary for controls that don't support
' all properties (e.g. Timer controls)
On Error Resume Next
widthFactor = ParentForm.ScaleWidth / ParentWidth
heightFactor = ParentForm.ScaleHeight / ParentHeight
' take the lesser of the two
If widthFactor < heightFactor Then
minFactor = widthFactor
Else
minFactor = heightFactor
End If
Dim Ctl As Control
LockWindow (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
UnlockWindow
End Sub
'Initialize Properties for User Control
Private Sub UserControl_InitProperties()
' m_ResizeFont = m_def_ResizeFont
m_ResizeFont = m_def_ResizeFont
End Sub
'Write property values to storage
Private 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,1
Public Property Get ResizeFont() As Boolean
ResizeFont = m_ResizeFont
End Property
Public Property Let ResizeFont(ByVal New_ResizeFont As Boolean)
m_ResizeFont = New_ResizeFont
PropertyChanged "ResizeFont"
End Property
gabbvn
Feb 11th, 2009, 11:21 PM
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
Sep 26th, 2009, 06:46 AM
Super Tool.
Great, help me a lot.
Very good tool.
aztrix
Aug 19th, 2010, 07:10 AM
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.