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.
Re: Combo Box align Right
Any ideas how I can do that please
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:
http://i39.tinypic.com/2lus6eg.png
Code example:
vb.net 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
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}
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
Re: Combo Box align Right
is there a simpler way of doing it
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.
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?
-
Re: Combo Box align Right
Quote:
Originally Posted by
dr223
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).
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}
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:
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 ' <--- Shouldn't this be "End With"?
Combo1.Items.AddRange(New String() {"One", "Two", "Three", "Four", "Five", "Six"})
Me.Controls.Add(Combo1)
End Sub
Be this:
vb Code:
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 With ' <--- Changed to "With"
Combo1.Items.AddRange(New String() {"One", "Two", "Three", "Four", "Five", "Six"})
Me.Controls.Add(Combo1)
End Sub
Re: Combo Box align Right