G
Guest
I want to right-align numbers in a combobox. Since there is no TextAlign
property for the ComboBox, I tried the following code in a form containing a
default combobox.
'Declarations and two subs.
Dim miarr() As Long = {200000000000333, 33}
Dim moGraphics As Graphics = Me.CreateGraphics
Dim msngWidth As Single = 120
Private Sub MyForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
Me.ComboBox1.DropDownWidth = CInt(msngWidth) + 10
Me.ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
For i = 1 To 2
Me.ComboBox1.Items.Add(i.ToString)
Next
End Sub
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
Try
Dim sngLen As Single =
moGraphics.MeasureString(miarr(e.Index).ToString, ComboBox1.Font).Width
Debug.WriteLine((sngLen).ToString)
e.DrawBackground()
e.Graphics.DrawString(miarr(e.Index).ToString, ComboBox1.Font,
System.Drawing.Brushes.Black, New _
RectangleF(msngWidth - sngLen, e.Bounds.Top + 2, msngWidth,
e.Bounds.Height))
Catch ex As Exception
End Try
End Sub
When I open this form and show the combo dropdown, I expected the numbers to
be right-aligned. Instead, they are off by about a half character. For the
values in the array, I get MeasureString values of 98.17106 and 16.26725
(default font).
Second observation, although DrawString() uses floating point numbers, it
seems that 16.40000 and 15.60000 produce identical positioned numbers; it
seems that the DrawString() method uses the Integer value when drawing the
characters.
This approach seems reasonable to me but the result is not satisfactory when
the numbers vary greatly in their digit count. What am I missing? Can
someone confirm this behaviour?
property for the ComboBox, I tried the following code in a form containing a
default combobox.
'Declarations and two subs.
Dim miarr() As Long = {200000000000333, 33}
Dim moGraphics As Graphics = Me.CreateGraphics
Dim msngWidth As Single = 120
Private Sub MyForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
Me.ComboBox1.DropDownWidth = CInt(msngWidth) + 10
Me.ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
For i = 1 To 2
Me.ComboBox1.Items.Add(i.ToString)
Next
End Sub
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
Try
Dim sngLen As Single =
moGraphics.MeasureString(miarr(e.Index).ToString, ComboBox1.Font).Width
Debug.WriteLine((sngLen).ToString)
e.DrawBackground()
e.Graphics.DrawString(miarr(e.Index).ToString, ComboBox1.Font,
System.Drawing.Brushes.Black, New _
RectangleF(msngWidth - sngLen, e.Bounds.Top + 2, msngWidth,
e.Bounds.Height))
Catch ex As Exception
End Try
End Sub
When I open this form and show the combo dropdown, I expected the numbers to
be right-aligned. Instead, they are off by about a half character. For the
values in the array, I get MeasureString values of 98.17106 and 16.26725
(default font).
Second observation, although DrawString() uses floating point numbers, it
seems that 16.40000 and 15.60000 produce identical positioned numbers; it
seems that the DrawString() method uses the Integer value when drawing the
characters.
This approach seems reasonable to me but the result is not satisfactory when
the numbers vary greatly in their digit count. What am I missing? Can
someone confirm this behaviour?