Solved

Keep constant position / Change position with ListView in C#

Posted on 2008-06-18
4
1,048 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now