文章編號: 822483 - 上次校閱: 2007年11月26日 - 版次: 2.9

在清單檢視控制項並不支援拖放功能的項目在執行階段

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

徵狀

如果 Microsoft Windows 表單上的 清單檢視 控制項中加入項目,您無法藉由拖曳 清單檢視 控制項中的項目,在執行階段重新排列項目。

發生的原因

清單檢視 控制項並不支援拖放功能的項目在執行階段,就會發生這個問題。

其他可行方案

如果要解決這個問題,提供至 ListView 控制項拖曳並放功能,如下所示:
  1. 將下列程式碼加入至 ListView1_ItemDrag 事件處理常式。

    Microsoft Visual Basic.NET 或 Microsoft Visual Basic 2005 程式碼
     'Begins a drag-and-drop operation in the ListView control.
    ListView1.DoDragDrop(ListView1.SelectedItems, DragDropEffects.Move)
    Microsoft Visual C#.NET 或 Microsoft Visual C# 2005年的程式碼
    //Begins a drag-and-drop operation in the ListView control.
    listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Move);
  2. 將下列程式碼加入至 ListView1_DragEnter 事件處理常式。

    Visual Basic.NET 或 Visual Basic 2005 程式碼
    Dim i As Integer
    For i = 0 To e.Data.GetFormats().Length - 1
    If e.Data.GetFormats()(i).Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection") Then
        'The data from the drag source is moved to the target.
        e.Effect = DragDropEffects.Move
    End If
    Next
    Visual C#.NET 或 Visual C# 2005年的程式碼
    int len=e.Data.GetFormats().Length-1 ;
    int i;
    for (i = 0 ; i<=len ;i++)
    {
        if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
        {
            //The data from the drag source is moved to the target.	
            e.Effect = DragDropEffects.Move;
        }
    }
    
  3. 將下列程式碼加入至 ListView1_DragDrop 事件處理常式。

    Visual Basic.NET 或 Visual Basic 2005 程式碼
    'Return if the items are not selected in the ListView control.
    If ListView1.SelectedItems.Count = 0 Then Return
    'Returns the location of the mouse pointer in the ListView control.
    Dim p As Point = ListView1.PointToClient(New Point(e.X, e.Y))
    'Obtain the item that is located at the specified location of the mouse pointer.
    Dim dragToItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)
    If dragToItem Is Nothing Then Return
    'Obtain the index of the item at the mouse pointer.
    Dim dragIndex As Integer = dragToItem.Index
    Dim i As Integer
    Dim sel(ListView1.SelectedItems.Count) As ListViewItem
    For i = 0 To ListView1.SelectedItems.Count - 1
        sel(i) = ListView1.SelectedItems.Item(i)
    Next
    For i = 0 To ListView1.SelectedItems.Count - 1
        'Obtain the ListViewItem to be dragged to the target location.
        Dim dragItem As ListViewItem = sel(i)
        Dim itemIndex As Integer = dragIndex
        If itemIndex = dragItem.Index Then Return
        If dragItem.Index < itemIndex Then
           itemIndex = itemIndex + 1
        Else
           itemIndex = dragIndex + i
        End If
        'Insert the item in the specified location.
        Dim insertitem As ListViewItem = dragItem.Clone
        ListView1.Items.Insert(itemIndex, insertitem)
        'Removes the item from the initial location while 
        'the item is moved to the new location.
        ListView1.Items.Remove(dragItem)
    Next
    
    Visual C#.NET 或 Visual C# 2005年的程式碼
    //Return if the items are not selected in the ListView control.
    if(listView1.SelectedItems.Count==0)
    {
       return;
    }
    //Returns the location of the mouse pointer in the ListView control.
    Point cp = listView1.PointToClient(new Point(e.X, e.Y));
    //Obtain the item that is located at the specified location of the mouse pointer.
    ListViewItem dragToItem = listView1.GetItemAt(cp.X, cp.Y);
    if(dragToItem==null)
    {
        return;
    } 
    //Obtain the index of the item at the mouse pointer.
    int dragIndex = dragToItem.Index;
    ListViewItem[] sel=new ListViewItem [listView1.SelectedItems.Count];
    for(int i=0; i<=listView1.SelectedItems.Count-1;i++)
    {
         sel[i]=listView1.SelectedItems[i];
    }
    for(int i=0; i<sel.GetLength(0);i++)
    { 
        //Obtain the ListViewItem to be dragged to the target location.
        ListViewItem dragItem = sel[i];
        int itemIndex = dragIndex;
        if(itemIndex==dragItem.Index)
       {
            return;
       }
       if(dragItem.Index<itemIndex)
           itemIndex++;
      else
           itemIndex=dragIndex+i;
       //Insert the item at the mouse pointer.
       ListViewItem insertItem = (ListViewItem)dragItem.Clone();
       listView1.Items.Insert(itemIndex, insertItem);
       //Removes the item from the initial location while 
       //the item is moved to the new location.
       listView1.Items.Remove(dragItem);
    }
    
  4. 在 [建置] 功能表上按一下 建置方案
  5. 在 [偵錯] 功能表上按一下 [開始]。

狀況說明

這種行為是經過設計規劃的。

其他相關資訊

重製行為的步驟

  1. 啟動 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005。
  2. 使用 Visual 基本.NET、 Visual Basic 2005、 Visual C# 2005 或 Visual C#.NET,以建立新的 Windows 應用程式。

    依照預設值,會建立 Form1。
  3. 清單檢視 控制項加入至 Form1。
  4. ListView1,] 上按一下滑鼠右鍵,然後按一下 [內容]。
  5. 在 [屬性] 對話方塊按一下 [項目] 屬性,然後再按一下省略符號按鈕 (...)]。
  6. ListViewItem 集合編輯器 對話方塊] 方塊中,按一下 新增 四次。
  7. 將第一個 ListViewItem 控制項的 Text 屬性設定至 Item1Item2 設定第二個 ListViewItem 控制項的 [文字] 屬性,將第三個 ListViewItem 控制項的 Text 屬性設定至 Item3,並且再將第四個 ListViewItem 控制項的 Text 屬性設定至 Item4
  8. 在 [ListViewItem 集合編輯器 對話方塊] 方塊中,按一下 [確定]
  9. 在 [屬性] 對話方塊的 ListView1,將 檢視 屬性設定為 [清單]。
  10. 將的 ListView1AllowDrop 屬性設定為 True
  11. 在 [建置] 功能表上按一下 建置方案
  12. 在 [偵錯] 功能表上按一下 [開始]。
  13. 請試著將 Item1 拖曳到 清單檢視 控制項中的新位置。

    您無法拖曳 Item1

?考

如需詳細資訊請造訪下列 Microsoft 網站]:
Control.DoDragDrop 方法
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(vs.71).aspx)
Control.DragDrop 事件
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragdrop(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragdrop(vs.71).aspx)
如需詳細資訊按一下 [下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項]:
822482? (http://support.microsoft.com/kb/822482/ ) 如何: 藉由使用 Microsoft Visual Basic.NET 提供在 Microsoft Windows 應用程式中的拖曳並放功能


這篇文章中的資訊適用於:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 標準版
  • Microsoft Visual C# .NET 2002 Standard Edition
關鍵字:?
kbmt kbvs2005swept kbvs2005applies kbforms kbwindowsforms kblistview kbdragdrop kbctrl kbcontrol kbprb KB822483 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:822483? (http://support.microsoft.com/kb/822483/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。