Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
how to put the text in the center in combobox in vb

Answered how to put the text in the center in combobox in vb

  • Sunday, July 23, 2006 2:22 PM
     
     

    Is thre any property the centeralize the text in combobox in the center?

    I know that in text box there is propert alignment,

    text.textalign

     but in combobox I don't find any property who do that?

    thanks!!!

All Replies

  • Sunday, July 23, 2006 3:48 PM
    Moderator
     
     
  • Monday, July 24, 2006 5:37 PM
    Moderator
     
     Answered
    Add a new class to your project, then paste this code:

    Public Class CenteredComboBox
      Inherits ComboBox
      Sub New()
        Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
        Me.DropDownStyle = ComboBoxStyle.DropDownList
      End Sub

      Private Sub CenteredComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
        e.DrawBackground()
        Dim txt As String = ""
        If e.Index >= 0 Then txt = Me.Items(e.Index).ToString
        TextRenderer.DrawText(e.Graphics, txt, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.HorizontalCenter)
        e.DrawFocusRectangle()
      End Sub
    End Class

    Build your project.  Now you can put CenteredComboBox controls on your form.  Please note that the text box portion of the control is only centered when you use the DropDownList style.

  • Monday, March 03, 2008 6:55 PM
     
     

     

    NoBugs:  That was TOTALLY FLAWLESS and RIGHT ON TIME!  THANK YOU!

     

    - MeanDude