lambda linq expression ascending descending scrambled ListBox

2020 January 9 17:50:55

3333

annex

https://files.cnblogs.com/files/xe2011/ListBoxSort.rar


In this way you can see if the content is more than enough scroll bar flashes, and I think it is clear the contents of the list box, and then add content caused.

With not delete the list, re-assignment method can explain flicker, but the process time is too long.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListBoxDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<string> array = new List<string>();

        //初始化
        private void Form1_Load(object sender, EventArgs e) {
            string[] arr = { "a.m.", "a", "Smith", "Jones", "module", "zoology",
                                "youth", "Hsieh", "abdomen", "Xerox", "season",
                                "Luncheon", "transferable","macro","genuine" };
            array.AddRange(arr);

            listBox1.DataSource = array;

        }


        private void button升序_Click(object sender,EventArgs e) {
            //lambda
            var a = array.OrderBy(c => SortOrder.Ascending).ToArray();

            //linq
            //var a = from x in array orderby x ascending select x;
            listBox1.DataSource =a;
        }

        private void button降序_Click(object sender,EventArgs e) {
            //lambda
            var b = array.OrderBy(c => SortOrder.Descending).Reverse().ToArray();

            //linq
            //var a = from x in array orderby x descending select x;


            listBox1.DataSource = b;
        }

        private void button乱序_Click(object sender,EventArgs e) {
            //lambda
            //var a = array.OrderBy(c => Guid.NewGuid()).ToArray();

            //linq
            var a = from x in array orderby Guid.NewGuid()  select x;
            listBox1.DataSource = a.ToArray();
        }
    }
}

Guess you like

Origin www.cnblogs.com/xe2011/p/12172776.html