ListViewInsertionMark.NearestIndex メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As ListViewInsertionMark Dim pt As Point Dim returnValue As Integer returnValue = instance.NearestIndex(pt)
戻り値
指定された位置に最も近い項目のインデックス。最も近い項目が、現在ドラッグしている項目である場合は、-1 が返されます。

ドラッグ アンド ドロップ操作の実行中にこのメソッドを使用することで、マウス ポインタに最も近い項目を特定できます。返されたインデックス値を使用して、Index プロパティを設定します。マウス ポインタに最も近い項目が、現在ドラッグしている項目である場合は、このメソッドの戻り値は -1 になります。この場合、Index プロパティをこの値に設定すると、挿入マークが非表示になります。
このメソッドはマウス ポインタの位置にかかわらず最も近い項目を探しますが、ListView.GetItemAt メソッドは指定された位置にある項目のみを返します。その位置に項目がなければ、null 参照 (Visual Basic では Nothing) を返します。たとえばマウス ポインタが 2 つの項目の間にあるときには、ListView.GetItemAt メソッドは null 参照 (Visual Basic では Nothing) を返します。そのため、ドラッグ アンド ドロップ操作を使用して項目を移動する場合には、必ず NearestIndex メソッドを使用してください。

ListView の挿入マーク機能を使用し、標準のドラッグ イベントを使ったドラッグ アンド ドロップ項目の並べ替えを実装する方法を次のコード例に示します。挿入マークの位置は、Control.DragOver イベントのハンドラで更新されます。このハンドラでは、マウス ポインタの位置を、最も近くにある項目の中心と比較し、その結果を使用して、挿入マークを項目の右に表示するか、左に表示するかを決定しています。
コード全体については、ListViewInsertionMark の概要のリファレンス トピックを参照してください。
' Moves the insertion mark as the item is dragged. Private Sub myListView_DragOver(sender As Object, e As DragEventArgs) ' Retrieve the client coordinates of the mouse pointer. Dim targetPoint As Point = myListView.PointToClient(New Point(e.X, e.Y)) ' Retrieve the index of the item closest to the mouse pointer. Dim targetIndex As Integer = _ myListView.InsertionMark.NearestIndex(targetPoint) ' Confirm that the mouse pointer is not over the dragged item. If targetIndex > -1 Then ' Determine whether the mouse pointer is to the left or ' the right of the midpoint of the closest item and set ' the InsertionMark.AppearsAfterItem property accordingly. Dim itemBounds As Rectangle = myListView.GetItemRect(targetIndex) If targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) Then myListView.InsertionMark.AppearsAfterItem = True Else myListView.InsertionMark.AppearsAfterItem = False End If End If ' Set the location of the insertion mark. If the mouse is ' over the dragged item, the targetIndex value is -1 and ' the insertion mark disappears. myListView.InsertionMark.Index = targetIndex End Sub 'myListView_DragOver
// Moves the insertion mark as the item is dragged. private void myListView_DragOver(object sender, DragEventArgs e) { // Retrieve the client coordinates of the mouse pointer. Point targetPoint = myListView.PointToClient(new Point(e.X, e.Y)); // Retrieve the index of the item closest to the mouse pointer. int targetIndex = myListView.InsertionMark.NearestIndex(targetPoint); // Confirm that the mouse pointer is not over the dragged item. if (targetIndex > -1) { // Determine whether the mouse pointer is to the left or // the right of the midpoint of the closest item and set // the InsertionMark.AppearsAfterItem property accordingly. Rectangle itemBounds = myListView.GetItemRect(targetIndex); if ( targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) ) { myListView.InsertionMark.AppearsAfterItem = true; } else { myListView.InsertionMark.AppearsAfterItem = false; } } // Set the location of the insertion mark. If the mouse is // over the dragged item, the targetIndex value is -1 and // the insertion mark disappears. myListView.InsertionMark.Index = targetIndex; }
// Moves the insertion mark as the item is dragged. void myListView_DragOver( Object^ /*sender*/, DragEventArgs^ e ) { // Retrieve the client coordinates of the mouse pointer. Point targetPoint = myListView->PointToClient( Point(e->X,e->Y) ); // Retrieve the index of the item closest to the mouse pointer. int targetIndex = myListView->InsertionMark->NearestIndex( targetPoint ); // Confirm that the mouse pointer is not over the dragged item. if ( targetIndex > -1 ) { // Determine whether the mouse pointer is to the left or // the right of the midpoint of the closest item and set // the InsertionMark.AppearsAfterItem property accordingly. Rectangle itemBounds = myListView->GetItemRect( targetIndex ); if ( targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) ) { myListView->InsertionMark->AppearsAfterItem = true; } else { myListView->InsertionMark->AppearsAfterItem = false; } } // Set the location of the insertion mark. If the mouse is // over the dragged item, the targetIndex value is -1 and // the insertion mark disappears. myListView->InsertionMark->Index = targetIndex; }
// Moves the insertion mark as the item is dragged. private void myListView_DragOver(Object sender, DragEventArgs e) { // Retrieve the client coordinates of the mouse pointer. Point targetPoint = myListView.PointToClient( new Point(e.get_X(), e.get_Y())); // Retrieve the index of the item closest to the mouse pointer. int targetIndex = myListView.get_InsertionMark(). NearestIndex(targetPoint); // Confirm that the mouse pointer is not over the dragged item. if (targetIndex > -1) { // Determine whether the mouse pointer is to the left or // the right of the midpoint of the closest item and set // the InsertionMark.AppearsAfterItem property accordingly. Rectangle itemBounds = myListView.GetItemRect(targetIndex); if (targetPoint.get_X() > itemBounds.get_Left() + itemBounds.get_Width() / 2) { myListView.get_InsertionMark().set_AppearsAfterItem(true); } else { myListView.get_InsertionMark().set_AppearsAfterItem(false); } } // Set the location of the insertion mark. If the mouse is // over the dragged item, the targetIndex value is -1 and // the insertion mark disappears. myListView.get_InsertionMark().set_Index(targetIndex); } //myListView_DragOver

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からListViewInsertionMark.NearestIndex メソッドを検索する場合は、下記のリンクをクリックしてください。

- ListViewInsertionMark.NearestIndex メソッドのページへのリンク