ListBox.IndexFromPoint ListBox.IndexFromPoint ListBox.IndexFromPoint ListBox.IndexFromPoint Method

定义

返回指定坐标处的项的从零开始的索引。Returns the zero-based index of the item at the specified coordinates.

重载

IndexFromPoint(Point) IndexFromPoint(Point) IndexFromPoint(Point) IndexFromPoint(Point)

返回指定坐标处的项的从零开始的索引。Returns the zero-based index of the item at the specified coordinates.

IndexFromPoint(Int32, Int32) IndexFromPoint(Int32, Int32) IndexFromPoint(Int32, Int32) IndexFromPoint(Int32, Int32)

返回指定坐标处的项的从零开始的索引。Returns the zero-based index of the item at the specified coordinates.

IndexFromPoint(Point) IndexFromPoint(Point) IndexFromPoint(Point) IndexFromPoint(Point)

返回指定坐标处的项的从零开始的索引。Returns the zero-based index of the item at the specified coordinates.

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 Point Point

Point 对象,包含用于获取项索引的坐标。A Point object containing the coordinates used to obtain the item index.

返回

在指定坐标处找到的项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatchesThe zero-based index of the item found at the specified coordinates; returns ListBox.NoMatches if no match is found.

示例

下面的代码示例演示如何使用ListBox包含要放置RichTextBox到控件中的项的控件执行拖放操作。The following code example demonstrates how to perform drag-and-drop operations using a ListBox control that contains items to drop into a RichTextBox control. 窗体的构造函数将AllowDrop属性设置为true ,以便在中执行拖放RichTextBox操作。The constructor of the form sets the AllowDrop property to true to enable drag-and-drop operations to occur in the RichTextBox. 该示例通过调用MouseDown DoDragDrop方法,使用ListBox的事件来启动拖动操作。The example uses the MouseDown event of the ListBox to start the drag operation by calling the DoDragDrop method. 该示例使用DragEnter事件确定要拖动到的RichTextBox项是否为有效的数据类型。The example uses the DragEnter event to determine if an item being dragged into the RichTextBox is a valid data type. DragDrop事件执行拖动的项到实际放RichTextBox控件中的当前光标位置在RichTextBoxThe DragDrop event performs the actual dropping of a dragged item into the RichTextBox control at the current cursor location within the RichTextBox. 此示例要求DragDropDragEnter事件已连接到示例中定义的事件处理程序。This example requires that the DragDrop and DragEnter events have been connected to the event handlers defined in the example.

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 'listBox1_MouseDown


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 'richTextBox1_DragEnter

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 'richTextBox1_DragDrop

注解

此方法使您能够确定哪个项位于控件中的特定位置。This method enables you to determine which item is located at a specific location within the control. 用户右键单击ListBox时,可以使用此方法来确定列表中的哪一项处于选定状态。You can use this method to determine which item within the list is selected when a user right-clicks the ListBox. 可以确定游标的位置,并将其传递pIndexFromPoint方法的参数,以确定用户鼠标右键单击的项。The location of the cursor can be determined and passed to the p parameter of the IndexFromPoint method to determine which item the user right-clicked the mouse over. 然后,您可以向用户显示一个快捷菜单,以提供基于特定项的任务和功能。You can then display a shortcut menu to the user to provide tasks and features based on the specific item.

IndexFromPoint(Int32, Int32) IndexFromPoint(Int32, Int32) IndexFromPoint(Int32, Int32) IndexFromPoint(Int32, Int32)

返回指定坐标处的项的从零开始的索引。Returns the zero-based index of the item at the specified coordinates.

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 Int32 Int32 Int32

要搜索的位置的 x 坐标。The x-coordinate of the location to search.

y
Int32 Int32 Int32 Int32

要搜索的位置的 y 坐标。The y-coordinate of the location to search.

返回

在指定坐标处找到的项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatchesThe zero-based index of the item found at the specified coordinates; returns ListBox.NoMatches if no match is found.

示例

下面的代码示例演示如何使用ListBox包含要放置RichTextBox到控件中的项的控件执行拖放操作。The following code example demonstrates how to perform drag-and-drop operations using a ListBox control that contains items to drop into a RichTextBox control. 窗体的构造函数将AllowDrop属性设置为true ,以便在中执行拖放RichTextBox操作。The constructor of the form sets the AllowDrop property to true to enable drag-and-drop operations to occur in the RichTextBox. 该示例通过调用MouseDown DoDragDrop方法,使用ListBox的事件来启动拖动操作。The example uses the MouseDown event of the ListBox to start the drag operation by calling the DoDragDrop method. 该示例使用DragEnter事件确定要拖动到的RichTextBox项是否为有效的数据类型。The example uses the DragEnter event to determine if an item being dragged into the RichTextBox is a valid data type. DragDrop事件执行拖动的项到实际放RichTextBox控件中的当前光标位置在RichTextBoxThe DragDrop event performs the actual dropping of a dragged item into the RichTextBox control at the current cursor location within the RichTextBox. 此示例要求DragDropDragEnter事件已连接到示例中定义的事件处理程序。This example requires that the DragDrop and DragEnter events have been connected to the event handlers defined in the example.

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 'listBox1_MouseDown


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 'richTextBox1_DragEnter

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 'richTextBox1_DragDrop

注解

此方法使您能够确定位于控件中特定位置的项。This method enables you to determine which item that is located at a specific location within the control. 用户右键单击ListBox时,可以使用此方法来确定列表中的哪一项处于选定状态。You can use this method to determine which item within the list is selected when a user right-clicks the ListBox. 可以确定光标的位置,并将其传递给xy的参数IndexFromPoint方法,以确定哪一项用户通过右键单击鼠标。The location of the cursor can be determined and passed to the x and y parameters of the IndexFromPoint method to determine which item the user right-clicked the mouse over. 然后,您可以向用户显示一个快捷菜单,以提供基于特定项的任务和功能。You can then display a shortcut menu to the user to provide tasks and features based on the specific item.

适用于