Tips SistemThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
Jika Anda menambahkan item ke ListView kontrol pada formulir Microsoft Windows, Anda tidak dapat menyusun item dengan menyeret
item dalam ListView kontrol pada jangka waktu.
Untuk mengatasi masalah ini, memberikan drag-and-drop
fungsi untuk ListView mengontrol sebagai berikut:
Tambahkan kode berikut untukListView1_ItemDrag event handler.
Microsoft Visual Basic.NET atau Microsoft Visual Basic 2005 kode
'Begins a drag-and-drop operation in the ListView control.
ListView1.DoDragDrop(ListView1.SelectedItems, DragDropEffects.Move)
Microsoft Visual C#.NET atau Microsoft Visual C# 2005 kode
//Begins a drag-and-drop operation in the ListView control.
listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Move);
Tambahkan kode berikut untukListView1_DragEnter event handler.
Visual Basic.NET atau Visual Basic 2005 kode
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 atau Visual C# 2005 kode
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;
}
}
Tambahkan kode berikut untukListView1_DragDrop event handler.
Visual Basic.NET atau Visual Basic 2005 kode
'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 atau Visual C# 2005 kode
//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);
}
Mulai Microsoft Visual Studio.NET atau Microsoft Visual Studio 2005.
Membuat aplikasi Windows baru dengan menggunakan Visual Basic
.NET, Visual Basic 2005, Visual C# 2005 atau Visual C#.NET.
Secara default, Form1 dibuat.
Tambahkan ListView kontrol untuk
Form1.
Klik kanan ListView1, lalu klikProperti.
Dalam Properti kotak dialog, klikItem properti, dan kemudian klik elipsis
tombol)...).
Dalam ListViewItem koleksi Editorkotak dialog, klik Tambahkan empat kali.
Menetapkan Teks properti
pertama ListViewItem kontrol untuk Item1, mengaturTeks properti dari kedua ListViewItem kontrol untukItem2, mengatur Teks properti ketigaListViewItem kontrol untuk Item3, dan kemudian menetapkanTeks milik keempat ListViewItem kontrol untukItem4.
Dalam ListViewItem koleksi Editorkotak dialog, klik Oke.
Dalam Properti kotak dialogListView1, mengatur Lihat properti untukDaftar.
Menetapkan AllowDrop propertiListView1 untuk Benar.
Pada Bangun menu, klik Bangun
Solusi.
Pada Debug menu, klikMulai.
Mencoba untuk menyeret Item1 untuk
lokasi baru di ListView kontrol.
Untuk informasi tambahan, klik nomor artikel di bawah ini
untuk melihat artikel di dalam Basis Pengetahuan Microsoft:
822482
(http://support.microsoft.com/kb/822482/
)
CARA: Menyediakan fungsionalitas Drag-dan-Drop di aplikasi Microsoft Windows dengan menggunakan Microsoft Visual Basic.NET
PENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:822483
(http://support.microsoft.com/kb/822483/en-us/
)
Seberapa besar upaya Anda untuk menggunakan artikel ini?
Sangat sedikit
Sedikit
Sedang
Besar
Sangat besar
Berikan saran tentang apa yang dapat kami lakukan untuk menyempurnakan informasi ini
Terima kasih! Masukan Anda akan digunakan untuk membantu kami meningkatkan konten dukungan. Untuk opsi bantuan lainnya, kunjungi Halaman Beranda Bantuan dan Dukungan.