Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

Can you have two displaymembers in a list box?

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?
  • 2
1 Solution
"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?
pauldonsonAuthor Commented:
Datagrid is of no use, it is part of a navigation menu and needs the listbox look and feel. Thanks for the suggestion though.
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.
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now