|
-
Apr 27th, 2010, 04:14 AM
#1
Thread Starter
Frenzied Member
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
-
Apr 27th, 2010, 04:27 AM
#2
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.
-
Apr 27th, 2010, 04:35 AM
#3
Thread Starter
Frenzied Member
Re: Combo Box align Right
Any ideas how I can do that please
-
Apr 27th, 2010, 05:20 AM
#4
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:
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
-
Apr 27th, 2010, 06:13 AM
#5
Thread Starter
Frenzied Member
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}
-
Apr 27th, 2010, 06:58 AM
#6
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
-
Apr 27th, 2010, 07:22 AM
#7
Thread Starter
Frenzied Member
Re: Combo Box align Right
is there a simpler way of doing it
-
Apr 27th, 2010, 07:31 AM
#8
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.
-
Apr 27th, 2010, 09:50 AM
#9
Thread Starter
Frenzied Member
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?
-
-
Apr 27th, 2010, 12:23 PM
#10
Re: Combo Box align Right
 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).
-
Jun 30th, 2010, 10:25 AM
#11
New Member
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}
-
Jun 30th, 2010, 05:58 PM
#12
Hyperactive Member
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
-
Jul 1st, 2010, 12:42 AM
#13
Re: Combo Box align Right
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|