Solved

Keep constant position / Change position with ListView in C#

Posted on 2008-06-18
4
1,057 Views
Last Modified: 2013-12-16
Hi,

I have a ListView/ListBox (same style) box in C#, and I want to be able to keep a certain item (I can always identify this item, as it is unique) at a constant position, that is, at the bottom of the list.

I am constantly adding items to the list, so if there is a way to change the position in the list I can work something out from there.

Using Visual C# Express.

Thanks.
0
Comment
Question by:dh-s
  • 2
4 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21812787
You'll have to remove it and re-add it everytime you add another item to the list.
0
 
LVL 3

Assisted Solution

by:BitRunner303
BitRunner303 earned 100 total points
ID: 21812896
As long as you can identify the item, then you can iterate through it's Items collection.  Then in your code handling the adding of items, you could do something like this (there may need to be some tweaking done to this code):

private void AddItem(string NewItem)
{
            decimal foundID = -1;
            foreach (ListViewItem myItem in this.listView1.Items)
            {
                if (myItem.SubItems[0].Text == "My Unique ID")
                {
                    foundID = myItem.Index;
                }
            }
            if (foundID > -1)
            {
                ListViewItem tmpItem = (ListViewItem)(this.listView1.Items[foundID].Clone());
                this.listView1.Items.RemoveAt(foundID);
                this.listView1.Items.Add(NewItem);
                this.listView1.Items.Add(tmpItem);
            }
}
0
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 150 total points
ID: 21812920
You can user "ListView.Items.Insert" to add new items at a specific position, instead of using the "Add" method to append an new item at the end of the list.

So, if you always want to insert new items at the first position, and move the other items down, then use somthing like:

ListView1.items.Insert(0, newListViewitem))
0
 
LVL 3

Expert Comment

by:BitRunner303
ID: 21813760
Yep, sorry forgot about that, in this case you'd want to do:

            this.listView1.Items.Insert(this.listView1.Items.Count, new ListViewItem(this.textBox1.Text));

Something like that (or supply your own new ListViewItem).
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question