ListViewInsertionMark.NearestIndex メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ListViewInsertionMark.NearestIndex メソッドの意味・解説 

ListViewInsertionMark.NearestIndex メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した位置最も近い項目のインデックス取得します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Function NearestIndex ( _
    pt As Point _
) As Integer
Dim instance As ListViewInsertionMark
Dim pt As Point
Dim returnValue As Integer

returnValue = instance.NearestIndex(pt)
public int NearestIndex (
    Point pt
)
public:
int NearestIndex (
    Point pt
)
public int NearestIndex (
    Point pt
)
public function NearestIndex (
    pt : Point
) : int

パラメータ

pt

最も近い項目を探すときの起点を表す Point

戻り値
指定され位置最も近い項目のインデックス最も近い項目が、現在ドラッグしている項目である場合は、-1 が返されます。

解説解説
使用例使用例

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ListViewInsertionMark クラス
ListViewInsertionMark メンバ
System.Windows.Forms 名前空間
ListView クラス



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ListViewInsertionMark.NearestIndex メソッド」の関連用語

ListViewInsertionMark.NearestIndex メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ListViewInsertionMark.NearestIndex メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS