Share via


ListBox.IndexFromPoint Metode

Definisi

Mengembalikan indeks berbasis nol item pada koordinat yang ditentukan.

Overload

IndexFromPoint(Point)

Mengembalikan indeks berbasis nol item pada koordinat yang ditentukan.

IndexFromPoint(Int32, Int32)

Mengembalikan indeks berbasis nol item pada koordinat yang ditentukan.

IndexFromPoint(Point)

Mengembalikan indeks berbasis nol item pada koordinat yang ditentukan.

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

Parameter

p
Point

Objek Point yang berisi koordinat yang digunakan untuk mendapatkan indeks item.

Mengembalikan

Indeks berbasis nol dari item yang ditemukan pada koordinat yang ditentukan; mengembalikan ListBox.NoMatches jika tidak ada kecocokan yang ditemukan.

Contoh

Contoh kode berikut menunjukkan cara melakukan operasi seret dan letakkan menggunakan ListBox kontrol yang berisi item untuk dijatuhkan RichTextBox ke kontrol. Konstruktor formulir mengatur AllowDrop properti ke untuk true memungkinkan operasi seret dan letakkan terjadi di RichTextBox. Contoh menggunakan MouseDown peristiwa ListBox untuk memulai operasi seret dengan memanggil DoDragDrop metode . Contoh menggunakan DragEnter peristiwa untuk menentukan apakah item yang diseret ke RichTextBox dalam adalah jenis data yang valid. Peristiwa melakukan DragDrop penghapusan aktual item yang diseret ke RichTextBox dalam kontrol di lokasi kursor saat ini dalam RichTextBox. Contoh ini mengharuskan DragDrop peristiwa dan DragEnter telah terhubung ke penanganan aktivitas yang ditentukan dalam contoh.

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

Keterangan

Metode ini memungkinkan Anda menentukan item mana yang terletak di lokasi tertentu dalam kontrol. Anda dapat menggunakan metode ini untuk menentukan item mana dalam daftar yang dipilih saat pengguna mengklik ListBoxkanan . Lokasi kursor dapat ditentukan dan diteruskan ke p parameter IndexFromPoint metode untuk menentukan item mana yang diklik kanan pengguna mouse. Anda kemudian dapat menampilkan menu pintasan kepada pengguna untuk menyediakan tugas dan fitur berdasarkan item tertentu.

Berlaku untuk

IndexFromPoint(Int32, Int32)

Mengembalikan indeks berbasis nol item pada koordinat yang ditentukan.

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

Parameter

x
Int32

Koordinat x lokasi untuk dicari.

y
Int32

Koordinat y lokasi untuk mencari.

Mengembalikan

Indeks berbasis nol dari item yang ditemukan pada koordinat yang ditentukan; mengembalikan ListBox.NoMatches jika tidak ada kecocokan yang ditemukan.

Contoh

Contoh kode berikut menunjukkan cara melakukan operasi seret dan letakkan menggunakan ListBox kontrol yang berisi item untuk dijatuhkan RichTextBox ke kontrol. Konstruktor formulir mengatur AllowDrop properti ke untuk true memungkinkan operasi seret dan letakkan terjadi di RichTextBox. Contoh menggunakan MouseDown peristiwa ListBox untuk memulai operasi seret dengan memanggil DoDragDrop metode . Contoh menggunakan DragEnter peristiwa untuk menentukan apakah item yang diseret ke RichTextBox dalam adalah jenis data yang valid. Peristiwa melakukan DragDrop penghapusan aktual item yang diseret ke RichTextBox dalam kontrol di lokasi kursor saat ini dalam RichTextBox. Contoh ini mengharuskan DragDrop peristiwa dan DragEnter telah terhubung ke penanganan aktivitas yang ditentukan dalam contoh.

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

Keterangan

Metode ini memungkinkan Anda menentukan item mana yang terletak di lokasi tertentu dalam kontrol. Anda dapat menggunakan metode ini untuk menentukan item mana dalam daftar yang dipilih saat pengguna mengklik ListBoxkanan . Lokasi kursor dapat ditentukan dan diteruskan ke x parameter IndexFromPoint dan y metode untuk menentukan item mana yang diklik kanan pengguna mouse. Anda kemudian dapat menampilkan menu pintasan kepada pengguna untuk menyediakan tugas dan fitur berdasarkan item tertentu.

Berlaku untuk