0

コンテンツを表示するリストボックスを備えた Windows フォームアプリケーションを取得しました。ボタンをクリックすると、リストボックスからアイテムを上下に移動できるようにしたいと考えています。現時点では、保存されているリスト ボックスの項目はテキスト ファイルにあり、アプリケーションの起動時に構成クラスに読み込まれます。項目を上下に移動し、テキスト ファイル内の順序を変更するにはどうすればよいですか?

私の主な申請書コード:

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;
using System.IO;

namespace company1
{
    public partial class Form1 : Form
    {
        List<Configuration> lines = new List<Configuration>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            //Read in every line in the file
            using (StreamReader reader = new StreamReader("file.txt"))
            {
                string line = reader.ReadLine();
                while (line != null)
                {
                    string[] array = new string[] { "\\n" };
                    string[] parts = new string[3];
                    parts = line.Split(array, StringSplitOptions.RemoveEmptyEntries);
                    lines.Add(new Configuration(parts[0], int.Parse(parts[1]), int.Parse(parts[2])));
                    line = reader.ReadLine();
                }

            }
            listBox1.DataSource = lines;
            listBox1.DisplayMember = "CompanyName";
        }
    }
}

構成クラス ファイル

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace company1
{
    class Configuration
    {
        string _CompanyName;
        int _Employees;
        int _Half;

        public Configuration(string companyname, int number_of_Employees, int half)
        {
            _CompanyName = companyname;
            _Employees = number_of_Employees;
            _Half = half;
        }

        //program properties and validation
        public string CompanyName
        {
            set
            {
                _CompanyName = value;
            }
            get
            {
                return _CompanyName;
            }
        }// End of levelname validation

        //program properties and validation
        public int EmployeesNumber
        {
            set
            {
                _Employees = value;
            }
            get
            {
                return _Employees;
            }
        }// End of levelname validation

        //program properties and validation
        public int Half
        {
            set
            {
                _Half = value;
            }
            get
            {
                return _Half;
            }
        }// End of levelname validation
    }


}

それを機能させるために何日も努力してきました。

4

2 に答える 2

0

リストの拡張メソッドを定義して、インデックスに基づいてアイテムを移動できます。

public static class ExtensionClass
{
    public static void Move<T>(this List<T> list, int index1, bool moveDown = true)
    {
        if (moveDown)
        {
            T temp = list[index1];
            list[index1] = list[index1 + 1];
            list[index1 + 1] = temp;
        }
        else
        {
            T temp = list[index1];
            list[index1] = list[index1 - 1];
            list[index1 - 1] = temp;

        }
    }
}

次に、コードで次のことができます。

List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
            Console.WriteLine("Original List");
            foreach (int i in list)
            {
                Console.Write(i + ",");
            }
            Console.WriteLine(Environment.NewLine + "Move Index 2 Down");
            list.Move(2);
            foreach (int i in list)
            {
                Console.Write(i + ",");
            }
            Console.WriteLine(Environment.NewLine + "Move Index 3 Up");
            list.Move(3, false);
            foreach (int i in list)
            {
                Console.Write(i + ",");
            }

出力は次のようになります。

Original List
1,2,3,4,5,6,7,
Move Index 2 Down
1,2,4,3,5,6,7,
Move Index 3 Up
1,2,3,4,5,6,7,
于 2012-04-24T04:33:55.430 に答える
0
// change the items in source list
var tmpLine = lines[10];
lines[10] = lines[9];
lines[9] = tmpLine;

// refresh datasource of listbox
listBox1.DataSource = null;
listBox1.DataSource = lines;
于 2012-04-24T02:02:46.490 に答える