Results 1 to 7 of 7

Thread: Center ComboBox Text

Threaded View

  1. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Center ComboBox Text

    Nice Edge!! Here is the revised code with Edge's contribution:-
    vbnet Code:
    1. Imports System.ComponentModel
    2. Imports System.Runtime.InteropServices
    3.  
    4. Public Class ComboBoxEx
    5.     Inherits ComboBox
    6.  
    7.     Sub New()
    8.         MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    9.  
    10.     End Sub
    11.  
    12.     Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
    13.         MyBase.OnHandleCreated(e)
    14.         Helpers.CenterComboText(Me)
    15.     End Sub
    16.  
    17.     Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
    18.         Helpers.CenterComboText(Me)
    19.         MyBase.OnTextChanged(e)
    20.     End Sub
    21.  
    22.     'We hide this property from the intellisense and property grid
    23.     'to prevent anyone from changing it.
    24.     <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)> _
    25.     Public Shadows ReadOnly Property DrawMode() As DrawMode
    26.         Get
    27.             'This property is declared merely so we can
    28.             'hide it
    29.             Return MyBase.DrawMode
    30.         End Get
    31.     End Property
    32.  
    33.     Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
    34.         Dim txt As String = MyBase.Items(e.Index).ToString
    35.         Dim sf As New StringFormat
    36.  
    37.         e.DrawBackground()
    38.  
    39.         sf.Alignment = StringAlignment.Center
    40.  
    41.         e.Graphics.DrawString(txt, e.Font, New SolidBrush(e.ForeColor), e.Bounds, sf)
    42.  
    43.         e.DrawFocusRectangle()
    44.  
    45.     End Sub
    46. End Class
    47.  
    48. Friend Class Win32API
    49.     <StructLayout(LayoutKind.Sequential)> _
    50.     Public Structure RECT
    51.         Public Left As Integer
    52.         Public Top As Integer
    53.         Public Right As Integer
    54.         Public Bottom As Integer
    55.     End Structure
    56.  
    57.     Public Const EM_SETMARGINS As Integer = &HD3
    58.     Public Const EC_LEFTMARGIN = &H1
    59.  
    60.     Public Declare Function FindWindowEx Lib "User32.dll" Alias "FindWindowExA" (ByVal hWnd As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    61.     Public Declare Function GetWindowRect Lib "User32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
    62.     Public Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    63.  
    64. End Class
    65.  
    66. Friend Class Helpers
    67.     Public Shared Sub CenterComboText(ByVal cbo As ComboBox)
    68.         ' combo must be set to DropDown style!
    69.         If Not cbo.DropDownStyle = ComboBoxStyle.DropDown Then Return
    70.         ' get handle to Edit box in combo
    71.         Dim cboEdit_hWnd As IntPtr = Win32API.FindWindowEx(cbo.Handle, IntPtr.Zero, "Edit", Nothing)
    72.         ' if handle found set combo edit box text left margin
    73.         If Not cboEdit_hWnd = IntPtr.Zero Then
    74.             ' Determine width of string displayed
    75.             Dim textWidth As Integer
    76.             Using g As System.Drawing.Graphics = cbo.CreateGraphics()
    77.                 textWidth = CInt(g.MeasureString(cbo.Text, cbo.Font).Width)
    78.             End Using
    79.             ' get combo's Edit window size
    80.             Dim rct As Win32API.RECT
    81.             Win32API.GetWindowRect(cboEdit_hWnd, rct)
    82.             ' figure left margin so text appears centered in combo's edit box area.
    83.             Dim leftMargin As Integer = ((rct.Right - rct.Left) - textWidth) \ 2
    84.             ' set left margin of combo's Edit box
    85.             Win32API.SendMessage(cboEdit_hWnd, Win32API.EM_SETMARGINS, Win32API.EC_LEFTMARGIN, leftMargin)
    86.         End If
    87.     End Sub
    88. End Class
    Last edited by Niya; Feb 7th, 2012 at 04:07 AM. Reason: Minor code change

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width