DataGridViewColumn.ContextMenuStrip 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置列的快捷菜单。
public:
virtual property System::Windows::Forms::ContextMenuStrip ^ ContextMenuStrip { System::Windows::Forms::ContextMenuStrip ^ get(); void set(System::Windows::Forms::ContextMenuStrip ^ value); };
public override System.Windows.Forms.ContextMenuStrip ContextMenuStrip { get; set; }
public override System.Windows.Forms.ContextMenuStrip? ContextMenuStrip { get; set; }
member this.ContextMenuStrip : System.Windows.Forms.ContextMenuStrip with get, set
Public Overrides Property ContextMenuStrip As ContextMenuStrip
属性值
与当前 ContextMenuStrip 关联的 DataGridViewColumn。 默认值为 null
。
示例
下面的代码示例使用 ContextMenuStrip 属性添加用于更改单元格背景色的功能。
ToolStripMenuItem^ toolStripItem1;
void AddContextMenu()
{
toolStripItem1->Text = L"Redden";
toolStripItem1->Click += gcnew EventHandler( this, &DataGridViewColumnDemo::toolStripItem1_Click );
System::Windows::Forms::ContextMenuStrip^ strip = gcnew System::Windows::Forms::ContextMenuStrip;
IEnumerator^ myEnum = dataGridView->Columns->GetEnumerator();
while ( myEnum->MoveNext() )
{
DataGridViewColumn^ column = safe_cast<DataGridViewColumn^>(myEnum->Current);
column->ContextMenuStrip = strip;
column->ContextMenuStrip->Items->Add( toolStripItem1 );
}
}
DataGridViewCellEventArgs^ mouseLocation;
// Change the cell's color.
void toolStripItem1_Click( Object^ /*sender*/, EventArgs^ /*args*/ )
{
dataGridView->Rows[ mouseLocation->RowIndex ]->Cells[ mouseLocation->ColumnIndex ]->Style->BackColor = Color::Red;
}
// Deal with hovering over a cell.
void dataGridView_CellMouseEnter( Object^ /*sender*/, DataGridViewCellEventArgs^ location )
{
mouseLocation = location;
}
ToolStripMenuItem toolStripItem1 = new ToolStripMenuItem();
private void AddContextMenu()
{
toolStripItem1.Text = "Redden";
toolStripItem1.Click += new EventHandler(toolStripItem1_Click);
ContextMenuStrip strip = new ContextMenuStrip();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
column.ContextMenuStrip = strip;
column.ContextMenuStrip.Items.Add(toolStripItem1);
}
}
private DataGridViewCellEventArgs mouseLocation;
// Change the cell's color.
private void toolStripItem1_Click(object sender, EventArgs args)
{
dataGridView.Rows[mouseLocation.RowIndex]
.Cells[mouseLocation.ColumnIndex].Style.BackColor
= Color.Red;
}
// Deal with hovering over a cell.
private void dataGridView_CellMouseEnter(object sender,
DataGridViewCellEventArgs location)
{
mouseLocation = location;
}
WithEvents toolStripItem1 As New ToolStripMenuItem()
Private Sub AddContextMenu()
toolStripItem1.Text = "Redden"
Dim strip As New ContextMenuStrip()
For Each column As DataGridViewColumn _
In dataGridView.Columns()
column.ContextMenuStrip = strip
column.ContextMenuStrip.Items.Add(toolStripItem1)
Next
End Sub
' Change the cell's color.
Private Sub toolStripItem1_Click(ByVal sender As Object, _
ByVal args As EventArgs) _
Handles toolStripItem1.Click
dataGridView.Rows(mouseLocation.RowIndex) _
.Cells(mouseLocation.ColumnIndex) _
.Style.BackColor = Color.Red
End Sub
Private mouseLocation As DataGridViewCellEventArgs
' Deal with hovering over a cell.
Private Sub dataGridView_CellMouseEnter(ByVal sender As Object, _
ByVal location As DataGridViewCellEventArgs) _
Handles DataGridView.CellMouseEnter
mouseLocation = location
End Sub
注解
当用户在列的显示区域中单击鼠标右键时,将显示快捷菜单。