VB ActiveX UserControls
VB Custom Control By default is importments UserControl classes, custom controls can be implemented by implementing UserControl events
UserControl class common events include:
Event AccessKeyPress (Keyascii As Integer)
Occurs when the user of the control presses one of the control's access keys, or when the Enter key is pressed when the developer has set the Default property to True, or when the Escape key is pressed when the developer has set the Cancel property to True ............... ..
Event Click () Occurs When The User Presses and The Releases A Mouse Button over an Object.
Event dblclick () Occurs When the user presses and realases a mouse button
AND THEN PRESSES AND RELASES IT AGAIN over an Object.
Event Dragdrop (Source As Control, X As Single, Y as Single)
Occurs WHEN A DRAG-AND-DROP OPERATION IS Completed.
Event Dragover (Source As Control, X As Single, Y As Single, State As Integer)
Occurs WHEN A DRAG-AND-DROP OPERATION IS in Progress.
Event Enterfocus () Occurs When Focus Enters The Control. The Control Itself Could Be Receiving Focus,
OR A Constituent Control Could Be Receiving Focus.
Event EXITFOCUS () Occurs When Focus Leaves The Control. The Control Itself Could Be Losing Focus,
OR A Constituent Control Could Be Losing Focus.
Event Gotfocus () Occurs When An Object Receives The Focus.
Event lostfocus () Occurs When an Object Loss The Focus.
Event GetDataMember (DataMber As String, Data As Object)
Occurs WHEN A DATA Consumer is asking this Data Source for One of it's data members.
Event show () Occurs When the Control's Visible Property Changes To True.
Event Hide () Occurs When the Control's Visible Property Changes To False.Event Hittest (X as single, y as single, hitresult as integer)
Occurs in a Windowless User Control in response to mouse activity.
Event initialize () Occurs When An Application Creates An Instance of A Form, MDIFORM, or Class.
Event Terminate () Occurs When All References To An Instance of A Form, MDIFORM, or Class
Are Removed from membay.
Event initproperties () Occurs the first time a user control or user document is created.
Sub PropertyChanged ([PropertyName])
Notifies The Container That A Property On A User Control Has Been Changed.
Event Readproperties (Propbag as propertybag) (PROPBAG AS Propertybag)
Occurs WHEN A User Control or User Document Is asked To Read ITA from a file.
Event WriteProperties (Propbag As Propertybag)
Occurs WHEN A User Control or User Document Is asked to Write ITS Data To a file.
Event KeyDown (Keycode As Integer, Shift As Integer)
Occurs when the user presses a key while an object Has the focus.
Event KeyPress (Keyascii AS Integer)
Occurshen the user presses and realases an ANSI key.
Event Keyup (Keycode As Integer, Shift As Integer)
Occurs when the user relases a key while an object Has the focus.
Event MouseDown (Button As Integer, Shift As Integer, x as single, y as single)
Occurs when the user presses the mouse button..
Event Paint () Occurs When Any Part of A Form or Picturebox Control Is Moved, Enlarged, or Exposed.
Event resize () Occurs When a form is first displayed or the size of an Object Changes
This example is known to understand the implementation of VB custom controls by implementing a custom control of a text box that is limited to input numbers.
1. Create a custom NumericInPut, add a text box txtInput in the custom control
2. Implementation When changing a custom control size, the TXTINPUT text box automatically fills the entire custom control area. You can pass the resize event of Implements UserControl, public declare function movewindow lib "user32" (byval hwnd as long, byval x as long, _
BYVAL NWIDTH AS Long, Byval NHEIGHT As Long, Byval Brepaint As Long AS Long
Public Function Twip2pixel (x) AS Integer
DIM CX As Long
DIM CY As Long
CX = Screen.twipsPixelx
Cy = Screen.twipsPerpixely
Twip2pixel = x / cx
END FUNCTION
Private Sub MoveControl (Ctrl As Control, Byval LEFT AS INTEGER, BYVAL TOP AS INTEGER, _
BYVAL WIDTH AS INTEGER, BYVAL HEIGHT AS INTEGER
DIM LRESULT AS Long
ON Error ResMe next
If Width <0 Then Width = 0
IF Height <0 Then Height = 0
LResult = MoveWindow (ctrl.hwnd, _
Twip2pixel (LEFT), Twip2pixel (TOP), Twip2pixel (Width), Twip2pixel (Height), 1)
'* If you change the size failure, change directly
If LRESULT = 0 Then Ctrl.move Left, Top, Width, Height
End Sub
Private sub UserControl_resize ()
MoveControl TxtInput, 0, 0, UserControl.scalewidth, UserControl.scaleHeight
End Sub
3. Add attributes for custom controls
Add an ALIGNMENT property to the control, four options for LEFT JUSTIFY / Right Justify / Center / General to control the alignment of the text box
Public Enum AlignmentConstants
[Left Justify] = 0
[Right Justify] = 1
[Center] = 2
[General] = 3
[User Defined] = 4
END ENUM
PRIVATE M_Alignment As AlignmentConstants
'The attribute must be public, the option is an enumeration / Boolean type
Public property get alignment () as alignmentConstants
Alignment = m_alignment
End Property
Public Property Let Alignment (Value As AlignmentConstants)
m_alignment = value
PropertyChanged "Alignment"
End Property
Readproperties and WriteProperties events are used to save and read the value after the attribute changes, otherwise each update control is used by default attribute values.
Private sub UserControl_readproperties (Propbag As Propertybag)
Alignment = propbag.readproperty ("alignment", 0) End Sub
Private sub UserControl_WriteProperties (Propbag As Propertybag)
Call Propbag.writeProperty ("Alignment", Align, 0)
End Sub
4. Add events for custom controls
Add a Click event to custom controls, when you click TXTINPUT
Event click ()
Private sub txtinput_click ()
RaiseEvent Click
End Sub
5. Expansion, expand custom controls into combination controls
The DrawFraMecontrol function provided by the Win32 API is used to depict a standard control. A frame of a button or scroll bar can be depicted, in this case, the TXTINPUT is expanded, add a button on the right side of TXTINPUT to make TXTINPUT a selection box.
The DrawFraMecontrol API function is defined as follows
Declare Function DrawFrameControl LIB "User32"
Byval HDC As Long, _ 'To paint in it
LPRECT AS Rect, _ 'Specifies a rectangle of the position and size of the frame
Byval un1 as long, _ 'Specify a constant of the frame type
Byval un2 as long _ 'a constant, specify the status of the frame to be depicted.
) AS long 'non-zero means success, zero expression failed
'Frame type constant includes
Private const DFC_CAPTION = 1 'Title Bar
Private const dfc_menu = 2 'menu
Private const dfc_scroll = 3 'scroll bar
Private const DFC_Button = 4 'Standard Button
'Frame state constant includes
Private const DFCS_CAPTIONCLOSE = & H0 'Close Button
Private const DFCS_CAPTIONMIN = & H1 'Minimize Button
Private const DFCS_CAPTIONMAX = & H2 'Maximize Button
Private const DFCS_CAPTIONRESTORE = & H3 'RESTORE button
Private const DFCS_CAPTIONHELP = & H4 'Windows 95 Only: Help Button
PRIVATE CONST DFCS_MENUARROW = & H0 'SubMenu Arrow
Private const dfcs_menucheck = & h1 'Check Mark
PRIVATE CONST DFCS_MENUBULLET = & H2 'Bullet
PRIVATE CONST DFCS_MENUARROWRIGHT = & H4PRIVATE CONST DFCS_SCROLLUP = & H0 'UP Arrow of Scroll Bar
Private const DFCS_SCROLLDOWN = & H1 'Down Arrow of Scroll Bar
Private const dfcs_scrollleft = & h2 'Left arrow of scroll bar
Private const dfcs_scrollright = & h3 'Right Arrow of Scroll Bar
Private const dfcs_scrollcomboBOX = & H5 'Combo Box Scroll' Bar
Private const dfcs_scrollsizegrip = & h8 'Size Grip
Private const dfcs_scrollsizegripright = & H10 'Size Grip in bottom-Right Corner of window
Private const DFCS_BUTTONCHECK = & H0 'CHECK BOX
Private const DFCS_BUTTONRADIO = & H4 'Radio Button
Private const dfcs_button3state = & h8 'Three-State Button
Private const dfcs_buttonpush = & h10 'push button
Private const dfcs_inactive = & h100 'Button is inactive (grayed)
Private const DFCS_PUSHED = & H200 'Button IS PUSHED
Private const dfcs_checked = & h400 'Button is Checked
Private const DFCS_ADJUSTRECT = & H2000 'bounding rectangle is adjusted to excedes the surrounding
'Edge of the Push Button
Private const dfcs_flat = & h4000 'Button Has A Flat Border
PRIVATE CONST DFCS_MONO = & H8000 'Button Has A Monochrome Border
'Draw text constant
Private const dt_center = & h1
Private const dt_left = & h0
PRIVATE CONST DT_RIGHT = & H2
Private const dt_vcenter = & h4
Private const dt_singlex = & h20
Private sub Draw ()
IF txtinput.locked or not txtinput.enabled then
MoveControl Txtinput, 0, 0, UserControl.scalewidth, UserControl.scaleHeightelse
If UserControl.Borderstyle = 1 THEN
MoveControl Txtinput, 0, 0, UserControl.Width - 300, UserControl.Height
Else
MoveControl Txtinput, 0, 0, UserControl.Width - 250, UserControl.Height
END IF
Call Drawpushbutton (False)
END IF
End Sub
Private Sub DrawpushButton (PUSHED AS BOOLEAN) '* Draw Button
DIM RC As Rect
GetWindowRect UserControl.hwnd, RC
Rc.right = rc.right - rc.left
If UserControl.Borderstyle = 1 THEN
IF userControl.appearance = 1 THEN
Rc.bottom = rc.bottom - rc.top - 3
Rc.right = rc.right - 3
Else
Rc.bottom = rc.bottom - rc.top - 2
Rc.right = rc.right - 2
END IF
Else
Rc.bottom = rc.bottom - rc.top
END IF
Rc.Left = twip2pixel (txtinput.width)
rc.top = 0
UserControl.cls
DrawFrameControl UserControl.hdc, RC, DFC_Button DFCS_Buttonpush, _
DFCS_ADJUSTRECT DFCS_BUTTON3STATE
'* Draw text
IF pushed the
Rc.right = rc.right 1
Rc.Bottom = rc.bottom 1
END IF
Drawtext UserControl.hdc, "...", -1, rc, dt_center dt_vcenter dt_singlex
End Sub
'Draw buttons in UserControl Show events
Private sub UserControl_show ()
Call Draw
End Sub
'Response Button Click Event
'Implement UserControl_MouseDown event
Event buttonclick ()
Private Sub UserControl_MouseDown (Button As Integer, Shift As Integer, x as single, y as single)
If Button = 0 THEN
RaiseEvent ButtonClick
END IF
End Sub