Click here to Skip to main content
15,885,213 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a 20 elements array and I want to add them to a listbox is a specific order. I have a counter for adding elements on the array so I want the list box to show the last element added on top and then the one before it.
For Example if my last element was added when the counter was 4 I want the element to be shown on the list box in this order
{4,3,2,1,0,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5}


What I have tried:

C#
string [] ListaServizi1 = new string[] { item, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18, item19, item20 };
                            if (historylistBox1.Items.Count == 20) 
                            {
                                historylistBox1.Items.Clear();
                            }
                            int startfrom =  tcounter;
                            
                          int  startover = 0;
                         int   index = 0;
                            for (int i=0; i <= 20; i++)
                            {
                                if (startfrom<listaservizi1.length mode="hold" />                                {
                                    index++;
                                    historylistBox1.Items.Add(index + "."+ListaServizi1[startfrom]);
                                    startfrom++;
                                   
                                 }
                                
                                else if(startfrom >= ListaServizi1.Length)
                                {
                                    index++;
                                    historylistBox1.Items.Add(index + "." + ListaServizi1[startover]);
                                    startover++;
                                    startfrom++;
                                  

                                }
Posted
Updated 21-Feb-16 23:55pm
v2

On February 3, I posted a solution here for a "generic stack" that you could easily adapt ... without modification ... to your task: [^].

Using the code in that example, your stack might be declared like this:
// in Form Scope

// define your Array of Strings ...

private const int StringStackDepth = 20;

private StackT<string> stringStack = new StackT<string>(StringStackDepth);

private BindingSource bindSource = new BindingSource();

// in a Method or EventHanlder
private void Form1_Load(object sender, EventArgs e)
{
    foreach (string s in ListaServizi1)
    {
        StringStack.Push(s);    
    }
    
    bindSource = new BindingSource();
    bindSource.DataSource = StringStack;
    listBox1.DataSource = bindSource;
}
Now the "hard part" here is getting the ListBox to update when you have modified the Stack. Here's an example of what you have to do:
C#
StringStack.Push("item 21");
bindSource = new BindingSource();
bindSource.DataSource = StringStack;
listBox1.DataSource = bindSource;
I suggest you consider the possible "cost" (computation ? memory use ?) making many frequent updates of the Stack ... note that I have never tried to really evaluate this technically.

If I were using this generic Stack in this way, I'd probably modify its code to raise an Event when the data changed; then the user would subscribe to that event, and perform the UI update in that EventHandler. This would be easy to do.

Of course, I might also be associating stack values with some other object/entity in a Dictionary, or other data structure, and then setting the 'ValueMember of the ListBox.
If you are using a ListView, binding is much more difficult, but you can find resources on how to do it, like here, on CodeProject: [^], or here: [^].

I assume this is a Windows Forms Project, and you are using a ListBox, or ListView Control: if that's not correct please revise your post.

Have you studied how to do binding, and use 'DataSource, 'ValueMember, and 'DisplayMember tools ?
 
Share this answer
 
v2
Comments
Maciej Los 22-Feb-16 5:26am    
Sure, a 5!
Very interesting solution!
BillWoodruff 22-Feb-16 6:07am    
it's improved now, Maciej, and thanks for your vote !
Maciej Los 22-Feb-16 6:08am    
You're very welcome, Bill.
coding star 22-Feb-16 5:28am    
Yes ,Your right it is a windows form and I am using a listbox control. Can you please Provide me with the link of your post that you've mentioned above
Maciej Los 22-Feb-16 5:58am    
On the end of the first line you'll find [^]. This is a link to Bill's solution.
This is not really a sort problem, you want the items in reverse order and with a rotation.

First of all the pitfall:
C#
for (int i=0; i <= 20; i++)
is wrong because it loop 21 times. Use i<20

ListaServizi1[i] read items in order.
ListaServizi1[19-i] read items in reverse order.
ListaServizi1[(5+19-i)%20] read items in reverse order with rotation.

Your code simplified should look like:
C#
for (int i=0; i < 20; i++)
{
        historylistBox1.Items.Add((i+1) + "."+ListaServizi1[(5+19-i)%20]);

    }
 
Share this answer
 
v3
Comments
Maciej Los 22-Feb-16 6:05am    
It looks pretty good, but does not resolve an "order" issue. Why? An algorithm to sort data is OK, but it produces string-based-output, like: {"0.A", "1.A", ..., "20.A"}, etc. So...
Patrice T 22-Feb-16 6:13am    
My code should exactly what the original code does.
What is the problem ?
By the way it is {"1.A", ..., "20.A"}
Maciej Los 22-Feb-16 6:45am    
I wanted to say that order of elements in the ListBox will be wrong: {"1.A", "11.A", "12.A", ..., "2.A", "20.A"}. Got it?
Patrice T 22-Feb-16 7:01am    
Do you mean that historylistBox1.Items.Add is sorting the items ?
Maciej Los 22-Feb-16 7:09am    
OMG, i'm an idiot! You're right! ListBox adds new item on the end of list!
5ed!
This is asked before but lucky this is not stackoverflow

C# - Sorting items in a listbox...![^]
 
Share this answer
 
Comments
coding star 22-Feb-16 3:37am    
The questions' titles are the same ,but the sorting ways are totally different.
Beginner Luck 22-Feb-16 3:38am    
because it is using linq method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900