473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Listview: add new row

I am writing code to display in a listview. I have used a For/Next loop that should add a new line in the listview each time it loops. All i get now is one line when i execute the code. I tested the loop by sending the output to a message box and it showed a message box for every loop. So the loop works, but it is not displaying it in the listview correctly. I think I am missing something that would make the loop process go to the next row in the listview. Here is the code i have so far.

Dim intMultiplicand, intMultiplier, intProduct As Integer
Dim lviTable As New ListViewItem
Const strX = "X"
Const strEquals = "="
Dim objForm1 As New Form1

intMultiplicand = 1
intMultiplier = 1
For intMultiplicand = 1 To 12
intProduct = intMultiplicand * intMultiplier
lviTable.SubItems.Add(intMultiplicand)
lviTable.SubItems.Add(strX)
lviTable.SubItems.Add(intMultiplier)
lviTable.SubItems.Add(strEquals)
lviTable.SubItems.Add(intProduct)

For intMultiplier = 1 To 12
intProduct = intMultiplicand * intMultiplier
lviTable.SubItems.Add(intMultiplicand)
lviTable.SubItems.Add(strX)
lviTable.SubItems.Add(intMultiplier)
lviTable.SubItems.Add(strEquals)
lviTable.SubItems.Add(intProduct)

Next intMultiplier
Next intMultiplicand

lstTable.Items.Add(lviTable)

Please help.
Jan 16 '07 #1
12 9223
Killer42
8,435 Expert 8TB
I'm not that familiar with the listview, so don't take anything I say too seriously. But it looks to me as though all you're doing in these loops is adding lots of subitems (which I believe are equivalent to "columns") into one row.

I didn't really follow the code very well, but if you're expecting multiple rows, then wouldn't I expect to find the Items.Add inside the loop(s)?

(I must have a different version of the listview control here, as it doesn't even have .Items - it has .ListItems. So I may be talking complete garbage. :))
Jan 16 '07 #2
I'm not that familiar with the listview, so don't take anything I say too seriously. But it looks to me as though all you're doing in these loops is adding lots of subitems (which I believe are equivalent to "columns") into one row.

I didn't really follow the code very well, but if you're expecting multiple rows, then wouldn't I expect to find the Items.Add inside the loop(s)?

(I must have a different version of the listview control here, as it doesn't even have .Items - it has .ListItems. So I may be talking complete garbage. :))

Good point. The loop is not adding any new Items. The assignment requires the subitems, but you are right. I need something that will add Items in the loop. Thanks. This gives me something to work with now. I have been staring at this code all weekend. You guys are great!
Jan 16 '07 #3
Good point. The loop is not adding any new Items. The assignment requires the subitems, but you are right. I need something that will add Items in the loop. Thanks. This gives me something to work with now. I have been staring at this code all weekend. You guys are great!

BTW,

The code is supposed to produce the multiplication table throught the 12's in the listview. It should look like this...

1 X 1 = 1
1 X 2 = 2
1 X 3 = 3

There should be 144 Items when it is finished, and it should all populate at the touch of one button.
Jan 16 '07 #4
Killer42
8,435 Expert 8TB
Hm... in that case, could you see whether this works?
Expand|Select|Wrap|Line Numbers
  1. Dim intMultiplicand, intMultiplier, intProduct As Integer
  2. Dim lviTable As New ListViewItem
  3. Const strX = "X"
  4. Const strEquals = "="
  5. ' Dim objForm1 As New Form1
  6.  
  7. ' intMultiplicand = 1
  8. ' intMultiplier = 1
  9. For intMultiplicand = 1 To 12
  10.   ' intProduct = intMultiplicand * intMultiplier
  11.   ' lviTable.SubItems.Add(intMultiplicand)
  12.   ' lviTable.SubItems.Add(strX)
  13.   ' lviTable.SubItems.Add(intMultiplier)
  14.   ' lviTable.SubItems.Add(strEquals)
  15.   ' lviTable.SubItems.Add(intProduct)
  16.   For intMultiplier = 1 To 12
  17.  
  18.     ' Somehow clear/reset lviTable. I don't know how.
  19.  
  20.     intProduct = intMultiplicand * intMultiplier
  21.     lviTable.SubItems.Add (intMultiplicand)
  22.     lviTable.SubItems.Add (strX)
  23.     lviTable.SubItems.Add (intMultiplier)
  24.     lviTable.SubItems.Add (strEquals)
  25.     lviTable.SubItems.Add (intProduct)
  26.     lstTable.Items.Add (lviTable)
  27.   Next intMultiplier
  28. Next intMultiplicand
  29.  
Jan 16 '07 #5
I was thinking the same thing, but I got this error when I tried that.

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

Additional information: Cannot add or insert the item '' in more than one place. You must first remove it from its current location or clone it.

What does that mean?
Jan 16 '07 #6
Now I am getting 144 Items, but it is not displaying the subitems. Here is what I have changed.

Expand|Select|Wrap|Line Numbers
  1.   Dim intMultiplicand, intMultiplier, intProduct As Integer
  2.         Dim lviTable As New ListViewItem
  3.         Const strX = "X"
  4.         Const strEquals = "="
  5.         Dim objForm1 As New Form1
  6.  
  7.         intMultiplier = 1
  8.         For intMultiplicand = 1 To 12
  9.             lstTable.Items.Add(intMultiplicand)          
  10.            intProduct = intMultiplicand * intMultiplier
  11.             lviTable.SubItems.Add(strX)
  12.             lviTable.SubItems.Add(intMultiplier)
  13.             lviTable.SubItems.Add(strEquals)
  14.             lviTable.SubItems.Add(intProduct)
  15.             For intMultiplier = 2 To 12
  16.                 lstTable.Items.Add(intMultiplicand)
  17.                 intProduct = intMultiplicand * intMultiplier
  18.                 lviTable.SubItems.Add(strX)
  19.                 lviTable.SubItems.Add(intMultiplier)
  20.                 lviTable.SubItems.Add(strEquals)
  21.                 lviTable.SubItems.Add(intProduct)
  22.  
  23.             Next intMultiplier
  24.         Next intMultiplicand
  25.  
  26.         lstTable.Items.Add(lviTable)
Jan 16 '07 #7
Killer42
8,435 Expert 8TB
I was thinking the same thing, but I got this error when I tried that.

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Cannot add or insert the item '' in more than one place. You must first remove it from its current location or clone it.


What does that mean?
I think that means you tried to add two items with the same index. To me, that sounds as though you are heading in the right direction - after all, at least you are trying to add more than one item. :)

You might try just plugging in some unique string (Eg. the two numbers concatenated) as the index, and see what happens. I wish I had more experience with the listview...
Jan 16 '07 #8
You have been a great help. At least I am getting new results now. Thanks.
Jan 16 '07 #9
hello, you just trying load mutiplication table in the listview. In vb 6.0 no need to new operator for listview. and please surf in msdn for listview in vb 6.0 you get good description.

if you have any doubt in that example you can query me
Jan 16 '07 #10
Killer42
8,435 Expert 8TB
And make sure you let us know here, how things turn out. :)
Jan 16 '07 #11
THAT WAS IT!!!!!!! You can add subItems without declaring a variable for the ListViewItems. Here is the solution:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
  2.         Dim intMultiplicand, intMultiplier, intProduct As Integer
  3.         Const strX = "X"
  4.         Const strEquals = "="
  5.         Dim objForm1 As New Form1
  6.         Dim intCount As Integer
  7.  
  8.  
  9.         intCount = 0       
  10.             For intMultiplicand = 1 To 12
  11.             intMultiplier = 1
  12.             lstTable.Items.Add(intMultiplicand)
  13.             intProduct = intMultiplicand * intMultiplier
  14.             lstTable.Items(intCount).SubItems.Add(strX)
  15.             lstTable.Items(intCount).SubItems.Add(intMultiplier)
  16.             lstTable.Items(intCount).SubItems.Add(strEquals)
  17.             lstTable.Items(intCount).SubItems.Add(intProduct)
  18.             intCount += 1
  19.             For intMultiplier = 2 To 12
  20.                 lstTable.Items.Add(intMultiplicand)
  21.                 intProduct = intMultiplicand * intMultiplier
  22.                 lstTable.Items(intCount).SubItems.Add(strX)
  23.                 lstTable.Items(intCount).SubItems.Add(intMultiplier)
  24.                 lstTable.Items(intCount).SubItems.Add(strEquals)
  25.                 lstTable.Items(intCount).SubItems.Add(intProduct)
  26.                 intCount += 1           
  27.              Next intMultiplier
  28.         Next intMultiplicand 
I added a count in the loop that would add one every loop so i could tell the listview where to place the Item and SubItem. Then I changed my lsvTable.SubItems.Add() to lstTable.Items(intCount).SubItems.Add. I'm not sure if i explained this correctly, but THANK YOU GUYS VERY MUCH. IT WORKED.
Jan 16 '07 #12
Killer42
8,435 Expert 8TB
Excellent!

It's good to see you've resolved your problem.
Jan 18 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Anushya | last post by:
Hi I am using Listview and inherited listview control overriding WndProc & PreProcessMessage in ListView. I need this to customize listview to display only the page the user scrolls to. Since i...
0
by: Anushya | last post by:
Hi I am using Listview and inherited listview control overriding WndProc & PreProcessMessage in ListView. I need this to customize listview to display only the page the user scrolls to. Since i...
0
by: keith | last post by:
In a ListView control (two columns), I added a few ListView items. ListView listview=new ListView(); listview.Parent=this; listview.View=View.Details; listview.Columns.Add...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
7
by: BobAchgill | last post by:
I am trying to decide which of these controls to use to implement letting my user select a full row from MyList. The MyList has several columns which would be nice to sort by at run time. The...
5
by: John Devlon | last post by:
Hi, Does anyone know how to get a value of a second column of a selected item in Listview. I've create a listview and added this code Listview.Items.Clear() Listview.Columns.Clear()...
0
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
4
by: Brian Gaze | last post by:
I have created a ListView control and have bound this to a datasource. Within the ItemTemplate of the ListView I have added another ListViewControl which is databound in the code behind. The idea...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, In my application I need to periodically remove all current items from a ListView and add a new set into it. The following abbreviated code contains the basic idea: private void...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.