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