Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 13

Implementing Constituent Controls

Recall that one of the ways you can implement an ActiveX control is through constituent controls—other controls you use as building blocks for the functionality and appearance of your custom control.

You can build your custom control from constituents by simply placing instances of controls from the toolbox on the UserControl's designer surface at design time, just as you would place controls on the surface of a Form in a standard EXE project (see Figure 13.6).

Placing constituent controls on a UserControl's surface
FIGURE 13.6 Placing constituent controls on a UserControl's surface

Managing Constituent Controls in the Resize Event

Constituent controls do not respond automatically as UserControls are resized. Instead, you must programmatically make provisions for constituent controls to respond to changes in the UserControl's size and shape.

The UserControl's Resize event will fire when a developer resizes a design-time instance of your control and also when the control's shape or size change at runtime.

You can, therefore, employ the UserControl's Resize event procedure to manage the appearance of the constituent controls within the ActiveX control and also perhaps to restrict the way in which the control can be resized.

In the example of Listing 13.10, we use the Resize event procedure to make sure that our custom control never shrinks below a certain minimum size. We also make sure that the constituent controls are always centered on the surface of the UserControl and bear the same relative positions.

LISTING 13.10
USING THE RESIZE EVENT PROCEDURE TO ENFORCE A CUSTOM CONTROL'S SIZE, SHAPE, AND THE VISUAL CONFIGURATION OF ITS CONSTITUENT CONTROLS

Private Sub UserControl_Resize()
   'minimum amount of space for borders
   Const MIN_BORDER_SIZE = 100
   Dim lMinHeight As Long
   Dim lMinWidth As Long
   'minimum allowable height & width
   'are determined from min border size
   '& sizes of constituent controls
   lMinHeight = 2 * MIN_BORDER_SIZE + _
   cmdApply.Height
   lMinWidth = 3 * MIN_BORDER_SIZE + _
      cmdApply.Width + _
      txtEnter.Width

   'if this UserControl has been
   're-sized to below minimum
   'sizes, then re-size it to
   'minimum allowable size
   If (ScaleHeight < lMinHeight) Then
      Height = lMinHeight + _
         (Height - ScaleHeight)
   End If
   If (ScaleWidth < lMinWidth) Then
      Width = lMinWidth + _
         (Width - ScaleWidth)
   End If

   'now figure out how big the
   'horizontal border can be
   'given the constituent control
   'sizes and the size of the UserControl
   Dim lhorizBorderSize As Long
   lhorizBorderSize = _
   (ScaleWidth - _
   txtEnter.Width - cmdApply.Width) / 3

   'set the horizontal alignment of
   'controls based on computed
   'horizontal border size
   txtEnter.Left = lhorizBorderSize
   cmdApply.Left = txtEnter.Left + _
      txtEnter.Width + _
      lhorizBorderSize

   'figure out vertical border
   Dim lVertBorderSize As Long
   lVertBorderSize = _
      (ScaleHeight - cmdApply.Height) / 2

    'set vertical alignment of controls
   cmdApply.Top = lVertBorderSize
   txtEnter.Top = lVertBorderSize
End Sub


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved