Can you have two displaymembers in a list box?

Posted on 2006-05-15
Last Modified: 2010-04-23
Sounds a bit barmy but I wondered if this was possible, I am wanting an effect similar to what can be achieved in MS Access by specifying the number of columns in a list box.

I have a listbox that has its datasource as the results of a stored procedure, I would like two columns from the stored procedure displayed in the list box (I have tried concatanating them in the SP but they aren't formatted in columns then).

Can anyone suggest a work around please?
Question by:pauldonson
    LVL 17

    Expert Comment

    "I would like two columns from the stored procedure displayed"

    I take it a datagrid is out of the question?  Normally if you have multiple columns to display, you would move to a datagrid.  Is there a reason why you choose to stick with a listbox?

    Author Comment

    Datagrid is of no use, it is part of a navigation menu and needs the listbox look and feel. Thanks for the suggestion though.
    LVL 20

    Accepted Solution

    You can get close, but not quite precise, by putting spaces between the two columns of information. The reason it won't be precise is that you can't put a fraction of a space in (I'm assuming you'll be using a proportional-space font; if you use a fixed-width font like Courier New, it's simple to pad with the appropriate number of spaces). To do this, you have to measure the width of your string, using the appropriate font, then calculate how many spaces you need to add to get the second column to line up. For instance, you could do something like the following:

            Dim strList() As String = {"abc", "defgghjk", "1222"}
            Dim i, intSpace As Integer
            Dim sngSpace, sngLen As Single
            Dim b As Bitmap
            Dim g As Graphics

            ' Compute the string dimensions in the given font
            b = New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            g = Graphics.FromImage(b)
            sngSpace = g.MeasureString(" ", lbxTest.Font).Width
            For i = 0 To strList.GetUpperBound(0)
                sngLen = g.MeasureString(strList(i), lbxTest.Font).Width
                intSpace = System.Convert.ToInt32((70 - sngLen) / sngSpace)
                Dim sb As New System.Text.StringBuilder
                sb.Insert(0, " ", intSpace)
                lbxTest.Items.Add(strList(i) & sb.ToString & "X")

    "70" is an arbitrary width that serves more or less as a tab stop inside your text box. You'll probably want to change that, depending on your actual needs. lbxTest is the listbox that you're putting these into. I just put an "X" for the second column, but you'd put your second item there.

    Another option that might be worth looking at is a Listview, using Details view. That will show any number of columns, nicely aligned.
    LVL 20

    Expert Comment

    I think either of my two suggestions is viable, even if not a perfect match for the question, which I don't think has a perfect answer.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now