2009-04-30 65 views

回答

69

這是一個快速下載和骯髒的應用程序。基本上我用一個按鈕和一個ListBox創建了一個Form。點擊按鈕後,ListBox會填入未來20天的日期(必須使用某些東西進行測試)。然後,它允許拖放列表框內重新排序:

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      this.listBox1.AllowDrop = true; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      for (int i = 0; i <= 20; i++) 
      { 
       this.listBox1.Items.Add(DateTime.Now.AddDays(i)); 
      } 
     } 

     private void listBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (this.listBox1.SelectedItem == null) return; 
      this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move); 
     } 

     private void listBox1_DragOver(object sender, DragEventArgs e) 
     { 
      e.Effect = DragDropEffects.Move; 
     } 

     private void listBox1_DragDrop(object sender, DragEventArgs e) 
     { 
      Point point = listBox1.PointToClient(new Point(e.X, e.Y)); 
      int index = this.listBox1.IndexFromPoint(point); 
      if (index < 0) index = this.listBox1.Items.Count-1; 
      object data = e.Data.GetData(typeof(DateTime)); 
      this.listBox1.Items.Remove(data); 
      this.listBox1.Items.Insert(index, data); 
     } 
3

如果您從未實施過拖放操作,首次需要幾個小時才能完成正確的操作,並且必須通讀文檔。特別是即時反饋和恢復列表,如果用戶取消操作需要一些想法。將行爲封裝到可重用的用戶控件中也需要一些時間。

如果您從來沒有做過拖放操作,請查看MSDN上的drag and drop example。這將是一個很好的起點,它可能需要半天才能完成。

0

另一種方法是使用the list-view控件,它是Explorer用來顯示文件夾內容的控件。這是比較複雜的,但實現項目拖動你。

+0

...,不支持簡單的事情,象綁定列表項:( – nathanchere 2013-09-12 22:13:37

+0

...也不在列表或詳細視圖中顯示時不會拖的工作。 – nathanchere 2013-09-14 02:54:26

2

7晚年。但對於任何人來說,這裏是代碼。

private void listBox1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (this.listBox1.SelectedItem == null) return; 
     this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move); 
    } 

    private void listBox1_DragOver(object sender, DragEventArgs e) 
    { 
     e.Effect = DragDropEffects.Move; 
    } 

    private void listBox1_DragDrop(object sender, DragEventArgs e) 
    { 
     Point point = listBox1.PointToClient(new Point(e.X, e.Y)); 
     int index = this.listBox1.IndexFromPoint(point); 
     if (index < 0) index = this.listBox1.Items.Count - 1; 
     object data = listBox1.SelectedItem; 
     this.listBox1.Items.Remove(data); 
     this.listBox1.Items.Insert(index, data); 
    } 

    private void itemcreator_Load(object sender, EventArgs e) 
    { 
     this.listBox1.AllowDrop = true; 
    }