newman     VBWeb     VBSource     Portfolio    

RESIZE.BAS Examples

REFERENCE

VBSource
These examples contain little more than a screen shot, a description and the source code that uses the Resize module.

This example is typical in that the form is resizeable. That is to say the BorderStyle property of this form is set to Resizeable. The form contains a frame control which contains seven other controls. In the Form_Resize event, if the user tries to make the form too small, this event pushes the form back open just enough. In the Form_Unload event, the code cleans up after itself.

Private Sub Form_Load()
    Call AddChild(Frame3D1, RE_WIDTH + RE_HEIGHT)
    Call AddContents(Command1, RE_TOP, Frame3D1)
    Call AddContents(Command2, RE_TOP, Frame3D1)
    Call AddContents(Command3, RE_LEFT + RE_TOP, Frame3D1)
    Call AddContents(Command4, RE_LEFT, Frame3D1)
    Call AddContents(Command5, RE_LEFT, Frame3D1)
    Call AddContents(Text1, RE_WIDTH + RE_HEIGHT, Frame3D1)
    Call AddContents(Combo1, RE_WIDTH, Frame3D1)
End Sub
Private Sub Form_Resize()
    If Me.WindowState = 1 Then Exit Sub

    Call EnforceForm(Me)
    Call EnforceContainer(Frame3D1)
    If CorrectionX > 0 Or CorrectionY > 0 Then
        'Move causes a cascading event.
        Me.Move Me.Left, Me.Top, _
            Me.Width + CorrectionX, _
            Me.Height + CorrectionY
    End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
    Call RemoveForm(Me)
End Sub
This is a common example. It keeps the scroll bars right along the edges of the form as the dorm is resized. In addition, clicking Command1 loads a second copy of the form. This example demonstrates good cleanup and the ability to handle changes in the ScaleMode.

Private Sub Command1_Click()
    Dim frmNew As New frmEditor
    frmNew.Show
End Sub
Private Sub Form_Load()
   Call AddChild(VScroll1, RE_LEFT + RE_HEIGHT)
   Me.ScaleMode = 1
   Call AddChild(HScroll1, RE_WIDTH + RE_TOP)
   Me.ScaleMode = 3
   Call AddChild(Picture1, RE_WIDTH + RE_HEIGHT)
   Me.ScaleMode = 1
End Sub
Private Sub Form_Resize()
    Static stSwitch As Integer

    If stSwitch Then
         Me.ScaleMode = 3
    Else: Me.ScaleMode = 1
    End If
    stSwitch = Not stSwitch
    Call EnforceForm(Me)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    Call RemoveForm(Me)
End Sub
The remaining examples all contain a Splitter Bar or Sash and Pane where the bar can be dragged to give more space on one side or the other. Such designs require a relationship between peer or sibling controls. Such controls share the same parent control. Even in such a situation, a hierarchy amung controls is still needed. One of the siblings must be designated the master. When the master control is moved, the other siblings move around it.

This example has a tall thin Label control in the middle that you can drag. Labels are called Light-Weight controls because they are not controls to Windows. Instead they are rendered by VB to look like controls. Typically, Light-Weight controls do not have a ScaleMode property which is needed to calculate the control's right and bottom positions. But in the case or a sash, only the left and top positions are needed. So a Light-Weight control can be used as a sash. Clicking Command1 loads a second copy of the form. The form is not resizable to keep the example simple.

Private Sub Command1_Click()
    Dim frmNew As New frmVerticalSplitterBar
    frmNew.Show
End Sub
Private Sub DropLabel(Source As Control, X As Single, Y As Single)
    Do
        CorrectionX = 0
        Source.Move X
        Call EnforceSash(Source)
        X = X + CorrectionX
    Loop Until CorrectionX = 0
End Sub
Private Sub Form_Load()
    Call AddPane(Text1, RE_WIDTH, Label1)
    Call AddPane(Command1, RE_LEFT, Label1)
    Call AddPane(Text2, RE_WIDTH + RE_LEFT, Label1)
    Call AddPane(Command2, RE_LEFT, Label1)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    Call RemoveForm(Me)
End Sub
Private Sub Text1_DragDrop(Source As Control, X As Single, Y As Single)
    Call DropLabel(Source, X + Text1.Left, Y + Text1.Top)
End Sub
Private Sub Text2_DragDrop(Source As Control, X As Single, Y As Single)
    Call DropLabel(Source, X + Text2.Left, Y + Text2.Top)
End Sub
	
This example shows a list box with a short wide picture box over it. The picture box is used as a Master or Sash control in that it gets dragged to change the hight of the list box. List boxes present a particular problem in that they do not keep the height they are assigned. List boxes snap to the closest height that accomodates an even number of lines. They do not allow a hight the displays only part of the lower line. When the list box snaps, it's the lower edge that moves which might not be the edge moved by enforcement. So the Resize module shecks both edges for errors and adds the error to a Correction variable. You can see how the Correction variable is used in the CommonDrop procedure. It's used to push the Master control back a bit. Even then, the bottom edge might be off a bit. So the correction is put in a loop.

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Y)
End Sub
Private Sub Form_Load()
    Call AddPane(List1, RE_TOP + RE_HEIGHT, Picture1)
End Sub
Private Sub List1_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Y + List1.Top)
End Sub
Private Sub CommonDrop(ByVal Y As Single)
    Do
        CorrectionY = 0
        Picture1.Move Picture1.Left, Y
        Call EnforceSash(Picture1)
        Y = Y + CorrectionY
    Loop Until CorrectionY = 0
End Sub
This example is just a bit more complicated with two list boxes. And the form is resizable. Both list boxes are associated with the sash and the form. This example is designed to test listboxes fighting with each other of position. Both list boxes want to have an even number of rows. So they both generate corrections in both the EnforceSash and EnforceForm instructions. The Resize module contains logic to control this, but it still happens on occation.
Private Sub Form_Load()
    Call AddChild(List1, RE_WIDTH)
    Call AddChild(Picture1, RE_WIDTH)
    Call AddChild(List2, RE_WIDTH + RE_HEIGHT)
    Call AddPane(List1, RE_HEIGHT, Picture1)
    Call AddPane(List2, RE_HEIGHT + RE_TOP, Picture1)
End Sub
Sub CommonDrop(ByVal Y As Long)
    Do
        CorrectionY = 0
        Picture1.Move Picture1.Left, Y
        Call EnforceSash(Picture1)
        Y = Y + CorrectionY
    Loop Until CorrectionY = 0
End Sub
Private Sub Form_Resize()
    If Me.WindowState = 1 Then
        Exit Sub
    End If

    Call EnforceForm(Me)
    If CorrectionX > 0 Or CorrectionY > 0 Then
        'Move causes a cascading event.
        Me.Move Me.Left, Me.Top, Me.Width + CorrectionX, Me.Height + CorrectionY
    End If
End Sub
Private Sub List1_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Y + List1.Top)
End Sub
Private Sub List2_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Y + List2.Top)
End Sub
This example also contains to list boxes that fight for position. But unlike the last example, this battle can go on forever. As a result, the endless Do loop normally found in the CommonDrop procedure has been replaced by a For Next loop that is limited to three loops. The real trick to this example is that the lists are in frames. This causes the Correction amount to go off in the wrong direction for all the controls under the sash. That is why the Correction is turned negative just while the EnforceContainer(Frame2) instruction is called.

Private Sub CommonDrop(Source As Control, ByVal X As Single, ByVal Y As Single)
    Dim i As Integer
    
    For i = 1 To 3
        Source.Move Source.Left, Y
        CorrectionY = 0
        Call EnforceSash(Source)
        Call EnforceContainer(Frame1)
        CorrectionY = -CorrectionY
        Call EnforceContainer(Frame2)
        If CorrectionY = 0 Then Exit For
        
        Y = Y - CorrectionY
    Next 'i
End Sub
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Source, X, Y)
End Sub
Private Sub Form_Load()
    Call AddPane(Frame1, RE_HEIGHT, Picture1)
    Call AddPane(Frame2, RE_TOP + RE_HEIGHT, Picture1)
    Call AddContents(List1, RE_WIDTH + RE_HEIGHT, Frame1)
    Call AddContents(List2, RE_WIDTH + RE_HEIGHT, Frame2)
    Call AddChild(Picture1, RE_WIDTH)
    Call AddChild(Frame1, RE_WIDTH)
    Call AddChild(Frame2, RE_WIDTH + RE_HEIGHT)
End Sub
Private Sub Form_Resize()
    If Me.WindowState = 1 Then Exit Sub 'MINIMIZED

    Call EnforceForm(Me)
    Call EnforceContainer(Frame1)
    Call EnforceContainer(Frame2)
    If (CorrectionX > 0 Or CorrectionY > 0) And Me.WindowState = 0 Then 'NORMAL
        'Move causes a cascading event.
        Me.Move Me.Left, Me.Top, Me.Width + CorrectionX, Me.Height + CorrectionY
    End If
End Sub
Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Source, X + Frame1.Left, Y + Frame1.Top)
End Sub
Private Sub Frame2_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Source, X + Frame2.Left, Y + Frame2.Top)
End Sub
Private Sub List1_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Source, X + List1.Left + Frame1.Left, Y + List1.Top + Frame1.Top)
End Sub
Private Sub List2_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Source, X + List2.Left + Frame2.Left, Y + List2.Top + Frame2.Top)
End Sub
Private Sub Picture1_DragDrop(Source As Control, X As Single, Y As Single)
    Call CommonDrop(Source, X + Picture1.Left, Y + Picture1.Top)
End Sub
	


Feedback Comments
Return Address (if you want a reply)


Copyright 1997 NEWMAN Services Corp. All rights reserved.
973-228-5753 pres@newmanservices.com
DECONSTRUCT THIS PAGE