The blog has moved to a new address. The blog is now located at http://devintelligence.com

Adsense

Wednesday, May 17, 2006

HitTestInfo Overview

Sometimes in applications you may need to recognize which element is located at the specified screen coordinates. For instance, you may have to determine which part of a DataGridView the user has clicked or double-clicked. For this purpose, DataGridView implements the HitTest method.

For demonstrative purposes on how to process data provided by a HitTestInfo object, you can make use of the following example. It handles the DataGridView.MouseMove event and shows information about a part under the mouse cursor.

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)

{

System.Windows.Forms.DataGridView.HitTestInfo hitInfo = dataGridView1.HitTest(e.X, e.Y);

this.listBox1.Items.Clear();


// displaying dataGridView related information defined a point under the mouse cursor

listBox1.Items.Add("Point: " + new Point(e.X, e.Y) );

listBox1.Items.Add("Type: " + hitInfo.Type.ToString());

listBox1.Items.Add("ColumnIndex: " + hitInfo.ColumnIndex);

listBox1.Items.Add("RowIndex: " + hitInfo.RowIndex);


if (hitInfo.ColumnIndex != -1)

{

DataGridViewColumn column = dataGridView1.Columns[hitInfo.ColumnIndex];

listBox1.Items.Add("Column Caption: " + column.DataPropertyName );

}

else

{

listBox1.Items.Add("No Column");

}


}

Technorati : , , , , ,
Ice Rocket : , , , , ,

No comments: