0
我得到了一個帶有顯示內容的列表框的窗體應用程序,我希望能夠在單擊按鈕時上下移動列表框中的項目。此時列表框中的項目存儲在文本文件中,當應用程序啓動時,文本文件將加載到配置類中。我將如何向上/向下移動項目並更改文本文件中的順序?c#上下移動列表框中的項目
我主要的申請表代碼:
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 2012-04-24 07:31:37
您可以將擴展方法放在任何可用於所有代碼的類中,它是一種通用方法,可用於所有類型的列表。我不確定你的配置類,但是如果它通過代碼可用,你可以把它放在那裏。如果你把它放在主程序中,那麼它可能只適用於那個特定的命名空間 – Habib 2012-04-24 08:45:31
@ Raphael1,它是否適合你? – Habib 2012-04-24 10:44:06