473,398 Members | 2,389 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,398 software developers and data experts.

align text in a ListBox

I would like to align text in a ListBox to achieve columns.

In the past I could determine the text width in Twips and

pad in required number of blank spaces. But now I'm using

VB2005 and it does not support Twips. How do I accomplish this?

Thanks
Jan 3 '06 #1
2 18863
Hi,

Set the listbox draw mode to ownerdrawnfixed to control how the
text is aligned. This example will use a stringformat to center the text
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
For x As Integer = 0 To 20
ListBox1.Items.Add(x)
Next

End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim g As Graphics = e.Graphics
Dim br As SolidBrush
Dim s As String
Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center

Try
s = ListBox1.Items.Item(e.Index).ToString
Catch ex As Exception
s = ""
Trace.WriteLine(ex.ToString)
End Try

br = New SolidBrush(Color.White)

g.FillRectangle(br, e.Bounds)

If CBool(e.State And DrawItemState.Selected) Then
g.FillRectangle(Brushes.LightBlue, e.Bounds)
End If

g.DrawString(s, ListBox1.Font, Brushes.Black, _
RectangleF.op_Implicit(e.Bounds), sf)

br.Dispose()
End Sub
End Class

Ken
-------------------
"Tom McL." <tb***@cwnet.com> wrote in message
news:ue**************@tk2msftngp13.phx.gbl...
I would like to align text in a ListBox to achieve columns.

In the past I could determine the text width in Twips and

pad in required number of blank spaces. But now I'm using

VB2005 and it does not support Twips. How do I accomplish this?

Thanks

Jan 4 '06 #2
Thanks Ken, That procedure worked great.

Now I have tried it using a comboBox it also
worked except when an item is selected the
new format is lost. Is there a way to draw this
into the Editable portion of the ComboBox.

Tom
Jan 5 '06 #3

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

Similar topics

6
by: TJ | last post by:
I've got a calendar that is based on the concept of lots of blocks that are spans with float:left. I would like to be able to have a detail section on the right side of the screen, so that when...
9
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have...
3
by: Leonardo da Vinci | last post by:
Greetings gentlemen and ladies, I have a question: in Tkinter, how to align a Listbox entry (i.e. a line of text) to the right? Google did not show up the answer to my request. Thanks very much....
0
by: Sori Schwimmer | last post by:
Eric Brunel is right in that a Listbox won't provide any builtin help for this kind of things. Suppose you can afford a width of 10 chars. Suppose your lines are stored in the variable Lines - a...
3
by: acunnon | last post by:
I am trying to put together an login page my problem is getting the three items aligned to the middle verticaly without specifing a height to anything on the page. CSS html{ height:100%;...
6
by: BerkshireGuy | last post by:
On an unbound form, I have a combobox called 'cboproducttype' and a text box called 'txtamountappliedfor'. I have an Add button that I would like the user to be able to hit once a product and...
13
by: Stevo | last post by:
I've found that for IE6+, if you add the property text-align:center to a DIV, then *anything* inside it gets centered. That can be a table, an object/embed, another DIV, an image, or some text. ...
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>
1
by: panos100m | last post by:
I cant put the text (register,members,find) in the center of the three buttons using Internet explorer... Any help will be much appreciated (html code below) <td class="ct"> <!-- menu...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.