Results 1 to 7 of 7

Thread: Center ComboBox Text

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Center ComboBox Text

    Is there any simple way to align the ComboBox text? (Center)

    If possible, I would like the items in the ComboBox to be aligned also.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Center ComboBox Text

    Here is how I would do it:-
    vbnet Code:
    1. Imports System.ComponentModel
    2. Public Class ComboBoxEx
    3.     Inherits ComboBox
    4.  
    5.  
    6.     Sub New()
    7.         MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    8.     End Sub
    9.  
    10.     'We hide this property from the intellisense and property grid
    11.     'to prevent anyone from changing it.
    12.     <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)> _
    13.     Public Shadows ReadOnly Property DrawMode() As DrawMode
    14.         Get
    15.             'This property is declared merely so we can
    16.             'hide it
    17.             Return MyBase.DrawMode
    18.         End Get
    19.     End Property
    20.  
    21.     Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
    22.         Dim txt As String = MyBase.Items(e.Index).ToString
    23.         Dim sf As New StringFormat
    24.         e.DrawBackground()
    25.  
    26.         sf.Alignment = StringAlignment.Center
    27.  
    28.         e.Graphics.DrawString(txt, e.Font, New SolidBrush(e.ForeColor), e.Bounds, sf)
    29.  
    30.         e.DrawFocusRectangle()
    31.  
    32.  
    33.     End Sub
    34. End Class
    Note that I could not find a way to center the textbox portion of the combox, only the list. Compile you program with this and this overriden combobox should appear in the toolbox of your project.
    Last edited by Niya; Feb 7th, 2012 at 12:36 AM. Reason: Edited code

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Center ComboBox Text

    Quote Originally Posted by Niya View Post
    Note that I could not find a way to center the textbox portion of the combox
    I think the edit box style (ES_CENTER) has to be set when the control is created, no clue how thats done in VB.
    I was bored and came up with this, it sets the left margin of the combo's edit box so text is centered, requires API...

    Code:
    Private Sub ComboBox1_TextChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.TextChanged
        ' center combo1 text when text changes
        CenterComboText(ComboBox1)
    End Sub
    
    Private Sub CenterComboText(cbo As ComboBox)
        ' combo must be set to DropDown style!
        If Not cbo.DropDownStyle = ComboBoxStyle.DropDown Then Return
        ' get handle to Edit box in combo
        Dim cboEdit_hWnd As IntPtr = FindWindowEx(cbo.Handle, IntPtr.Zero, "Edit", Nothing)
        ' if handle found set combo edit box text left margin
        If Not cboEdit_hWnd = IntPtr.Zero Then
            ' Determine width of string displayed
            Dim textWidth As Integer
            Using g As System.Drawing.Graphics = cbo.CreateGraphics()
                textWidth = CInt(g.MeasureString(cbo.Text, cbo.Font).Width)
            End Using
            ' get combo's Edit window size
            Dim rct As New RECT
            GetWindowRect(cboEdit_hWnd, rct)
            ' figure left margin so text appears centered in combo's edit box area.
            Dim leftMargin As Integer = ((rct.Right - rct.Left) - textWidth) \ 2
            ' set left margin of combo's Edit box 
            SendMessage(cboEdit_hWnd, EM_SETMARGINS, EC_LEFTMARGIN, leftMargin)
        End If
    End Sub

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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

  5. #5
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: Center ComboBox Text

    Awesome. Thank you.

  6. #6
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Center ComboBox Text

    This thread is literally over 6 1/2 years old, please don't resurrect it.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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