Aracılığıyla paylaş


ListBox.IndexFromPoint Yöntem

Tanım

Belirtilen koordinatlarda öğenin sıfır tabanlı dizinini döndürür.

Aşırı Yüklemeler

Name Description
IndexFromPoint(Point)

Belirtilen koordinatlarda öğenin sıfır tabanlı dizinini döndürür.

IndexFromPoint(Int32, Int32)

Belirtilen koordinatlarda öğenin sıfır tabanlı dizinini döndürür.

IndexFromPoint(Point)

Kaynak:
ListBox.cs
Kaynak:
ListBox.cs
Kaynak:
ListBox.cs
Kaynak:
ListBox.cs
Kaynak:
ListBox.cs

Belirtilen koordinatlarda öğenin sıfır tabanlı dizinini döndürür.

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

Parametreler

p
Point

Point Öğe dizinini almak için kullanılan koordinatları içeren nesne.

Döndürülenler

Belirtilen koordinatlarda bulunan öğenin sıfır tabanlı dizini; eşleşme bulunamazsa döndürür ListBox.NoMatches .

Örnekler

Aşağıdaki kod örneği, denetime bırakacak RichTextBox öğeleri içeren bir ListBox denetim kullanarak sürükle ve bırak işlemlerinin nasıl gerçekleştirildiğini gösterir. Formun oluşturucusunda sürükle ve bırak işlemlerinin AllowDrop gerçekleşmesini RichTextBoxsağlamak için özelliğini true olarak ayarlar. Örnek, yöntemini çağırarak sürükleme işlemini başlatmak için öğesinin DoDragDrop olayını ListBox kullanırMouseDown. Örnekte, içine sürüklenen DragEnterRichTextBox bir öğenin geçerli bir veri türü olup olmadığını belirlemek için olayı kullanılır. Olay, DragDrop sürüklenen bir öğenin içindeki geçerli imleç konumundaki denetime RichTextBox asıl bırakma işlemini RichTextBoxgerçekleştirir. Bu örnek, ve DragEnter olaylarının DragDrop örnekte tanımlanan olay işleyicilerine bağlı olmasını gerektirir.

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

Açıklamalar

Bu yöntem, hangi öğenin denetim içindeki belirli bir konumda bulunduğunu belirlemenizi sağlar. Bir kullanıcı öğesine sağ tıkladığında ListBoxlistedeki hangi öğenin seçileceğini belirlemek için bu yöntemi kullanabilirsiniz. İmlecin konumu belirlenebilir ve kullanıcının farenin p üzerine sağ tıkladığı öğeyi belirlemek için yöntemin parametresine IndexFromPoint geçirilebilir. Daha sonra kullanıcıya, belirli bir öğeyi temel alan görevler ve özellikler sağlamak için bir kısayol menüsü görüntüleyebilirsiniz.

Şunlara uygulanır

IndexFromPoint(Int32, Int32)

Kaynak:
ListBox.cs
Kaynak:
ListBox.cs
Kaynak:
ListBox.cs
Kaynak:
ListBox.cs
Kaynak:
ListBox.cs

Belirtilen koordinatlarda öğenin sıfır tabanlı dizinini döndürür.

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

Parametreler

x
Int32

Aranacak konumun x koordinatı.

y
Int32

Aranacak konumun y koordinatı.

Döndürülenler

Belirtilen koordinatlarda bulunan öğenin sıfır tabanlı dizini; eşleşme bulunamazsa döndürür ListBox.NoMatches .

Örnekler

Aşağıdaki kod örneği, denetime bırakacak RichTextBox öğeleri içeren bir ListBox denetim kullanarak sürükle ve bırak işlemlerinin nasıl gerçekleştirildiğini gösterir. Formun oluşturucusunda sürükle ve bırak işlemlerinin AllowDrop gerçekleşmesini RichTextBoxsağlamak için özelliğini true olarak ayarlar. Örnek, yöntemini çağırarak sürükleme işlemini başlatmak için öğesinin DoDragDrop olayını ListBox kullanırMouseDown. Örnekte, içine sürüklenen DragEnterRichTextBox bir öğenin geçerli bir veri türü olup olmadığını belirlemek için olayı kullanılır. Olay, DragDrop sürüklenen bir öğenin içindeki geçerli imleç konumundaki denetime RichTextBox asıl bırakma işlemini RichTextBoxgerçekleştirir. Bu örnek, ve DragEnter olaylarının DragDrop örnekte tanımlanan olay işleyicilerine bağlı olmasını gerektirir.

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

Açıklamalar

Bu yöntem, denetim içinde belirli bir konumda bulunan öğeyi belirlemenizi sağlar. Bir kullanıcı öğesine sağ tıkladığında ListBoxlistedeki hangi öğenin seçileceğini belirlemek için bu yöntemi kullanabilirsiniz. İmlecin konumu belirlenebilir ve yöntemin x ve y parametrelerine IndexFromPoint geçirilebilir ve kullanıcının fareye sağ tıkladığı öğeyi belirleyebilir. Daha sonra kullanıcıya, belirli bir öğeyi temel alan görevler ve özellikler sağlamak için bir kısayol menüsü görüntüleyebilirsiniz.

Şunlara uygulanır