组合框文本从右到左对齐?

时间:2015-03-18 15:01:49

标签: vb.net

我有这个代码来改变组合填充颜色,每件事都很完美 但我需要让组合框文本从右到左对齐,任何帮助请求。

这是我的代码:

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.Index < 0 Then Exit Sub
    Dim rect As Rectangle = e.Bounds
    If e.State And DrawItemState.Selected Then
        e.Graphics.FillRectangle(Brushes.LightSeaGreen, rect) 'change the selected color you like
    Else
        e.Graphics.FillRectangle(SystemBrushes.Window, rect)
    End If
    Dim colorname As String = ComboBox1.Items(e.Index)
    Dim ba As New SolidBrush(Color.FromName(colorname))
    Dim b2 As Brush = Brushes.Black 'add one
    e.Graphics.DrawString(colorname, Me.ComboBox1.Font, b2, rect.X, rect.Y)
End Sub

Private Sub combo_colors1()
    Me.ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
    Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    Me.ComboBox1.BeginUpdate()
    ComboBox1.EndUpdate()
End Sub

1 个答案:

答案 0 :(得分:0)

尝试使用TextRenderer.DrawText方法的格式化标记:

'e.Graphics.DrawString(colorname, Me.ComboBox1.Font, Brushes.Black, rect.X, rect.Y)
TextRenderer.DrawText(e.Graphics, colorname, Me.ComboBox1.Font, e.Bounds, _
                      Color.Black, Color.Empty, _
                      TextFormatFlags.Right Or TextFormatFlags.VerticalCenter)