Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Keep constant position / Change position with ListView in C#

Posted on 2008-06-18
4
Medium Priority
?
1,069 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:Joel Coehoorn
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 400 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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

876 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