且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#排序列表框中的元素

更新时间:2022-10-22 21:08:45

2月3日,我在这里发布了一个通用解决方案堆栈你可以很容易地适应...而不需要修改...你的任务:[ ^ ]。



使用该示例中的代码,您的堆栈可能声明如下:
 //在表单范围内

//定义你的字符串数组...

private const int StringStackDepth = 20;

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

private BindingSource bindSource = new BindingSource();

//在Method或EventHanlder
private void Form1_Load(object sender,EventArgs e)
{
foreach(ListaServizi1中的字符串s)
{
StringStack.Push(s);
}

bindSource = new BindingSource();
bindSource.DataSource = StringStack;
listBox1.DataSource = bindSource;
}

现在,这里的难点是在修改堆栈时让ListBox更新。以下是您必须执行的操作的示例:

 StringStack.Push(  item 21 跨度>); 
bindSource = new BindingSource();
bindSource.DataSource = StringStack;
listBox1.DataSource = bindSource;

我建议你考虑可能的成本(计算?内存使用?)进行堆栈的许多频繁更新...请注意,我从未尝试过从技术上来说,这真的很有价值。



如果我以这种方式使用这个通用堆栈,我可能会修改其代码以在数据发生变化时引发事件;然后用户将订阅该事件,并在该EventHandler中执行UI更新。这很容易做到。



当然,我也可能将堆栈值与字典或其他数据结构中的其他对象/实体相关联,然后设置ListBox的ValueMember。

如果你使用ListView,绑定要困难得多,但你可以在CodeProject上找到有关如何操作的资源,如此处所示:[ ^ ],或者这里:[ ^ ]。



我假设这是一个Windows窗体项目,你使用的是ListBox或ListView控件:如果这不正确请修改你的帖子。



您是否研究过如何进行绑定,并使用'DataSource',ValueMember和'DisplayMember工具?


这不是一个真正的排序问题,您希望项目反向顺序和旋转。



首先是陷阱:

  for  int  i =  0 ;我<  =  20 ; i ++)

错误,因为它循环21次。使用 i< 20



ListaServizi1 [i] 按顺序阅读物品。

ListaServizi1 [19-i] 按相反顺序读取物品。

ListaServizi1 [(5 + 19-i)%20] 以旋转的相反顺序读取项目。



您的代码简化应该看起来例如:

  for  int  i =  0 ; i <   20 ; i ++)
{
historylistBox1.Items.Add((i + 1)+ 。 跨度> + ListaServizi1 [(5 + 19-i)的20%]);

}


之前有人询问过,但幸运的是这不是***



C# - 对列表框中的项目进行排序......! [ ^

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:

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++;
                                  

                                }

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:

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 ?


This is not really a sort problem, you want the items in reverse order and with a rotation.

First of all the pitfall:
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:

for (int i=0; i < 20; i++)
{
        historylistBox1.Items.Add((i+1) + "."+ListaServizi1[(5+19-i)%20]);

    }


This is asked before but lucky this is not ***

C# - Sorting items in a listbox...![^]