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.
12 9223
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. :))
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!
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.
Hm... in that case, could you see whether this works? - 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
-
-
' Somehow clear/reset lviTable. I don't know how.
-
-
intProduct = intMultiplicand * intMultiplier
-
lviTable.SubItems.Add (intMultiplicand)
-
lviTable.SubItems.Add (strX)
-
lviTable.SubItems.Add (intMultiplier)
-
lviTable.SubItems.Add (strEquals)
-
lviTable.SubItems.Add (intProduct)
-
lstTable.Items.Add (lviTable)
-
Next intMultiplier
-
Next intMultiplicand
-
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?
Now I am getting 144 Items, but it is not displaying the subitems. Here is what I have changed. - Dim intMultiplicand, intMultiplier, intProduct As Integer
-
Dim lviTable As New ListViewItem
-
Const strX = "X"
-
Const strEquals = "="
-
Dim objForm1 As New Form1
-
-
intMultiplier = 1
-
For intMultiplicand = 1 To 12
-
lstTable.Items.Add(intMultiplicand)
-
intProduct = intMultiplicand * intMultiplier
-
lviTable.SubItems.Add(strX)
-
lviTable.SubItems.Add(intMultiplier)
-
lviTable.SubItems.Add(strEquals)
-
lviTable.SubItems.Add(intProduct)
-
For intMultiplier = 2 To 12
-
lstTable.Items.Add(intMultiplicand)
-
intProduct = intMultiplicand * intMultiplier
-
lviTable.SubItems.Add(strX)
-
lviTable.SubItems.Add(intMultiplier)
-
lviTable.SubItems.Add(strEquals)
-
lviTable.SubItems.Add(intProduct)
-
-
Next intMultiplier
-
Next intMultiplicand
-
-
lstTable.Items.Add(lviTable)
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...
You have been a great help. At least I am getting new results now. Thanks.
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
And make sure you let us know here, how things turn out. :)
THAT WAS IT!!!!!!! You can add subItems without declaring a variable for the ListViewItems. Here is the solution: - Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
-
Dim intMultiplicand, intMultiplier, intProduct As Integer
-
Const strX = "X"
-
Const strEquals = "="
-
Dim objForm1 As New Form1
-
Dim intCount As Integer
-
-
-
intCount = 0
-
For intMultiplicand = 1 To 12
-
intMultiplier = 1
-
lstTable.Items.Add(intMultiplicand)
-
intProduct = intMultiplicand * intMultiplier
-
lstTable.Items(intCount).SubItems.Add(strX)
-
lstTable.Items(intCount).SubItems.Add(intMultiplier)
-
lstTable.Items(intCount).SubItems.Add(strEquals)
-
lstTable.Items(intCount).SubItems.Add(intProduct)
-
intCount += 1
-
For intMultiplier = 2 To 12
-
lstTable.Items.Add(intMultiplicand)
-
intProduct = intMultiplicand * intMultiplier
-
lstTable.Items(intCount).SubItems.Add(strX)
-
lstTable.Items(intCount).SubItems.Add(intMultiplier)
-
lstTable.Items(intCount).SubItems.Add(strEquals)
-
lstTable.Items(intCount).SubItems.Add(intProduct)
-
intCount += 1
-
Next intMultiplier
-
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.
Excellent!
It's good to see you've resolved your problem.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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()...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |