Add Textbox.Lines To Listview....How???

TeachMe

Member
Joined
Jun 27, 2012
Messages
16
Programming Experience
Beginner
Hi Friends,

I have 3 textboxes with lines that I'm trying to add to 3 columns in a Listivew1. I am trying to get the TextBox1.Lines into Listview1 Column1, TextBox2.Lines into Listview1 Column2, & TextBox3.Lines into Listview1 Column3.

I have tried to put the TextBox.Lines into an array & send them into each column in the listview, but it's not working. Here's the code that I'm using:

VB.NET:
Dim Item As ListViewItem

For Each Product1 In TextBox1.Lines
   Item = ListView1.Items.Add(Product1)
Next

For Each Product2 In TextBox2.Lines
    Item.SubItems.Add(Product2)
Next

For Each Product3 In TextBox3.Lines
    Item.SubItems.Add(Product3)
Next

Here's what my form looks like:
ListiviewAddLines.PNG


SO I NEED:
Numbers 1,2,3,4,& 5 from TextBox1.Lines should go into Column1

Numbers 6,7,8,9,&10 from TextBox2.Lines should go into Column2

Numbers 11,12,13,14,& 15 from TextBox3.Lines should go into Column3



I would appreciate a simple code snippet. Thanks again.
 
That's a rather bad UI to begin with because there's no specific reason that each TextBox should have the same number of lines. I would strongly suggest that you rethink that design.

That said, here's the steps that I would recommend:

1. Declare three variables for the arrays and assign the Lines properties of the TextBoxes to them.
2. Get the upper bound of each array.
3. Get the maximum of those three upper bounds.
4. Run a For loop from 0 to that greatest upper bound.
5. In the loop, determine whether the current counter value is a valid index for each array, getting the element at that index or Nothing otherwise.
6. Create a ListViewItem, populate it with the three values and Add it to the ListView.

This is how you should solve problems like this, i.e. come up with a set of steps first (the algorithm) and then write code to implement those steps. You can always know whether your code is correct because you always have the steps to refer back ro instead of plucking code out of the air.
 
Back
Top