//对于datagridview使用CellMouseDown事件
private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
dataGridView.ClearSelection();
dataGridView.Rows[e.RowIndex].Selected = true;
dataGridView.CurrentCell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
contextMenuStrip_ListViewItemRightClick.Show(MousePosition.X, MousePosition.Y);
}
}
}
//对于treeview可以使用mousedown事件
方法一:
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TreeNode node = this.treeView1.GetNodeAt(e.Location);
if (node != null)
{
this.treeView1.SelectedNode = node;
}
}
}
void jcsTreeView1_MouseDown(object sender, MouseEventArgs e)
{
System.Windows.Forms.TreeViewHitTestInfo hittestinfo = this.jcsTreeView1.HitTest(e.X ,e.Y);
if (hittestinfo.Node != null)
{
TreeViewHitTestLocations loc = hittestinfo.Location;
if(loc == TreeViewHitTestLocations.Label )
MessageBox.Show(hittestinfo.Node.Text);
}
}