The new vbAccelerator Site - more VB and .NET Code and Controls

Set Left and Right Margin of Text Boxes and Combo Boxes

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Combo Box,Text Box,Windows,Windows Controls

Updated:

21/02/99

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


This tip demonstrates how to modify the left and right margins of Drop-Down Combo Boxes and Text box controls.

Start a new project then add a text box and a combo box control to the form:

Option Explicit

Private Const EC_LEFTMARGIN = &H1
Private Const EC_RIGHTMARGIN = &H2
Private Const EC_USEFONTINFO = &HFFFF&
Private Const EM_SETMARGINS = &HD3&
Private Const EM_GETMARGINS = &HD4&

Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" _
   (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
   (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, _
   ByVal lpszWindow As String) As Long

Private Property Get EdithWnd(ByVal ctl As Control) As Long
   If TypeName(ctl) = "ComboBox" Then
      EdithWnd = FindWindowEx(ctl.hwnd, 0, "EDIT", vbNullString)
   ElseIf TypeName(ctl) = "TextBox" Then
      EdithWnd = ctl.hwnd
   End If
End Property
Public Property Let RightMargin(ByVal ctl As Control, ByVal lMargin As Long)
   Dim lhWnd As Long
   lhWnd = EdithWnd(ctl)
   If (lhWnd <> 0) Then
      SendMessageLong lhWnd, EM_SETMARGINS, EC_RIGHTMARGIN, lMargin * &H10000
   End If
End Property
Public Property Get RightMargin(ByVal ctl As Control) As Long
   Dim lhWnd As Long
   lhWnd = EdithWnd(ctl)
   If (lhWnd <> 0) Then
      RightMargin = SendMessageLong(lhWnd, EM_GETMARGINS, 0, 0) \ &H10000
   End If
End Property
Public Property Let LeftMargin(ByVal ctl As Control, ByVal lMargin As Long)
   Dim lhWnd As Long
   lhWnd = EdithWnd(ctl)
   If (lhWnd <> 0) Then
      SendMessageLong lhWnd, EM_SETMARGINS, EC_LEFTMARGIN, lMargin
   End If
End Property
Public Property Get LeftMargin(ByVal ctl As Control) As Long
   Dim lhWnd As Long
   lhWnd = EdithWnd(ctl)
   If (lhWnd <> 0) Then
      LeftMargin = (SendMessageLong(lhWnd, EM_GETMARGINS, 0, 0) And &HFFFF&)
   End If
End Property


Private Sub Form_Load()
   LeftMargin(Text1) = EC_USEFONTINFO
   LeftMargin(Combo1) = EC_USEFONTINFO
End Sub


Start the project. The left hand margin of the text and combo boxes will be set to the same size. You can use the EC_USEFONTINFO (&HFFFF&) constant to set the margin to the default position, or you can set the exact amount in pixels.
&nbsp

Related Tips and Articles:

&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 21/02/99