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


} 

任意のヘルプ感謝、それは仕事を取得し日間しようとして。

答えて

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

+0

拡張メソッドは、すべてのコードで使用できるクラスに置くことができます。汎用メソッドであり、すべてのタイプのリストで機能します。あなたの設定クラスについてはわかりませんが、コードを使って利用できる場合は、そこに置くことができます。もしそれをメインプログラムの中に入れたら、それは特定のネームスペース – Habib

+0

@ Raphael1にしか利用できないかもしれません、それはあなたのために働いたのですか? – Habib

関連する問題