ترتيب مجموعة winforms listbox باستخدام السحب و الإسقاط ؟

StackOverflow https://stackoverflow.com/questions/805165

  •  03-07-2019
  •  | 
  •  

سؤال

هذا هو عملية بسيطة?

أنا فقط كتابة سريعة hacky UI أداة داخلية.

أنا لا أريد أن تنفق عصر على ذلك.

هل كانت مفيدة؟

المحلول

إليك التطبيق سريع أسفل والقذرة. أساسا أنا خلق نموذج مع زر ومربع القائمة. في نقرة زر، ويحصل على مربع القائمة تعبئة مع تاريخ 20 يوما القادمة (وكان لاستخدام شيء فقط للاختبار). ثم، لأنها تتيح السحب والإسقاط داخل مربع القائمة لإعادة ترتيب:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.listBox1.AllowDrop = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= 20; i++)
            {
                this.listBox1.Items.Add(DateTime.Now.AddDays(i));
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.listBox1.SelectedItem == null) return;
            this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move);
        }

        private void listBox1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            Point point = listBox1.PointToClient(new Point(e.X, e.Y));
            int index = this.listBox1.IndexFromPoint(point);
            if (index < 0) index = this.listBox1.Items.Count-1;
            object data = e.Data.GetData(typeof(DateTime));
            this.listBox1.Items.Remove(data);
            this.listBox1.Items.Insert(index, data);
        }

نصائح أخرى

و7 سنوات في وقت متأخر. ولكن بالنسبة لشخص جديد، وهنا هو رمز.

private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (this.listBox1.SelectedItem == null) return;
        this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move);
    }

    private void listBox1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    private void listBox1_DragDrop(object sender, DragEventArgs e)
    {
        Point point = listBox1.PointToClient(new Point(e.X, e.Y));
        int index = this.listBox1.IndexFromPoint(point);
        if (index < 0) index = this.listBox1.Items.Count - 1;
        object data = listBox1.SelectedItem;
        this.listBox1.Items.Remove(data);
        this.listBox1.Items.Insert(index, data);
    }

    private void itemcreator_Load(object sender, EventArgs e)
    {
        this.listBox1.AllowDrop = true;
    }

وكانت المرة الأولى يستغرق بضع ساعات إذا لم تنفذ السحب والإسقاط، وتريد أن تحصل عليه الحق في القيام به، ويجب أن يقرأ من خلال مستندات. وخاصة ردود فعل فورية واستعادة قائمة إذا كان المستخدم إلغاء العملية تتطلب بعض الأفكار. سوف التغليف سلوك إلى عنصر تحكم مستخدم قابلة لإعادة الاستخدام يستغرق بعض الوقت أيضا.

إذا لم تفعل سحب وإسقاط على الإطلاق، إلقاء نظرة على هذا <وأ href = "http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop. ASPX "يختلط =" نوفولو noreferrer "> سحب وإسقاط سبيل المثال من MSDN. وهذا من شأنه أن يكون نقطة انطلاق جيدة ويجب أن يأخذك ربما نصف يوم للحصول على عمل شيء.

البديل هو استخدام قائمة عرض التحكم هو التحكم Explorer يستخدم لعرض محتويات المجلدات.هو أكثر تعقيدا ، ولكن تنفذ هذا البند سحب بالنسبة لك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top