ListBox.IndexFromPoint Metod

Definition

Returnerar det nollbaserade indexet för objektet vid de angivna koordinaterna.

Överlagringar

Name Description
IndexFromPoint(Point)

Returnerar det nollbaserade indexet för objektet vid de angivna koordinaterna.

IndexFromPoint(Int32, Int32)

Returnerar det nollbaserade indexet för objektet vid de angivna koordinaterna.

IndexFromPoint(Point)

Källa:
ListBox.cs
Källa:
ListBox.cs
Källa:
ListBox.cs
Källa:
ListBox.cs
Källa:
ListBox.cs

Returnerar det nollbaserade indexet för objektet vid de angivna koordinaterna.

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

Parametrar

p
Point

Ett Point objekt som innehåller koordinaterna som används för att hämta objektindexet.

Returer

Det nollbaserade indexet för objektet som hittades vid de angivna koordinaterna. returnerar ListBox.NoMatches om ingen matchning hittas.

Exempel

I följande kodexempel visas hur du utför dra och släpp-åtgärder med hjälp av en ListBox kontroll som innehåller objekt som ska tas bort till en RichTextBox kontroll. Konstruktorn i formuläret anger AllowDrop egenskapen till true så att dra och släpp-åtgärder kan utföras i RichTextBox. I exemplet används MouseDown händelsen ListBox för för att starta dra-åtgärden genom att anropa DoDragDrop metoden. I exemplet används DragEnter händelsen för att avgöra om ett objekt som dras till RichTextBox är en giltig datatyp. Händelsen DragDrop utför den faktiska nedsläppningen av ett släpat objekt i RichTextBox kontrollen på den aktuella markörens plats i RichTextBox. Det här exemplet kräver att DragDrop händelserna och DragEnter har anslutits till de händelsehanterare som definierats i exemplet.

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

Kommentarer

Med den här metoden kan du avgöra vilket objekt som finns på en specifik plats i kontrollen. Du kan använda den här metoden för att avgöra vilket objekt i listan som väljs när en användare högerklickar på ListBox. Markörens plats kan fastställas och skickas till parametern pIndexFromPoint för metoden för att avgöra vilket objekt användaren högerklickade musen över. Du kan sedan visa en snabbmeny för användaren för att tillhandahålla uppgifter och funktioner baserat på det specifika objektet.

Gäller för

IndexFromPoint(Int32, Int32)

Källa:
ListBox.cs
Källa:
ListBox.cs
Källa:
ListBox.cs
Källa:
ListBox.cs
Källa:
ListBox.cs

Returnerar det nollbaserade indexet för objektet vid de angivna koordinaterna.

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

Parametrar

x
Int32

X-koordinaten för sökplatsen.

y
Int32

Y-koordinaten för sökplatsen.

Returer

Det nollbaserade indexet för objektet som hittades vid de angivna koordinaterna. returnerar ListBox.NoMatches om ingen matchning hittas.

Exempel

I följande kodexempel visas hur du utför dra och släpp-åtgärder med hjälp av en ListBox kontroll som innehåller objekt som ska tas bort till en RichTextBox kontroll. Konstruktorn i formuläret anger AllowDrop egenskapen till true så att dra och släpp-åtgärder kan utföras i RichTextBox. I exemplet används MouseDown händelsen ListBox för för att starta dra-åtgärden genom att anropa DoDragDrop metoden. I exemplet används DragEnter händelsen för att avgöra om ett objekt som dras till RichTextBox är en giltig datatyp. Händelsen DragDrop utför den faktiska nedsläppningen av ett släpat objekt i RichTextBox kontrollen på den aktuella markörens plats i RichTextBox. Det här exemplet kräver att DragDrop händelserna och DragEnter har anslutits till de händelsehanterare som definierats i exemplet.

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

Kommentarer

Med den här metoden kan du avgöra vilket objekt som finns på en specifik plats i kontrollen. Du kan använda den här metoden för att avgöra vilket objekt i listan som väljs när en användare högerklickar på ListBox. Markörens plats kan fastställas och skickas till x parametrarna y och IndexFromPoint för metoden för att avgöra vilket objekt användaren högerklickade musen över. Du kan sedan visa en snabbmeny för användaren för att tillhandahålla uppgifter och funktioner baserat på det specifika objektet.

Gäller för