Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Keep constant position / Change position with ListView in C#

Posted on 2008-06-18
4
Medium Priority
?
1,065 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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