Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ListBox.IndexFromPoint Method (Point)

Returns the zero-based index of the item at the specified coordinates.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public int IndexFromPoint (
	Point p
)
public int IndexFromPoint (
	Point p
)
public function IndexFromPoint (
	p : Point
) : int

Parameters

p

A Point object containing the coordinates used to obtain the item index.

Return Value

The zero-based index of the item found at the specified coordinates; returns ListBox.NoMatches if no match is found.

This method enables you to determine which item is located at a specific location within the control. You can use this method to determine which item within the list is selected when a user right-clicks the ListBox. The location of the cursor can be determined and passed to the p parameter of the IndexFromPoint method to determine which item the user right-clicked the mouse over. You can then display a shortcut menu to the user to provide tasks and features based on the specific item.

The following code example demonstrates how to perform drag-and-drop operations using a ListBox control that contains items to drop into a RichTextBox control. The constructor of the form sets the AllowDrop property to true to enable drag-and-drop operations to occur in the RichTextBox. The example uses the MouseDown event of the ListBox to start the drag operation by calling the DoDragDrop method. The example uses the DragEnter event to determine if an item being dragged into the RichTextBox is a valid data type. The DragDrop event performs the actual dropping of a dragged item into the RichTextBox control at the current cursor location within the RichTextBox. This example requires that the DragDrop and DragEnter events have been connected to the event handlers defined in the example.

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 Form1()
{
    InitializeComponent();
    //Sets the control to allow drops, and then adds the necessary 
    //event handlers.
    this.richTextBox1.set_AllowDrop(true);
} //Form1

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.get_X(), e.get_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.get_Items().get_Item(index).ToString(), 
            DragDropEffects.Copy);
    }
} //listBox1_MouseDown

private void richTextBox1_DragEnter(Object sender, DragEventArgs e)
{
    // If the data is text, copy the data to the RichTextBox control.
    if (e.get_Data().GetDataPresent("Text")) {
        e.set_Effect(DragDropEffects.Copy);
    }
} //richTextBox1_DragEnter

private void richTextBox1_DragDrop(Object sender, DragEventArgs e)
{
    // Paste the text into the RichTextBox where at selection location.
    richTextBox1.set_SelectedText(e.get_Data().GetData("System.String", true).
        ToString());
} //richTextBox1_DragDrop

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft