vb.net DataGridView Cell、RowHeader 和 ColumnHeader 的不同 ContextMenuStrip
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6761320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different ContextMenuStrip for DataGridView Cell, RowHeader and ColumnHeader
提问by Smith
I want to set different ContextMenuStrip
for DataGridView
Cells
, RowHeaders
and ColumnHeaders
.
我想设置不同ContextMenuStrip
的DataGridView
Cells
,RowHeaders
和ColumnHeaders
。
The idea is that when I right-click any of these items, a different ContextMenuStrip
is displayed. How do I do this?
这个想法是,当我右键单击这些项目中的任何一个时,ContextMenuStrip
会显示不同的内容。我该怎么做呢?
回答by Jay Riggs
Use the DataGridView's MouseDown
event to test if the right mouse has been clicked and if so use the associated HitTestInfo
property to determine if a cell, row or column has been clicked. Use this information to display the ContextMenuStrip you need.
使用 DataGridView 的MouseDown
事件来测试是否单击了鼠标右键,如果是,则使用关联的HitTestInfo
属性来确定是否单击了单元格、行或列。使用此信息显示您需要的 ContextMenuStrip。
Here's an example MouseDown
event that does this. To try the sample drop a DataGridView and three ContentMenuStrips on a form. Name the ContentMenuStrips mnuCell, mnuRow and mnuColumn.
这MouseDown
是执行此操作的示例事件。要尝试该示例,请在表单上放置一个 DataGridView 和三个 ContentMenuStrips。将 ContentMenuStrips 命名为 mnuCell、mnuRow 和 mnuColumn。
Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim ht As DataGridView.HitTestInfo
ht = Me.DataGridView1.HitTest(e.X, e.Y)
If ht.Type = DataGridViewHitTestType.Cell Then
DataGridView1.ContextMenuStrip = mnuCell
mnuCell.Items(0).Text = String.Format("This is the cell at {0}, {1}", ht.ColumnIndex, ht.RowIndex)
ElseIf ht.Type = DataGridViewHitTestType.RowHeader Then
DataGridView1.ContextMenuStrip = mnuRow
mnuRow.Items(0).Text = "This is row " + ht.RowIndex.ToString()
ElseIf ht.Type = DataGridViewHitTestType.ColumnHeader Then
DataGridView1.ContextMenuStrip = mnuColumn
mnuColumn.Items(0).Text = "This is col " + ht.ColumnIndex.ToString()
End If
End If
End Sub
Here I'm assigning the DataGridView's ContextMenuStrip property to the ContextMenuStrip appropriate for the item right clicked (cell, row or column). To demonstrate how you might further customize the behavior of the ContextMenuStrips I'm also setting the text in each ContentMenuStrips' menu item.
在这里,我将 DataGridView 的 ContextMenuStrip 属性分配给适合右键单击的项目(单元格、行或列)的 ContextMenuStrip。为了演示如何进一步自定义 ContextMenuStrips 的行为,我还在每个 ContentMenuStrips 的菜单项中设置文本。
回答by Miroslav Zadravec
On MouseDown event of DataGridView, use DataGridView.HitTest method to check what was clicked. Then you can switch context menus depending on what was clicked.
在 DataGridView 的 MouseDown 事件上,使用 DataGridView.HitTest 方法检查点击了什么。然后您可以根据单击的内容切换上下文菜单。