ListBox.IndexFromPoint 方法

定義

傳回位於指定座標處的項目之以零為起始的索引。

多載

IndexFromPoint(Point)

傳回位於指定座標處的項目之以零為起始的索引。

IndexFromPoint(Int32, Int32)

傳回位於指定座標處的項目之以零為起始的索引。

IndexFromPoint(Point)

來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs

傳回位於指定座標處的項目之以零為起始的索引。

C#
public int IndexFromPoint(System.Drawing.Point p);

參數

p
Point

Point 物件,含有用來取得項目索引的座標。

傳回

在指定座標處找到的項目之以零起始的索引,如果找不到相符的項目,則傳回 ListBox.NoMatches

範例

下列程式碼範例示範如何使用包含要放入 RichTextBox 控制項之專案的控制項來執行拖放作業 ListBox 。 表單的建構函式會將 AllowDrop 屬性設定為 true ,讓拖放作業出現在 中 RichTextBox 。 此範例會 MouseDown 使用 的 ListBox 事件,藉由呼叫 DoDragDrop 方法來啟動拖曳作業。 此範例會 DragEnter 使用 事件來判斷要拖曳到 的專案 RichTextBox 是否為有效的資料類型。 事件 DragDrop 會在 內目前游標位置執行拖曳專案的實際置放至 RichTextBox 控制項。 RichTextBox 這個範例要求 DragDropDragEnter 事件已經連接到範例中定義的事件處理常式。

C#
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();
}

備註

這個方法可讓您判斷哪個專案位於 控制項內的特定位置。 您可以使用這個方法來判斷當使用者以滑鼠右鍵按一下 ListBox 時,會選取清單中的哪個專案。 您可以判斷游標的位置,並傳遞至 p 方法的 IndexFromPoint 參數,以判斷使用者以滑鼠右鍵按一下哪個專案。 然後,您可以向使用者顯示快捷方式功能表,根據特定專案提供工作和功能。

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

IndexFromPoint(Int32, Int32)

來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs

傳回位於指定座標處的項目之以零為起始的索引。

C#
public int IndexFromPoint(int x, int y);

參數

x
Int32

要進行搜尋之目標位置的 X 座標。

y
Int32

要進行搜尋之目標位置的 Y 座標。

傳回

在指定座標處找到的項目之以零起始的索引,如果找不到相符的項目,則傳回 ListBox.NoMatches

範例

下列程式碼範例示範如何使用包含要放入 RichTextBox 控制項之專案的控制項來執行拖放作業 ListBox 。 表單的建構函式會將 AllowDrop 屬性設定為 true ,讓拖放作業出現在 中 RichTextBox 。 此範例會 MouseDown 使用 的 ListBox 事件,藉由呼叫 DoDragDrop 方法來啟動拖曳作業。 此範例會 DragEnter 使用 事件來判斷要拖曳到 的專案 RichTextBox 是否為有效的資料類型。 事件 DragDrop 會在 內目前游標位置執行拖曳專案的實際置放至 RichTextBox 控制項。 RichTextBox 這個範例要求 DragDropDragEnter 事件已經連接到範例中定義的事件處理常式。

C#
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();
}

備註

這個方法可讓您判斷位於 控制項內特定位置的專案。 您可以使用這個方法來判斷當使用者以滑鼠右鍵按一下 ListBox 時,會選取清單中的哪個專案。 您可以判斷資料指標的位置,並傳遞至 x 方法的 IndexFromPointy 參數,以判斷使用者以滑鼠右鍵按一下哪個專案。 然後,您可以向使用者顯示快捷方式功能表,根據特定專案提供工作和功能。

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10