ListBox.IndexFromPoint 메서드

정의

지정된 좌표에 있는 항목의 0부터 시작하는 인덱스를 반환합니다.

오버로드

IndexFromPoint(Point)

지정된 좌표에 있는 항목의 0부터 시작하는 인덱스를 반환합니다.

IndexFromPoint(Int32, Int32)

지정된 좌표에 있는 항목의 0부터 시작하는 인덱스를 반환합니다.

IndexFromPoint(Point)

지정된 좌표에 있는 항목의 0부터 시작하는 인덱스를 반환합니다.

public:
 int IndexFromPoint(System::Drawing::Point p);
public int IndexFromPoint (System.Drawing.Point p);
member this.IndexFromPoint : System.Drawing.Point -> int
Public Function IndexFromPoint (p As Point) As Integer

매개 변수

p
Point

항목 인덱스를 얻는 데 사용되는 좌표를 포함하는 Point 개체입니다.

반환

지정된 좌표에 있는 항목의 인덱스(0부터 시작)입니다. 일치하는 항목이 없으면 ListBox.NoMatches를 반환합니다.

예제

다음 코드 예제에서는 끌어서 놓을 항목을 포함 하는 컨트롤을 ListBox 사용 하 여 끌어서 놓기 작업을 수행 하는 방법을 보여 줍니다는 컨트롤입니다 RichTextBox . 폼의 생성자는 속성을 true 로 설정 AllowDrop 하여 에서 RichTextBox끌어서 놓기 작업을 수행할 수 있도록 합니다. 예제에서는 합니다 MouseDown 의 이벤트를 ListBox 호출 하 여 끌기 작업을 시작 하려면를 DoDragDrop 메서드. 이 예제에서는 이벤트를 사용하여 DragEnter 로 끌어가 RichTextBox 는 항목이 유효한 데이터 형식인지 확인합니다. 이벤트는 DragDrop 내의 현재 커서 위치에서 컨트롤에 RichTextBox 끌어 놓은 항목의 실제 삭제를 RichTextBox수행합니다. 이 예제에서는 DragDropDragEnter 이벤트가 예제에 정의된 이벤트 처리기에 연결되어야 합니다.

public:
   Form1()
   {
      InitializeComponent();
      
      // Sets the control to allow drops, and then adds the necessary event handlers.
      this->richTextBox1->AllowDrop = true;
   }

private:
   void listBox1_MouseDown( Object^ sender, System::Windows::Forms::MouseEventArgs^ e )
   {
      // Determines which item was selected.
      ListBox^ lb = (dynamic_cast<ListBox^>(sender));
      Point pt = Point(e->X,e->Y);

      //Retrieve the item at the specified location within the ListBox.
      int index = lb->IndexFromPoint( pt );

      // Starts a drag-and-drop operation.
      if ( index >= 0 )
      {
         // Retrieve the selected item text to drag into the RichTextBox.
         lb->DoDragDrop( lb->Items[ index ]->ToString(), DragDropEffects::Copy );
      }
   }

   void richTextBox1_DragEnter( Object^ /*sender*/, DragEventArgs^ e )
   {
      // If the data is text, copy the data to the RichTextBox control.
      if ( e->Data->GetDataPresent( "Text" ) )
            e->Effect = DragDropEffects::Copy;
   }

   void richTextBox1_DragDrop( Object^ /*sender*/, DragEventArgs^ e )
   {
      // Paste the text into the RichTextBox where at selection location.
      richTextBox1->SelectedText = e->Data->GetData( "System.String", true )->ToString();
   }
public Form1()
{
   InitializeComponent();
   // Sets the control to allow drops, and then adds the necessary event handlers.
   this.richTextBox1.AllowDrop = true;
}
 
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
   // Determines which item was selected.
   ListBox lb =( (ListBox)sender);
   Point pt = new Point(e.X,e.Y);
   //Retrieve the item at the specified location within the ListBox.
   int index = lb.IndexFromPoint(pt);

   // Starts a drag-and-drop operation.
   if(index>=0) 
   {
      // Retrieve the selected item text to drag into the RichTextBox.
      lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Copy);
   }
}

private void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
   // If the data is text, copy the data to the RichTextBox control.
   if(e.Data.GetDataPresent("Text"))
      e.Effect = DragDropEffects.Copy;
}

private void richTextBox1_DragDrop(object sender, DragEventArgs e) 
{
   // Paste the text into the RichTextBox where at selection location.
   richTextBox1.SelectedText =  e.Data.GetData("System.String", true).ToString();
}
Public Sub New()
   MyBase.New()

   'This call is required by the Windows Form Designer.
   InitializeComponent()

   richTextBox1.AllowDrop = True

End Sub

Private Sub listBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listBox1.MouseDown
   ' Determines which item was selected.
   Dim lb As ListBox = CType(sender, ListBox)
   Dim pt As New Point(e.X, e.Y)
   'Retrieve the item at the specified location within the ListBox.
   Dim index As Integer = lb.IndexFromPoint(pt)

   ' Starts a drag-and-drop operation.
   If index >= 0 Then
      ' Retrieve the selected item text to drag into the RichTextBox.
      lb.DoDragDrop(lb.Items(index).ToString(), DragDropEffects.Copy)
   End If
End Sub


Private Sub richTextBox1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles richTextBox1.DragEnter
   ' If the data is text, copy the data to the RichTextBox control.
   If e.Data.GetDataPresent("Text") Then
      e.Effect = DragDropEffects.Copy
   End If
End Sub

Private Sub richTextBox1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles richTextBox1.DragDrop
   ' Paste the text into the RichTextBox where at selection location.
   richTextBox1.SelectedText = e.Data.GetData("System.String", True).ToString()
End Sub

설명

이 메서드를 사용하면 컨트롤 내의 특정 위치에 있는 항목을 확인할 수 있습니다. 이 메서드를 사용하여 사용자가 를 마우스 오른쪽 단추로 클릭할 때 목록 내의 항목을 선택할 수 있습니다.ListBox 커서의 위치를 결정 하 고 전달할 수 있습니다는 p 의 매개 변수는 IndexFromPoint 사용자는 항목을 확인할 메서드를 통해 마우스 마우스 오른쪽 단추로 클릭 합니다. 그런 다음 사용자에게 바로 가기 메뉴를 표시하여 특정 항목에 따라 작업 및 기능을 제공할 수 있습니다.

적용 대상

IndexFromPoint(Int32, Int32)

지정된 좌표에 있는 항목의 0부터 시작하는 인덱스를 반환합니다.

public:
 int IndexFromPoint(int x, int y);
public int IndexFromPoint (int x, int y);
member this.IndexFromPoint : int * int -> int
Public Function IndexFromPoint (x As Integer, y As Integer) As Integer

매개 변수

x
Int32

검색할 위치의 X 좌표입니다.

y
Int32

검색할 위치의 Y 좌표입니다.

반환

지정된 좌표에 있는 항목의 인덱스(0부터 시작)입니다. 일치하는 항목이 없으면 ListBox.NoMatches를 반환합니다.

예제

다음 코드 예제에서는 끌어서 놓을 항목을 포함 하는 컨트롤을 ListBox 사용 하 여 끌어서 놓기 작업을 수행 하는 방법을 보여 줍니다는 컨트롤입니다 RichTextBox . 폼의 생성자는 속성을 true 로 설정 AllowDrop 하여 에서 RichTextBox끌어서 놓기 작업을 수행할 수 있도록 합니다. 예제에서는 합니다 MouseDown 의 이벤트를 ListBox 호출 하 여 끌기 작업을 시작 하려면를 DoDragDrop 메서드. 이 예제에서는 이벤트를 사용하여 DragEnter 로 끌어가 RichTextBox 는 항목이 유효한 데이터 형식인지 확인합니다. 이벤트는 DragDrop 내의 현재 커서 위치에서 컨트롤에 RichTextBox 끌어 놓은 항목의 실제 삭제를 RichTextBox수행합니다. 이 예제에서는 DragDropDragEnter 이벤트가 예제에 정의된 이벤트 처리기에 연결되어야 합니다.

public:
   Form1()
   {
      InitializeComponent();
      
      // Sets the control to allow drops, and then adds the necessary event handlers.
      this->richTextBox1->AllowDrop = true;
   }

private:
   void listBox1_MouseDown( Object^ sender, System::Windows::Forms::MouseEventArgs^ e )
   {
      // Determines which item was selected.
      ListBox^ lb = (dynamic_cast<ListBox^>(sender));
      Point pt = Point(e->X,e->Y);

      //Retrieve the item at the specified location within the ListBox.
      int index = lb->IndexFromPoint( pt );

      // Starts a drag-and-drop operation.
      if ( index >= 0 )
      {
         // Retrieve the selected item text to drag into the RichTextBox.
         lb->DoDragDrop( lb->Items[ index ]->ToString(), DragDropEffects::Copy );
      }
   }

   void richTextBox1_DragEnter( Object^ /*sender*/, DragEventArgs^ e )
   {
      // If the data is text, copy the data to the RichTextBox control.
      if ( e->Data->GetDataPresent( "Text" ) )
            e->Effect = DragDropEffects::Copy;
   }

   void richTextBox1_DragDrop( Object^ /*sender*/, DragEventArgs^ e )
   {
      // Paste the text into the RichTextBox where at selection location.
      richTextBox1->SelectedText = e->Data->GetData( "System.String", true )->ToString();
   }
public Form1()
{
   InitializeComponent();
   // Sets the control to allow drops, and then adds the necessary event handlers.
   this.richTextBox1.AllowDrop = true;
}
 
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
   // Determines which item was selected.
   ListBox lb =( (ListBox)sender);
   Point pt = new Point(e.X,e.Y);
   //Retrieve the item at the specified location within the ListBox.
   int index = lb.IndexFromPoint(pt);

   // Starts a drag-and-drop operation.
   if(index>=0) 
   {
      // Retrieve the selected item text to drag into the RichTextBox.
      lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Copy);
   }
}

private void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
   // If the data is text, copy the data to the RichTextBox control.
   if(e.Data.GetDataPresent("Text"))
      e.Effect = DragDropEffects.Copy;
}

private void richTextBox1_DragDrop(object sender, DragEventArgs e) 
{
   // Paste the text into the RichTextBox where at selection location.
   richTextBox1.SelectedText =  e.Data.GetData("System.String", true).ToString();
}
Public Sub New()
   MyBase.New()

   'This call is required by the Windows Form Designer.
   InitializeComponent()

   richTextBox1.AllowDrop = True

End Sub

Private Sub listBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listBox1.MouseDown
   ' Determines which item was selected.
   Dim lb As ListBox = CType(sender, ListBox)
   Dim pt As New Point(e.X, e.Y)
   'Retrieve the item at the specified location within the ListBox.
   Dim index As Integer = lb.IndexFromPoint(pt)

   ' Starts a drag-and-drop operation.
   If index >= 0 Then
      ' Retrieve the selected item text to drag into the RichTextBox.
      lb.DoDragDrop(lb.Items(index).ToString(), DragDropEffects.Copy)
   End If
End Sub


Private Sub richTextBox1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles richTextBox1.DragEnter
   ' If the data is text, copy the data to the RichTextBox control.
   If e.Data.GetDataPresent("Text") Then
      e.Effect = DragDropEffects.Copy
   End If
End Sub

Private Sub richTextBox1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles richTextBox1.DragDrop
   ' Paste the text into the RichTextBox where at selection location.
   richTextBox1.SelectedText = e.Data.GetData("System.String", True).ToString()
End Sub

설명

이 메서드를 사용하면 컨트롤 내의 특정 위치에 있는 항목을 확인할 수 있습니다. 이 메서드를 사용하여 사용자가 를 마우스 오른쪽 단추로 클릭할 때 목록 내의 항목을 선택할 수 있습니다.ListBox 커서의 위치를 확인하고 메서드의 IndexFromPointy 매개 변수에 x 전달하여 사용자가 마우스를 마우스 오른쪽 단추로 클릭한 항목을 확인할 수 있습니다. 그런 다음 사용자에게 바로 가기 메뉴를 표시하여 특정 항목에 따라 작업 및 기능을 제공할 수 있습니다.

적용 대상