שתף באמצעות


Displaying multiple lines of data in a list box

Question

Sunday, April 1, 2012 9:19 PM

I'm new to Visual basic (which will be obvious from my question) and am writing program that does various calculations, then displays the results in a list box. the results, however, are all concatenated, end don't each appear on a new line. I tried adding a carriage return/line feed at the end buit the compiler rejected it.

I can't find any examples of creating a list box, and adding multiple entries (one for each time a loop is performed), nor of how to use columns to make the resulting display cosmetically pleasing.

Does anyone know where there are such examples?

Thanks

All replies (11)

Monday, April 2, 2012 6:46 PM ✅Answered | 1 vote

The linked code accesses the SubItems property explicity.

This is another version which I think is simpler to handle:

      Dim columns(2) As String

      For i = 1 To 10
         Dim Item As ListViewItem

         columns(0) = "First"
         columns(1) = "Second"
         columns(2) = "Third"

         Item = New ListViewItem(columns)

         lvw.Items.Add(Item)
      Next

Armin


Sunday, April 1, 2012 9:30 PM | 1 vote

If you want to add items at each iteration of the loop you can write code like the following

For i = 1 To 5
    ListBox1.Items.Add(i.ToString())
Next

Of course, this is a very simple code snippet, but you can use it as a starting point for your code.
If you need something aesthetically pleasing try using a ListView (http://msdn.microsoft.com/en-us/library/6dwb14tw(VS.90).aspx) instead.

Bye.

Luigi Bruno - Microsoft Community Contributor 2011 Award


Sunday, April 1, 2012 9:41 PM

If you call the Add method of the Listbox' Items property for each value, it should work:

      For i = 1 To 15
         ListBox1.Items.Add(i.ToString)
      Next

The MultiColumn property of the Listbox is not really helpful. It's not a grid-style control but it's for avoiding vertical scrolling. The code above with MultiColumn = True produces this output:

A ListView control is more suitable for multiple columns. Set it's View property to Details for this purpose.

For later you might consider using the DataGridView control.

Armin


Sunday, April 1, 2012 11:40 PM | 1 vote

Rav,

To add to what others here have said, have you considered using another control to display the results in? A listbox is hardly appropriate unless you have need to allow the user to select one of them and then [do something, I have no idea what] with them?

How about this: Use a stringbuilder, build the entire output text, then display it in a textbox set for multi lines? You could even use a label if you knew in advance how large to make it but either way -- a listbox for it doesn't really sound like the control that you want to use.

For what it's worth...


Monday, April 2, 2012 11:02 AM | 1 vote

Hello,

it will be better if u will use Listview or DataGridview, make a for loop and add rows and column according to ur need,

thanks,

-- Thanks, Amit kala Please remember to click “Mark as Answer” on the post that helps you, & to click “Unmark as Answer” if a marked post does not actually answer your question.


Monday, April 2, 2012 11:08 AM

I appreciate you agreeing with me. :-)

Armin


Monday, April 2, 2012 5:31 PM

Thanks guys for all the help... I've changed from a list box to a list view. I've set the list view in the definition with columns and the headings are appearing correctly, but how do I get each iteration of the loop to start a new line for the entry? Each loop of my code shows the results in column 1.

My code looks like:

For i = 1 to 50

lvResults.Items.Add(yyear(i))

lvResults.Items.Add(Income(i))

lvResults.Items.Add(Pension(i)

etc...

Next

I tried using LvResults.Items.Add(yyear(i), 0) to start the year in column 1, and lvResults.Items.Add (Income(i),1) to put the next data item in the next column but the compiler shows an error.

All of this is basic (no pun intended) to an experienced programmer, but I can't find anything that explains this in simple terms! Once I get this working, my next challenge is to print the same data!

Thanks again

Ray


Monday, April 2, 2012 5:38 PM | 1 vote

Have a look at this example:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview(VS.90).aspx

Armin


Monday, April 2, 2012 6:38 PM

Thanks Armin, I can now get all of the data in columns nicely, with a new line for each occurence of the loop. The only issue that I have, is that all of my data starts in the second column....  the code example inserts text into the 1st column as a literal, and I can't seem to use a variable.

How do I start in column 1 with my 1st variable?

Ray


Monday, April 2, 2012 8:51 PM | 1 vote

Ray,

I'm glad that you got your answer and obviously I was confused as to what you wanted.

Do you mind if I ask what exactly you're showing in the grid?


Monday, April 2, 2012 11:32 PM

Armin: Thank you.... thank you! It works.

Frank: Sorry if my question was confusing. My program starts off with the value of various savings (registered pension accounts, non-registered etc), then calculates for the next 50 years both the annual income from each investment and the  year end portfolio values.

I hope this makes sense.

My next issue is to figure out how to print the data in the array!

Once again, thanks for all your help. Withjout it, I would have spent days and days in frustration.

Ray