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

Definición

Devuelve el índice de base cero del elemento en las coordenadas especificadas.Returns the zero-based index of the item at the specified coordinates.

Sobrecargas

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

Devuelve el índice de base cero del elemento en las coordenadas especificadas.Returns the zero-based index of the item at the specified coordinates.

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

Devuelve el índice de base cero del elemento en las coordenadas especificadas.Returns the zero-based index of the item at the specified coordinates.

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

Devuelve el índice de base cero del elemento en las coordenadas especificadas.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

Parámetros

p
Point Point Point Point

Objeto Point que contiene las coordenadas que se utilizan para obtener el índice del elemento.A Point object containing the coordinates used to obtain the item index.

Devoluciones

Índice de base cero del elemento encontrado en las coordenadas especificadas; devuelve ListBox.NoMatches si no se encuentran coincidencias.The zero-based index of the item found at the specified coordinates; returns ListBox.NoMatches if no match is found.

Ejemplos

En el ejemplo de código siguiente se muestra cómo realizar operaciones de arrastrar y colocar con ListBox un control que contiene elementos que se van RichTextBox a colocar en un control.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. El constructor del formulario establece la AllowDrop propiedad en true para permitir que se produzcan RichTextBoxlas operaciones de arrastrar y colocar en.The constructor of the form sets the AllowDrop property to true to enable drag-and-drop operations to occur in the RichTextBox. En el ejemplo se MouseDown utiliza el evento DoDragDrop de para iniciar la operación de arrastre llamando al método. ListBoxThe example uses the MouseDown event of the ListBox to start the drag operation by calling the DoDragDrop method. En el ejemplo RichTextBox se DragEnter utiliza el evento para determinar si un elemento que se arrastra en es un tipo de datos válido.The example uses the DragEnter event to determine if an item being dragged into the RichTextBox is a valid data type. El DragDrop evento realiza la colocación real de un elemento arrastrado RichTextBox en el control en la ubicación actual del cursor dentro RichTextBoxde.The DragDrop event performs the actual dropping of a dragged item into the RichTextBox control at the current cursor location within the RichTextBox. Este ejemplo requiere que los DragDrop eventos DragEnter y se hayan conectado a los controladores de eventos definidos en el ejemplo.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

Comentarios

Este método permite determinar qué elemento se encuentra en una ubicación concreta dentro del control.This method enables you to determine which item is located at a specific location within the control. Puede usar este método para determinar qué elemento de la lista se selecciona cuando un usuario hace clic con el ListBoxbotón secundario en.You can use this method to determine which item within the list is selected when a user right-clicks the ListBox. La ubicación del cursor se puede determinar y pasar al p parámetro IndexFromPoint del método para determinar en qué elemento hizo clic con el botón secundario del mouse.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. Después, puede mostrar un menú contextual al usuario para proporcionar tareas y características basadas en el elemento específico.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)

Devuelve el índice de base cero del elemento en las coordenadas especificadas.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

Parámetros

x
Int32 Int32 Int32 Int32

Coordenada x de la ubicación que se va a buscar.The x-coordinate of the location to search.

y
Int32 Int32 Int32 Int32

Coordenada y de la ubicación que se va a buscar.The y-coordinate of the location to search.

Devoluciones

Índice de base cero del elemento encontrado en las coordenadas especificadas; devuelve ListBox.NoMatches si no se encuentran coincidencias.The zero-based index of the item found at the specified coordinates; returns ListBox.NoMatches if no match is found.

Ejemplos

En el ejemplo de código siguiente se muestra cómo realizar operaciones de arrastrar y colocar con ListBox un control que contiene elementos que se van RichTextBox a colocar en un control.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. El constructor del formulario establece la AllowDrop propiedad en true para permitir que se produzcan RichTextBoxlas operaciones de arrastrar y colocar en.The constructor of the form sets the AllowDrop property to true to enable drag-and-drop operations to occur in the RichTextBox. En el ejemplo se MouseDown utiliza el evento DoDragDrop de para iniciar la operación de arrastre llamando al método. ListBoxThe example uses the MouseDown event of the ListBox to start the drag operation by calling the DoDragDrop method. En el ejemplo RichTextBox se DragEnter utiliza el evento para determinar si un elemento que se arrastra en es un tipo de datos válido.The example uses the DragEnter event to determine if an item being dragged into the RichTextBox is a valid data type. El DragDrop evento realiza la colocación real de un elemento arrastrado RichTextBox en el control en la ubicación actual del cursor dentro RichTextBoxde.The DragDrop event performs the actual dropping of a dragged item into the RichTextBox control at the current cursor location within the RichTextBox. Este ejemplo requiere que los DragDrop eventos DragEnter y se hayan conectado a los controladores de eventos definidos en el ejemplo.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

Comentarios

Este método permite determinar qué elemento se encuentra en una ubicación concreta del control.This method enables you to determine which item that is located at a specific location within the control. Puede usar este método para determinar qué elemento de la lista se selecciona cuando un usuario hace clic con el ListBoxbotón secundario en.You can use this method to determine which item within the list is selected when a user right-clicks the ListBox. La ubicación del cursor se puede determinar y pasar a los x parámetros y y del IndexFromPoint método para determinar el elemento en el que el usuario hizo clic con el botón secundario del mouse.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. Después, puede mostrar un menú contextual al usuario para proporcionar tareas y características basadas en el elemento específico.You can then display a shortcut menu to the user to provide tasks and features based on the specific item.

Se aplica a