Solved

Maximizing Controls along with the Form

Posted on 2002-06-17
12
237 Views
Last Modified: 2010-05-03
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?

Thanks in advance,


Jazon
0
Comment
Question by:piratepatrol
12 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7083931
you need to add appropriate code to the Form_Resize event.
0
 
LVL 4

Expert Comment

by:Monchanger
ID: 7084165
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
 
LVL 4

Expert Comment

by:RichW
ID: 7087917
'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

Private Sub Form_Load
  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
 
LVL 3

Author Comment

by:piratepatrol
ID: 7091271
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
 
LVL 4

Expert Comment

by:RichW
ID: 7092360
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
 
LVL 3

Author Comment

by:piratepatrol
ID: 7094262
Hi RichW,

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


Jazon
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:RichW
ID: 7094511
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
 
LVL 4

Expert Comment

by:RichW
ID: 7094512
That line should read like this:

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

Author Comment

by:piratepatrol
ID: 7094573
Is this for Init or FormResize?  dRatioX is undefined in Init.
0
 
LVL 4

Accepted Solution

by:
RichW earned 50 total points
ID: 7095492
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7911094
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
 

Expert Comment

by:SpideyMod
ID: 7967662
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now