2012-04-24 34 views
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 
    } 


} 

知道的任何幫助,一直試圖幾天才能得到它的工作。

回答

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; 
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, 
+0

我是否將擴展方法放在配置類中,或主程序? – Raphael1 2012-04-24 07:31:37

+0

您可以將擴展方法放在任何可用於所有代碼的類中,它是一種通用方法,可用於所有類型的列表。我不確定你的配置類,但是如果它通過代碼可用,你可以把它放在那裏。如果你把它放在主程序中,那麼它可能只適用於那個特定的命名空間 – Habib 2012-04-24 08:45:31

+0

@ Raphael1,它是否適合你? – Habib 2012-04-24 10:44:06

相關問題