Solved

Keep constant position / Change position with ListView in C#

Posted on 2008-06-18
4
1,062 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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