ListBox.IndexFromPoint Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Retourneert de op nul gebaseerde index van het item op de opgegeven coördinaten.
Overloads
| Name | Description |
|---|---|
| IndexFromPoint(Point) |
Retourneert de op nul gebaseerde index van het item op de opgegeven coördinaten. |
| IndexFromPoint(Int32, Int32) |
Retourneert de op nul gebaseerde index van het item op de opgegeven coördinaten. |
IndexFromPoint(Point)
Retourneert de op nul gebaseerde index van het item op de opgegeven coördinaten.
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
Parameters
Retouren
De op nul gebaseerde index van het item dat op de opgegeven coördinaten is gevonden; retourneert ListBox.NoMatches als er geen overeenkomst is gevonden.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u slepen-en-neerzetten-bewerkingen kunt uitvoeren met behulp van een ListBox besturingselement dat items bevat die u in een RichTextBox besturingselement kunt neerzetten. De constructor van het formulier stelt de AllowDrop eigenschap in om true slepen en neerzetten in te schakelen in de RichTextBox. In het voorbeeld wordt de MouseDown gebeurtenis van de ListBox gebeurtenis gebruikt om de sleepbewerking te starten door de DoDragDrop methode aan te roepen. In het voorbeeld wordt de DragEnter gebeurtenis gebruikt om te bepalen of een item dat naar het RichTextBox item wordt gesleept, een geldig gegevenstype is. De DragDrop gebeurtenis voert het daadwerkelijk verwijderen van een gesleept item uit naar het RichTextBox besturingselement op de huidige cursorlocatie binnen de RichTextBox. Voor dit voorbeeld moeten de DragDrop gebeurtenissen DragEnter zijn verbonden met de gebeurtenis-handlers die in het voorbeeld zijn gedefinieerd.
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
Opmerkingen
Met deze methode kunt u bepalen welk item zich op een specifieke locatie in het besturingselement bevindt. U kunt deze methode gebruiken om te bepalen welk item in de lijst wordt geselecteerd wanneer een gebruiker met de rechtermuisknop op het ListBoxitem klikt. De locatie van de cursor kan worden bepaald en doorgegeven aan de p parameter van de IndexFromPoint methode om te bepalen welk item de gebruiker met de rechtermuisknop heeft geklikt. Vervolgens kunt u een snelmenu aan de gebruiker weergeven om taken en functies te leveren op basis van het specifieke item.
Van toepassing op
IndexFromPoint(Int32, Int32)
Retourneert de op nul gebaseerde index van het item op de opgegeven coördinaten.
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
Parameters
- x
- Int32
De x-coördinaat van de locatie die moet worden gezocht.
- y
- Int32
De y-coördinaat van de locatie die moet worden gezocht.
Retouren
De op nul gebaseerde index van het item dat op de opgegeven coördinaten is gevonden; retourneert ListBox.NoMatches als er geen overeenkomst is gevonden.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u slepen-en-neerzetten-bewerkingen kunt uitvoeren met behulp van een ListBox besturingselement dat items bevat die u in een RichTextBox besturingselement kunt neerzetten. De constructor van het formulier stelt de AllowDrop eigenschap in om true slepen en neerzetten in te schakelen in de RichTextBox. In het voorbeeld wordt de MouseDown gebeurtenis van de ListBox gebeurtenis gebruikt om de sleepbewerking te starten door de DoDragDrop methode aan te roepen. In het voorbeeld wordt de DragEnter gebeurtenis gebruikt om te bepalen of een item dat naar het RichTextBox item wordt gesleept, een geldig gegevenstype is. De DragDrop gebeurtenis voert het daadwerkelijk verwijderen van een gesleept item uit naar het RichTextBox besturingselement op de huidige cursorlocatie binnen de RichTextBox. Voor dit voorbeeld moeten de DragDrop gebeurtenissen DragEnter zijn verbonden met de gebeurtenis-handlers die in het voorbeeld zijn gedefinieerd.
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
Opmerkingen
Met deze methode kunt u bepalen welk item zich op een specifieke locatie in het besturingselement bevindt. U kunt deze methode gebruiken om te bepalen welk item in de lijst wordt geselecteerd wanneer een gebruiker met de rechtermuisknop op het ListBoxitem klikt. De locatie van de cursor kan worden bepaald en doorgegeven aan de x en y parameters van de IndexFromPoint methode om te bepalen welk item de gebruiker met de rechtermuisknop heeft geklikt. Vervolgens kunt u een snelmenu aan de gebruiker weergeven om taken en functies te leveren op basis van het specifieke item.