Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lining up the columns in a ListBox

Status
Not open for further replies.

EBECK

Programmer
Nov 11, 2004
14
US
I'm developing an application for a homeless shelter organization. The application uses a list box (I'd really prefer to use a list box) to display their Guest records. I've been having some difficulty having the columns (fields) line up, as shown in the partial display below. (The column headings are Last Name, First Name, Middle Initial and SS Number.)

AARON CHARLES XXX-XX-XXXX
AARON JACK C XXX-XX-XXXX
AARON TEAURAUN A XXX-XX-XXXX
ABBOTT HERBERT C XXX-XX-XXXX
ABBOTT JOHN E XXX-XX-XXXX
ABBOTT KENNETH XXX-XX-XXXX
ABBOTT RICHARD D XXX-XX-XXXX
ABDOURHMAN MAHMOUD XXX-XX-XXXX
ABELT FREDERICK A XXX-XX-XXXX

The code used to display these records follows.

' Load the Guest records into the list box.
With dsDataSet.Tables("tblGuest")
For intIndex = 0 To .Rows.Count - 1
lstRecords.Items.Insert(intIndex, _
.Rows(intIndex).Item("LastName") & Chr(9) & _
.Rows(intIndex).Item("FirstName") & Chr(9) & Chr(9) & _
.Rows(intIndex).Item("MiddleInitial") & Chr(9) & Chr(9) & _
.Rows(intIndex).Item("SSNumber"))
Next
End With


I've tried checking the length of the FirstName field and concatenating one tab (chr(9)) for longer names instead of two, but that doesn't always work (see RICHARD and MAHMOUD above; their length is the same, but MAHMOUD's name is a bit wider (with the .Net font used), so his SS Number jumps to the next tab column).

Do you have any ideas how I could line up the columns? Thank you in advance for your time and consideration!

Ed
 
Not really what the listbox is ideally suited for, and I think the only way you will get any decent results from it, is to override the drawitem event.

Alternatively you could always use a Grid control, or even the ListView control


Sweep
...if it works dont mess with it
 
Two ways.... use tabs, or a font that has characters thats the same length.

- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
Quick reading again (didnt notice you used tabs already), so its only way... use characters thats the same length.


- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
To give an idea how to override the DrawItem method (in Listboxes and Combos), add a ListBox to a form and set its
Drawmode property to OwnerDrawnFixed

Then simply call the zbuildlist method, which adds items to the list, and the drawitem method, used the graphics drawstring method to create pseudo list columns.

Code:
Private Sub zBuildList()

    xList.Items.Add("Joe,Bloggs,01132 33333")
    xList.Items.Add("Peter,Smith,01142 762623")
    xList.SelectedIndex = 0

End Sub

Private Sub xList_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles xList.DrawItem

If xList.Items.Count = 0 Then Exit Sub

' Draw the background of the ListBox control for each item.
e.DrawBackground()

' Create a new Brush
Dim myBrush As Brush

' Get List Item
Dim sDisplay As String = xList.Items(e.Index).ToString

' Set Brush Color depending if Item Selected
myBrush = Brushes.Black
If InStr(e.State.ToString, "Selected") <> 0 Then myBrush = Brushes.White

' Draw Text Field Values
Dim iLoop As Integer = 0
Dim LeftAddition As Integer = 0
Dim sWidths As String = "50,80,80"
Dim sWide As String() = sWidths.Split(","c)

For Each s As String In sDisplay.Split(","c)
    e.Graphics.DrawString(s, e.Font, myBrush, New   RectangleF(e.Bounds.X + LeftAddition, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
    LeftAddition += CInt(sWide(iLoop))
    iLoop += 1
Next

' If the ListBox has focus, draw a focus rec on selected item
e.DrawFocusRectangle()

End Sub


Sweep
...if it works dont mess with it
 
I agree with Sweep's first comment - use a listview control - probably easier to code as well!!??
 
Thank you to Sweep, SMURF and Bruce for your feedback. I appreciate the help and direction you gave. I ended up replacing the ListBox with the ListView. It works great! (Had I not put so much time into looking into the ListView I would have tried overriding the DrawItem code you provided, Sweep. It looks like that would do a pretty good job too.)

Anyway, thanks again!

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top