ListBox.IndexFromPoint Méthode

Définition

Retourne l'index de base zéro de l'élément correspondant aux coordonnées spécifiées.

Surcharges

IndexFromPoint(Point)

Retourne l'index de base zéro de l'élément correspondant aux coordonnées spécifiées.

IndexFromPoint(Int32, Int32)

Retourne l'index de base zéro de l'élément correspondant aux coordonnées spécifiées.

IndexFromPoint(Point)

Retourne l'index de base zéro de l'élément correspondant aux coordonnées spécifiées.

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

Paramètres

p
Point

Objet Point contenant les coordonnées utilisées pour obtenir l'index d'un élément

Retours

Index de base zéro de l'élément trouvé aux coordonnées spécifiées ; retourne ListBox.NoMatches si aucune correspondance n'est trouvée.

Exemples

L’exemple de code suivant montre comment effectuer des opérations de glisser-déplacer à l’aide d’un ListBox contrôle qui contient des éléments à déposer dans un RichTextBox contrôle. Le constructeur du formulaire définit la AllowDrop propriété sur pour true permettre aux opérations de glisser-déplacer de se produire dans le RichTextBox. L’exemple utilise l’événement MouseDown de pour ListBox démarrer l’opération de glisser en appelant la DoDragDrop méthode . L’exemple utilise l’événement DragEnter pour déterminer si un élément déplacé dans le RichTextBox est un type de données valide. L’événement DragDrop effectue la suppression réelle d’un élément déplacé dans le contrôle à l’emplacement RichTextBox du curseur actuel dans .RichTextBox Cet exemple nécessite que les DragDrop événements et DragEnter aient été connectés aux gestionnaires d’événements définis dans l’exemple.

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

Remarques

Cette méthode vous permet de déterminer quel élément se trouve à un emplacement spécifique dans le contrôle. Vous pouvez utiliser cette méthode pour déterminer quel élément de la liste est sélectionné lorsqu’un utilisateur clique avec le bouton droit sur .ListBox L’emplacement du curseur peut être déterminé et passé au p paramètre de la IndexFromPoint méthode pour déterminer l’élément sur lequel l’utilisateur a cliqué avec le bouton droit sur la souris. Vous pouvez ensuite afficher un menu contextuel pour l’utilisateur pour fournir des tâches et des fonctionnalités en fonction de l’élément spécifique.

S’applique à

IndexFromPoint(Int32, Int32)

Retourne l'index de base zéro de l'élément correspondant aux coordonnées spécifiées.

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

Paramètres

x
Int32

Coordonnée x de la position à rechercher.

y
Int32

Coordonnée y de la position à rechercher.

Retours

Index de base zéro de l'élément trouvé aux coordonnées spécifiées ; retourne ListBox.NoMatches si aucune correspondance n'est trouvée.

Exemples

L’exemple de code suivant montre comment effectuer des opérations de glisser-déplacer à l’aide d’un ListBox contrôle qui contient des éléments à déposer dans un RichTextBox contrôle. Le constructeur du formulaire définit la AllowDrop propriété sur pour true permettre aux opérations de glisser-déplacer de se produire dans le RichTextBox. L’exemple utilise l’événement MouseDown de pour ListBox démarrer l’opération de glisser en appelant la DoDragDrop méthode . L’exemple utilise l’événement DragEnter pour déterminer si un élément déplacé dans le RichTextBox est un type de données valide. L’événement DragDrop effectue la suppression réelle d’un élément déplacé dans le contrôle à l’emplacement RichTextBox du curseur actuel dans .RichTextBox Cet exemple nécessite que les DragDrop événements et DragEnter aient été connectés aux gestionnaires d’événements définis dans l’exemple.

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

Remarques

Cette méthode vous permet de déterminer quel élément se trouve à un emplacement spécifique dans le contrôle. Vous pouvez utiliser cette méthode pour déterminer quel élément de la liste est sélectionné lorsqu’un utilisateur clique avec le bouton droit sur .ListBox L’emplacement du curseur peut être déterminé et passé aux x paramètres et y de la IndexFromPoint méthode pour déterminer l’élément sur lequel l’utilisateur a cliqué avec le bouton droit sur la souris. Vous pouvez ensuite afficher un menu contextuel pour l’utilisateur pour fournir des tâches et des fonctionnalités en fonction de l’élément spécifique.

S’applique à