473,394 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Right-aligned numbers in a combobox

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?
Nov 21 '05 #1
1 2311
Hi,

Use a stringformat for that. Here is an example.

Dim sf As New StringFormat
sf.Alignment = StringAlignment.Far

g.DrawString(miarr(e.Index).ToString, ComboBox1.Font, Brushes.Black,
RectangleF.op_Implicit(e.Bounds), sf)
Ken
-------------------
"genojoe" <ge*****@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
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?

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bo Wisén | last post by:
Hi, A project in VB6 recently worked without any problems but now it's complaining when I try to use 'Right$'. In immediate mode, when I'm typing 'print left$("123456",2)' I get the correct...
12
by: Ted Mencini | last post by:
When I replaced older <IMG ALIGN=RIGHT ...> tags with a CSS definition <IMG class=right ...> xxx.css: IMG.right { BORDER: 0; align: right } I notice that the effect is NOT the same....
2
by: Dr. Richard E. Hawkins | last post by:
I've googled around, and asked everyone I know, but I still can't find any reference to anyone else having faced this particular IE bug with floats. I've put a page at...
1
by: Astra | last post by:
Hi All This has been bugging me for months and I just can't my finger on why it won't work in Safari/FireFox (IE lets me off cos it's nice liek that). In essence, the offending code is as...
22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
3
by: deanfamily11 | last post by:
Ok, here's another problem I'm having. I've tried several different things, but I just can't get anything to be right justified. Any thoughts? #include <iostream> #include <iomanip>...
19
by: ashkaan57 | last post by:
Hi, I have a page in a right-to-left language and I am trying to make some bulleted lists using <ul>, but it puts the bullets to the left. Is there any way I can set the bullets to be on the...
5
by: tshad | last post by:
I have a datagrid that I cannot get to right justify a money amount (which is just a label). No matter what I do - it still right justifies it. <asp:TemplateColumn Visible="true"...
12
by: Dio1080 | last post by:
Ok, I need help on a for loop nested program using for loops, I'm trying to use a patten that goes like this: 123456 12345 1234 123 12 1 but when I try it, it goes like this: 123456
3
by: happyse27 | last post by:
Hi All, I wanted to align the text box for user registration but the code just wont budge... Kindly advise what is wrong? Cheers... Andrew <HTML>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.