Results 1 to 13 of 13

Thread: Combo Box align Right

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Combo Box align Right

    Hi,

    I have a combo box on my form, which aligns left. I want it to align right instead. In the Properties section I can change the RighttoLeft to YES but the dropdownlist will show on the right instead of the left.

    I want it to remain on the left and text aligned from the right..

    Please advice

    Thank you

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Combo Box align Right

    RightToLeft property is intended for displaying text in laguages like Arabic or Hebrew where words are read from right to left.
    As far as I know the ComboBox class does not have a textalign property. I think the simplest way to implement right-alignment would be setting its DrawMode property to DrawMode.OwnerDrawFixed and draw your elements there as you see fit.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Combo Box align Right

    Any ideas how I can do that please

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Combo Box align Right

    Here. You can either handle each DrawItem method individually or extend the Combobox class and add this functionality to an inherited control:

    Result:


    Code example:
    vb.net Code:
    1. Public Class Form1
    2.     Friend WithEvents Combo1 As New ComboBox With { _
    3.         .Location = New Point(10, 10), _
    4.         .Size = New Size(300, 16), _
    5.         .DropDownStyle = ComboBoxStyle.DropDownList, _
    6.         .DrawMode = DrawMode.OwnerDrawFixed}
    7.  
    8.  
    9.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    10.         Combo1.Items.AddRange(New String() {"One", "Two", "Three", "Four", "Five", "Six"})
    11.         Me.Controls.Add(Combo1)
    12.     End Sub
    13.  
    14.     Private Sub Combo1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles Combo1.DrawItem
    15.  
    16.         If e.Index = -1 Then
    17.             e.DrawBackground()
    18.             Exit Sub
    19.         End If
    20.         Dim h As Single = Combo1.Font.Height
    21.  
    22.         Dim sf As New StringFormat
    23.         sf.LineAlignment = StringAlignment.Center
    24.         sf.Alignment = StringAlignment.Far
    25.  
    26.         Dim itemtext As String = Combo1.Items(e.Index).ToString
    27.  
    28.         e.DrawBackground()
    29.         Dim br As Brush = SystemBrushes.WindowText
    30.  
    31.         Select Case e.State
    32.             Case DrawItemState.Selected
    33.                 e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
    34.                 br = SystemBrushes.HighlightText
    35.         End Select
    36.  
    37.         e.Graphics.DrawString(itemtext, Combo1.Font, br, e.Bounds, sf)
    38.     End Sub
    39.  
    40.  
    41. End Class

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Combo Box align Right

    the above code gives an error;

    End of statement expected

    AND THE FOLLOWING CODE IS HIGHLIGHTED

    { _
    .Location = New Point(10, 10), _
    .Size = New Size(300, 16), _
    .DropDownStyle = ComboBoxStyle.DropDownList, _
    .DrawMode = DrawMode.OwnerDrawFixed}

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Combo Box align Right

    That's strange. Well, change it to:
    Code:
    Public Class Form1
        Friend WithEvents Combo1 As New ComboBox 
    
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            With Combo1
               .Location = New Point(10, 10)
               .Size = New Size(300, 16)
               .DropDownStyle = ComboBoxStyle.DropDownList
               .DrawMode = DrawMode.OwnerDrawFixed
            End Sub
    
            Combo1.Items.AddRange(New String() {"One", "Two", "Three", "Four", "Five", "Six"})
            Me.Controls.Add(Combo1)
        End Sub
    
        Private Sub Combo1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles Combo1.DrawItem
    
            If e.Index = -1 Then
                e.DrawBackground()
                Exit Sub
            End If
            Dim h As Single = Combo1.Font.Height
    
            Dim sf As New StringFormat
            sf.LineAlignment = StringAlignment.Center
            sf.Alignment = StringAlignment.Far
    
            Dim itemtext As String = Combo1.Items(e.Index).ToString
    
            e.DrawBackground()
            Dim br As Brush = SystemBrushes.WindowText
    
            Select Case e.State
                Case DrawItemState.Selected
                    e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
                    br = SystemBrushes.HighlightText
            End Select
    
            e.Graphics.DrawString(itemtext, Combo1.Font, br, e.Bounds, sf)
        End Sub
    
    
    End Class

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Combo Box align Right

    is there a simpler way of doing it

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Combo Box align Right

    I'm afraid no.
    As I said you can only do it once if you extend the standard combobox.

    The alternative would be looking for a third-party control and it's unlikely it would be free.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Combo Box align Right

    Code:
    Public Class Form1
        Friend WithEvents Combo1 As New ComboBox With {_
            .Location = New Point(10, 10), _
            .Size = New Size(300, 16), _
            .DropDownStyle = ComboBoxStyle.DropDownList, _
            .DrawMode = DrawMode.OwnerDrawFixed}
    
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Combo1.Items.AddRange(New String() {"One", "Two", "Three", "Four", "Five", "Six"})
            Me.Controls.Add(Combo1)
        End Sub
    
        Private Sub Combo1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles Combo1.DrawItem
    
            If e.Index = -1 Then
                e.DrawBackground()
                Exit Sub
            End If
            Dim h As Single = Combo1.Font.Height
    
            Dim sf As New StringFormat
            sf.LineAlignment = StringAlignment.Center
            sf.Alignment = StringAlignment.Far
    
            Dim itemtext As String = Combo1.Items(e.Index).ToString
    
            e.DrawBackground()
            Dim br As Brush = SystemBrushes.WindowText
    
            Select Case e.State
                Case DrawItemState.Selected
                    e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
                    br = SystemBrushes.HighlightText
            End Select
    
            e.Graphics.DrawString(itemtext, Combo1.Font, br, e.Bounds, sf)
        End Sub
    
    
    End Class
    Tried this received 2 erroes;

    - End of Statement required
    - Syntax error

    Whats the problem?
    -

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Combo Box align Right

    Quote Originally Posted by dr223 View Post
    Tried this received 2 erroes;

    - End of Statement required
    - Syntax error

    Whats the problem?
    -
    Cicatrix already gave you an alternate code example to deal with your error.

    I think the syntax cicatrix used in his first example only works in VS2008 and higher. Apparently you don't have VS2008 or higher, which you should have indicated in your post (there is a dropdown right next to where you type the title).

  11. #11
    New Member
    Join Date
    Jan 2008
    Location
    South Bend, IN USA
    Posts
    3

    Re: Combo Box align Right

    Hello,

    A space is needed between the left curly brace and the underscore, as below:

    Friend WithEvents Combo1 As New ComboBox With { _
    .Location = New Point(10, 10), _
    .Size = New Size(300, 16), _
    .DropDownStyle = ComboBoxStyle.DropDownList, _
    .DrawMode = DrawMode.OwnerDrawFixed}

  12. #12
    Hyperactive Member jazFunk's Avatar
    Join Date
    Dec 2008
    Location
    Palm Harbor
    Posts
    407

    Re: Combo Box align Right

    I'm no expert, but right off the bat this stand out. Please, correct me if I'm wrong:

    Shouldn't this:


    vb Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    2.         With Combo1
    3.            .Location = New Point(10, 10)
    4.            .Size = New Size(300, 16)
    5.            .DropDownStyle = ComboBoxStyle.DropDownList
    6.            .DrawMode = DrawMode.OwnerDrawFixed
    7.         End Sub   '  <--- Shouldn't this be "End With"?
    8.  
    9.         Combo1.Items.AddRange(New String() {"One", "Two", "Three", "Four", "Five", "Six"})
    10.         Me.Controls.Add(Combo1)
    11.     End Sub



    Be this:

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    2.         With Combo1
    3.            .Location = New Point(10, 10)
    4.            .Size = New Size(300, 16)
    5.            .DropDownStyle = ComboBoxStyle.DropDownList
    6.            .DrawMode = DrawMode.OwnerDrawFixed
    7.         End With  '  <--- Changed to "With"
    8.  
    9.         Combo1.Items.AddRange(New String() {"One", "Two", "Three", "Four", "Five", "Six"})
    10.         Me.Controls.Add(Combo1)
    11.     End Sub
    Things I've found useful:
    DateTime.ToString() Patterns | Retrieving and Saving Data in Databases | ADO.NET Data Containers: An Explanation

    Quote of the day from jmcilhinney:
    'Talking about Option Strict and Option Explicit in the same sentence is pointless unless it is to say that they should both be On.'

  13. #13

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