• Status: Solved
• Priority: Medium
• Security: Public
• Views: 287

# Maximizing Controls along with the Form

Hi friends,

Can someone just remind me what you have to do so that your textboxes, buttons, labels, etc. are repositioned and/or enlarged when you maximize the form?

Jazon
0
piratepatrol
1 Solution

Senior .Net ConsultantCommented:
you need to add appropriate code to the Form_Resize event.
0

Commented:
This is a simple function I wrote for this purpose.
HeightPercent & WidthPercent are a measure of the control's size in relation to the form it is placed upon.
TopPercent is the relative vertical positioning of the control.
HorizCenterPercent talks about the horizontal position of the *center* of the control (not the left border as commonly done - tough you could alter the code to do that)

Finally, the four constants g_intBORDER_LEFT, g_intBORDER_TOP, g_intBORDER_BOTTOM & g_intBORDER_RIGHT are boundries which create an area in which the controls are contained. I don't know how to explain better, just that try setting them all to zero for starters. Then you can experiment a bit and give them values.

I almost forgot - SetControlCenter() is an example call to the main function, which centers the control horizontally inside the form (it's the long way around, but a good example, and it does the whole thing with the borders, which BTW do not have to be symetrical - so that a "centered" control might not actually be located at the center of the form, but at the center of the available area for controls)

Last word from me - as emoreau said, calls to these functions should appear in Form_Resize.

Monchanger

Public Sub SizeControlByPercent( _
ByRef theControl As Control, _
Optional ByVal HeightPercent As Single, _
Optional ByVal WidthPercent As Single, _
Optional ByVal TopPercent As Single = -1, _
Optional ByVal HorizCenterPercent As Single = -1)

If WidthPercent > 0 Then
theControl.Width = (WidthPercent / 100) * (theControl.Parent.ScaleWidth - g_intBORDER_LEFT - g_intBORDER_RIGHT)
End If

If HeightPercent > 0 Then
theControl.Height = (HeightPercent / 100) * (theControl.Parent.ScaleHeight - g_intBORDER_TOP - g_intBORDER_BOTTOM)
End If

If TopPercent <> -1 Then
theControl.Top = g_intBORDER_TOP + (TopPercent / 100) * (theControl.Parent.ScaleHeight - g_intBORDER_TOP - g_intBORDER_BOTTOM)
End If

If HorizCenterPercent <> -1 Then
theControl.Left = g_intBORDER_LEFT + (HorizCenterPercent / 100) * (theControl.Parent.ScaleWidth - g_intBORDER_LEFT - g_intBORDER_RIGHT) - (theControl.Width \ 2)
End If
End Sub

Public Sub SetControlCenter(ByRef theControl As Control)
SizeControlByPercent theControl, , , , 50#
End Sub
0

Commented:
'Paste this into a class module and name it cFormResize.

Option Explicit

Private nFormHeight      As Integer
Private nFormWidth       As Integer
Private nNumOfControls   As Integer
Private nTop()           As Integer
Private nLeft()          As Integer
Private nHeight()        As Integer
Private nWidth()         As Integer
Private nFontSize()      As Integer
Private nRightMargin()   As Integer
Private bFirstTime       As Boolean

Sub Init(frm As Form, Optional nWindState As Variant)

Dim I          As Integer
Dim bWinMax    As Boolean

bWinMax = Not IsMissing(nWindState)

nFormHeight = frm.Height
nFormWidth = frm.Width
nNumOfControls = frm.Controls.Count - 1
bFirstTime = True
ReDim nTop(nNumOfControls)
ReDim nLeft(nNumOfControls)
ReDim nHeight(nNumOfControls)
ReDim nWidth(nNumOfControls)
ReDim nFontSize(nNumOfControls)

ReDim nRightMargin(nNumOfControls)
On Error Resume Next
For I = 0 To nNumOfControls
If Not TypeOf frm.Controls(I) Is Timer And Not TypeOf frm.Controls(I) Is WebBrowser Then
If TypeOf frm.Controls(I) Is Line Then
nTop(I) = frm.Controls(I).Y1
nLeft(I) = frm.Controls(I).X1
nHeight(I) = frm.Controls(I).Y2
nWidth(I) = frm.Controls(I).X2
Else
nTop(I) = frm.Controls(I).Top
nLeft(I) = frm.Controls(I).Left
nHeight(I) = frm.Controls(I).Height
nWidth(I) = frm.Controls(I).Width
nFontSize(I) = frm.FontSize
nRightMargin(I) = frm.Controls(I).RightMargin
End If
End If
Next

If bWinMax Or frm.WindowState = 2 Then ' maxim
frm.Height = Screen.Height
frm.Width = Screen.Width
Else
frm.Height = frm.Height * Screen.Height / 7290
frm.Width = frm.Width * Screen.Width / 9690
End If

bFirstTime = True

End Sub

Sub FormResize(frm As Form)

Dim I             As Integer
Dim nCaptionSize  As Integer
Dim dRatioX       As Double
Dim dRatioY       As Double
Dim nSaveRedraw   As Long

On Error Resume Next
nSaveRedraw = frm.AutoRedraw

frm.AutoRedraw = True

If bFirstTime Then
bFirstTime = False
Exit Sub
End If

If frm.Height < nFormHeight / 2 Then frm.Height = nFormHeight / 2

If frm.Width < nFormWidth / 2 Then frm.Width = nFormWidth / 2
nCaptionSize = 400
dRatioY = 1# * (nFormHeight - nCaptionSize) / (frm.Height - nCaptionSize)
dRatioX = 1# * nFormWidth / frm.Width
On Error Resume Next ' for comboboxes, timers and other nonsizible controls

For I = 0 To nNumOfControls
If TypeOf frm.Controls(I) Is Line Then
frm.Controls(I).Y1 = Int(nTop(I) / dRatioY)
frm.Controls(I).X1 = Int(nLeft(I) / dRatioX)
frm.Controls(I).Y2 = Int(nHeight(I) / dRatioY)
frm.Controls(I).X2 = Int(nWidth(I) / dRatioX)
Else
frm.Controls(I).Top = Int(nTop(I) / dRatioY)
frm.Controls(I).Left = Int(nLeft(I) / dRatioX)
frm.Controls(I).Height = Int(nHeight(I) / dRatioY)
frm.Controls(I).Width = Int(nWidth(I) / dRatioX)
frm.Controls(I).FontSize = Int(nFontSize(I) / dRatioX) + Int(nFontSize(I) / dRatioX) Mod 2
frm.Controls(I).RightMargin = Int(nRightMargin(I) / dRatioY)
End If
Next

frm.AutoRedraw = nSaveRedraw

End Sub

' PASTE THIS IN YOUR FORM

Public resizer As cFormResize
Set resizer = New cFormResize
resizer.Init Me
End Sub

Private Sub Form_Resize()
resizer.FormResize Me
End Sub

This will resize all objects according to how you size your form.

RichW

0

Author Commented:
Hi RichW,

How do I change the code so that the only control that's resized is my textbox?  The rest of the code is perfect.

Thanks friend,

Jazon
0

Commented:
Go into the class code under the method Sub FormResize(frm As Form)

Replace the For statement with this:

For I = 0 To nNumOfControls
If TypeOf frm.Controls(I) Is TextBox Then
frm.Controls(I).Y1 = Int(nTop(I) / dRatioY)
frm.Controls(I).X1 = Int(nLeft(I) / dRatioX)
frm.Controls(I).Y2 = Int(nHeight(I) / dRatioY)
frm.Controls(I).X2 = Int(nWidth(I) / dRatioX)
'Else
'   frm.Controls(I).Top = Int(nTop(I) / dRatioY)
'   frm.Controls(I).Left = Int(nLeft(I) / dRatioX)
'   frm.Controls(I).Height = Int(nHeight(I) / dRatioY)
'   frm.Controls(I).Width = Int(nWidth(I) / dRatioX)
'   frm.Controls(I).FontSize = Int(nFontSize(I) / dRatioX) + Int(nFontSize(I) / dRatioX) Mod 2
'   frm.Controls(I).RightMargin = Int(nRightMargin(I) / dRatioY)
End If
Next

This will grab only textboxes.  If you wish to resize only one particular textbox you can add a second If statement under the first:

If TypeOf frm.Controls(I) Is TextBox Then 'Line Then
If frm.Controls(i).Name = "txtMyTextBox" Then

Change txtMyTextBox to the name of the textbox you wish to target.

RichW
0

Author Commented:
Hi RichW,

It no longer works after I make the changes.  Could it be because my textbox is within a frame?

Jazon
0

Commented:
Try this in the Init and the FormResize of the class:

For I = 0 To nNumOfControls
If TypeOf frm.Controls(I) Is TextBox Then
If TypeOf frm.Controls(I).Container Is Frame Then
frm.Controls(I).Top = Int(nTop(I) / dRatioY)
frm.Controls(I).Left = Int(nLeft(I) / dRatioX)
frm.Controls(I).Height = Int(nHeight(I) / dRatioY)
frm.Controls(I).Width = Int(nWidth(I) / dRatioX)
frm.Controls(I).FontSize = Int(nFontSize(I) / dRatioX) + Int(nFontSize(I) / dRatioX) Mod 2
frm.Controls(I).RightMargin = Int(nRightMargin(I) / dRatioY)
End If
End If
Next

0

Commented:
That line should read like this:

frm.Controls(I).FontSize = Int(nFontSize(I) / dRatioX) + Int(nFontSize(I) / dRatioX) Mod 2
0

Author Commented:
Is this for Init or FormResize?  dRatioX is undefined in Init.
0

Commented:
I just did this with a form that has three textboxes, and a label.  One of the textboxes is in a frame.

This code will resize only the textbox and frame.  I didn't originally realize that you had the textbox in a frame.  Sorry about that.

If you want only the textbox to resize, remove the reference to the frame's name.

For I = 0 To nNumOfControls
If TypeOf frm.Controls(I) Is TextBox Or TypeOf frm.Controls(I) Is Frame Then
If frm.Controls(I).Name = "Text3" Or frm.Controls(I).Name = "Frame1" Then
frm.Controls(I).Top = Int(nTop(I) / dRatioY)
frm.Controls(I).Left = Int(nLeft(I) / dRatioX)
frm.Controls(I).Height = Int(nHeight(I) / dRatioY)
frm.Controls(I).Width = Int(nWidth(I) / dRatioX)
frm.Controls(I).FontSize = Int(nFontSize(I) / dRatioX) + Int(nFontSize(I) / dRatioX) Mod 2
frm.Controls(I).RightMargin = Int(nRightMargin(I) / dRatioY)
End If
End If
Next

RichW
0

Commented:
Hi piratepatrol,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

Accept RichW's comment(s) as an answer.

piratepatrol, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0

Commented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.