473,398 Members | 2,335 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,398 software developers and data experts.

array of ListViewItem - how to do it

grs
The following is a code example from the Microsoft MSDN.
My question is on the following three lines of code:

ListViewItem item1 = new ListViewItem("item1",0);
ListViewItem item2 = new ListViewItem("item2",1);
ListViewItem item3 = new ListViewItem("item3",0);

You would in most cases know the ListViewItem name or the number of items to
create (item1,item2 etc).
How would you create this individual rows programmatically ?

thanks
grs
=====================================
private void CreateMyListView()
{
// Create a new ListView control.
ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes.
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;

// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("item1",0);
// Place a check mark next to the item.
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
ListViewItem item3 = new ListViewItem("item3",0);
// Place a check mark next to the item.
item3.Checked = true;
item3.SubItems.Add("7");
item3.SubItems.Add("8");
item3.SubItems.Add("9");

// Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

// Create two ImageList objects.
ImageList imageListSmall = new ImageList();
ImageList imageListLarge = new ImageList();

// Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MyS mallImage1.bmp"));
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MyS mallImage2.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyL argeImage1.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyL argeImage2.bmp"));

//Assign the ImageList objects to the ListView.
listView1.LargeImageList = imageListLarge;
listView1.SmallImageList = imageListSmall;

// Add the ListView to the control collection.
this.Controls.Add(listView1);
}
Nov 17 '05 #1
6 13068
Hi grs,

Thanks for your post.

I do not think I understand you question. What does "How would you create
this individual rows programmatically ?" mean? In your code, I see that you
have added the listviewitems into ListView with listView1.Items.AddRange
method.

Can you show me your concern more concrete?

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #2
grs
"Jeffrey Tan[MSFT]" wrote:
Hi Jeffrey,

The problem is the hardcoded "item1". You do not know how many
items there will be so you can not create an ListViewItem object
in advance. I do not think you would want to to a create on
item1,item2, etc and restrict the number of ListViewItems
My question is on the following three lines of code:
ListViewItem item1 = new ListViewItem("item1",0);
---------------^(*) here is the problem
ListViewItem item2 = new ListViewItem("item2",1);


So the recap of my problem is that I do not know the number of rows that
exists how do I create the ListViewItems (which unless I am wrong
correspond to rows)

thanks
grs

grs

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #3
On Mon, 31 Oct 2005 17:15:10 -0600, "grs" <gs****@budgetext.com>
wrote:
You would in most cases know the ListViewItem name or the number of items to
create (item1,item2 etc).
How would you create this individual rows programmatically ?


First of all, it is usually best to seperate the code for creating the
ListView from the code adding the ListViewItems.

Here is the code I usually use to add ListViewItems. Assuming you have
a collection MyDataClassCollection that contains a number of
MyDataClass instances that can be used to generate ListViewItems.:

private void RefreshListView()
{
listView.BeginUpdate();

listView.Items.Clear();
AddItemsToListView();

listView.EndUpdate();
}
private void AddItemsToListView()
{
//Add to an ArrayList first because AddRange is a lot faster
//than Add when dealing with lots of elements.
//Also, there is no need to turn off the sorter when adding
//all the elements at once

ArrayList listItems = new ArrayList();

foreach (MyDataClass myDataItem in MyDataClassCollection)
{
// The if statement can be removed if all items in
// MyDataClassCollection should be added to the ListView.
if( ShouldDisplay(myDataItem) )
listItems.Add(CreateListViewItem(myDataItem));
}

// Adds the items to the ListView
listView.Items.AddRange(
(ListViewItem[]) listItems.ToArray( typeof(ListViewItem)));
}

// Generate a listviewitem by using the myDataItem instance.
private static ListViewItem CreateEpisodeListViewItem(MyDataClass
myDataItem)
{
ListViewItem item = new ListViewItem(
new string[]
{
myDataItem.StringProperty,
myDataItem.IntProperty.ToString(),
(myDataItem.ReferenceProperty == null) ? "" :
myDataItem.ReferenceProperty.ToString()
});
item.ForeColor = CalculateForeColor(myDataItem);

// It is usually a good idea to use the Tag property to attach
// the MyDataClass instance directly to the ListViewItem.
item.Tag = episode;

return item;
}

--
Marcus Andrén
Nov 17 '05 #4
Hi grs,

Thanks for your feedback.

Sorry, but I still do not understand you very well. I am not sure the
"item1" you mentioned refers to the ListViewItem reference or the first
parameter passed to ListViewItem constructor? Can you clarify this to me?
If you referred to the ListViewItem reference, I do not think this is a
problem, we can reuse the same reference to create many ListViewItem
instances, then add them into ListView.Items collection. The reference need
not correspond to ListViewItem instance one-by-one.
If you referred to the first parameter passed to ListViewItem, I just do
not understand why. This parameter identifies the text to be displayed in
ListView, if you want to add a new item, you SHOULD know which text you
want to display.

In normal situation, once we know the text we want to display in ListView,
we can construct a new ListViewItem instance and add it into ListView.Items
collection, no matter how many items you want to add, only one ListViewItem
reference is needed.

If I misunderstand you, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #5
grs
Jeffrey,

Your statement below cleared everything up for me. I seem to have a
problem with reusing references, I have had this mind road-block before.
Maybe someday I will get it.

thanks for turning me around.
grs
we can reuse the same reference to create many ListViewItem
instances, then add them into ListView.Items collection


grs

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #6
I am glad my reply can help you. If you need further help, please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kluch | last post by:
I am trying to select and focus single ListViewItem and can't seem to do so, here is my code: IEnumerator * itemList = mLsvMyList->Items->GetEnumerator(); while (itemList->MoveNext()) {...
8
by: Chad Miller | last post by:
lvProjects.Clear() lvProjects.View = View.Details lvProjects.FullRowSelect = True lvProjects.Columns.Add("Id", 50, HorizontalAlignment.Left) lvProjects.Columns.Add("Name", -1,...
1
by: Alan T | last post by:
I have a listview defined a coloumn at design time. And the code I use to add a listviewitem: ListViewItem lvi = new ListViewItem(user.Name); lvi.Tag = (Object)user.Id; ...
2
by: Kela | last post by:
An interesting problem: I have a ListView with LabelEdit set to TRUE. When I change the label, I want to make some decisions as to whether the ListViewItem (that's just been edited) should stay in...
0
by: garyusenet | last post by:
I am trying to create a form that will display a list of open internet explorer windows and allow the user to choose one of them. So far I have created an arraylist which contains all of the open...
13
by: deciacco | last post by:
How can I have access to the items collection of a listview control on my form from a background thread? I know I need delegates to update the listview control and I have those calls in the...
10
by: Gav | last post by:
I am trying to have a ListView to dispay a list of names and want to have an id stored within the list but not visable. I have tried to go about doing this by using the ListViewItem and setting...
1
by: elmbrook | last post by:
Hi I am trying to add an image to a listview box. I have the below code and according to me this should work. However, the image does not show at all. I can add an image when I manually add each...
1
by: Praveen Raj V | last post by:
Is it possible to disable the checkbox of the particular ListViewItem? I found code to check and uncheck the ListViewItem. ListViewItem lvi = new ListViewItem("One"); lvi.Checked = false; but...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.