コンテンツを表示するリストボックスを持つ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
}
}
任意のヘルプ感謝、それは仕事を取得し日間しようとして。
拡張メソッドを構成クラス内に配置しますか?またはメインプログラム? – Raphael1
拡張メソッドは、すべてのコードで使用できるクラスに置くことができます。汎用メソッドであり、すべてのタイプのリストで機能します。あなたの設定クラスについてはわかりませんが、コードを使って利用できる場合は、そこに置くことができます。もしそれをメインプログラムの中に入れたら、それは特定のネームスペース – Habib
@ Raphael1にしか利用できないかもしれません、それはあなたのために働いたのですか? – Habib