ListView.GetItemAt(Int32, Int32) Method

Definition

Retrieves the item at the specified location.

C#
public System.Windows.Forms.ListViewItem GetItemAt(int x, int y);
C#
public System.Windows.Forms.ListViewItem? GetItemAt(int x, int y);

Parameters

x
Int32

The x-coordinate of the location to search for an item (expressed in client coordinates).

y
Int32

The y-coordinate of the location to search for an item (expressed in client coordinates).

Returns

A ListViewItem that represents the item at the specified position. If there is no item at the specified location, the method returns null.

Examples

The following code example demonstrates the use of the PictureBox and ListView controls. The PictureBox is initialized by setting the PictureBox.BorderStyle and PictureBox.SizeMode properties using the BorderStyle and PictureBoxSizeMode enumerations, respectively. The ListView is populated with pictures from the Samples directory. The GetItemAt method is used in when handling the ListView control's MouseDown event to determine whether an item is selected. If an item is selected, the event handler displays the selected file by setting the PictureBox.Image property. The example requires the existence of JPG files located in the C:\documents and Settings\All Users\Documents\My Pictures\Sample Pictures directory. To run this example, paste the code into a form and call the InitializePictureBox and PopulateListView methods in the form's constructor or Load method. Ensure all events are associated with their event handlers.

C#
private void PopulateListView()
{
    ListView1.Width = 270;
    ListView1.Location = new System.Drawing.Point(10, 10);

    // Declare and construct the ColumnHeader objects.
    ColumnHeader header1, header2;
    header1 = new ColumnHeader();
    header2 = new ColumnHeader();

    // Set the text, alignment and width for each column header.
    header1.Text = "File name";
    header1.TextAlign = HorizontalAlignment.Left;
    header1.Width = 70;

    header2.TextAlign = HorizontalAlignment.Left;
    header2.Text = "Location";
    header2.Width = 200;

    // Add the headers to the ListView control.
    ListView1.Columns.Add(header1);
    ListView1.Columns.Add(header2);

    // Specify that each item appears on a separate line.
    ListView1.View = View.Details;
    
    // Populate the ListView.Items property.
    // Set the directory to the sample picture directory.
    System.IO.DirectoryInfo dirInfo = 
        new System.IO.DirectoryInfo(
        "C:\\Documents and Settings\\All Users" +
        "\\Documents\\My Pictures\\Sample Pictures");

    // Get the .jpg files from the directory
    System.IO.FileInfo[] files = dirInfo.GetFiles("*.jpg");

    // Add each file name and full name including path
    // to the ListView.
    if (files != null)
    {
        foreach ( System.IO.FileInfo file in files )
        {
            ListViewItem item = new ListViewItem(file.Name);
            item.SubItems.Add(file.FullName);
            ListView1.Items.Add(item);
        }
    }
}

private void InitializePictureBox()
{
    PictureBox1 = new PictureBox();

    // Set the location and size of the PictureBox control.
    this.PictureBox1.Location = new System.Drawing.Point(70, 120);
    this.PictureBox1.Size = new System.Drawing.Size(140, 140);
    this.PictureBox1.TabStop = false;

    // Set the SizeMode property to the StretchImage value.  This
    // will shrink or enlarge the image as needed to fit into
    // the PictureBox.
    this.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

    // Set the border style to a three-dimensional border.
    this.PictureBox1.BorderStyle = BorderStyle.Fixed3D;

    // Add the PictureBox to the form.
    this.Controls.Add(this.PictureBox1);
}

private void ListView1_MouseDown(object sender, MouseEventArgs e)
{

    ListViewItem selection = ListView1.GetItemAt(e.X, e.Y);

    // If the user selects an item in the ListView, display
    // the image in the PictureBox.
    if (selection != null)
    {
        PictureBox1.Image = System.Drawing.Image.FromFile(
            selection.SubItems[1].Text);
    }
}

Remarks

The GetItemAt method lets you determine which item is located at a specific location within the client region of the ListView control. You can use this method when the user clicks or right-clicks a subitem (when the View property is set to View.Details) and you want to determine which item owns the subitem that was clicked based on the mouse coordinates at the time the user clicked the mouse.

Note

When the FullRowSelect property value is false, this method returns an item only when the specified location is within the bounds of the parent item (which is displayed in the first column when the control is in details mode). You must set the FullRowSelect property to true to retrieve an item with this method given a location within the bounds of a subitem. You can also use the HitTest method to retrieve detailed information about the item at a particular location.

To do drag-and-drop repositioning of items within a ListView, you might want to use the ListViewInsertionMark.NearestIndex method rather than the GetItemAt method. The ListViewInsertionMark.NearestIndex method finds the closest item regardless of where the mouse pointer is located. The GetItemAt method, on the other hand, returns null if there is no item at the specified location.

Note

The ListViewInsertionMark class is supported only on Windows XP and Windows Server 2003 when your application calls the Application.EnableVisualStyles method.

Applies to

Product Versions
.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

See also