DataGridView.CellMouseEnter 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
鼠标指针进入单元格时发生。
public:
event System::Windows::Forms::DataGridViewCellEventHandler ^ CellMouseEnter;
public event System.Windows.Forms.DataGridViewCellEventHandler CellMouseEnter;
public event System.Windows.Forms.DataGridViewCellEventHandler? CellMouseEnter;
member this.CellMouseEnter : System.Windows.Forms.DataGridViewCellEventHandler
Public Custom Event CellMouseEnter As DataGridViewCellEventHandler
事件类型
示例
下面的代码示例演示 CellMouseEnter Tic-Tac-Toe 游戏实现中的事件处理程序,该处理程序使用 控件中的 DataGridView 图像列。 事件处理程序使用单元格值来确定要显示的光标和工具提示。
此代码是How to: Work with Image Columns in the Windows 窗体 DataGridView 控件中显示的较大示例的一部分。
void dataGridView1_CellMouseEnter( Object^ sender, DataGridViewCellEventArgs^ e )
{
Bitmap^ markingUnderMouse = dynamic_cast<Bitmap^>(dataGridView1->Rows[ e->RowIndex ]->Cells[ e->ColumnIndex ]->Value);
if ( markingUnderMouse == blank )
{
dataGridView1->Cursor = Cursors::Default;
}
else
if ( markingUnderMouse == o || markingUnderMouse == x )
{
dataGridView1->Cursor = Cursors::No;
ToolTip(e,true);
}
}
void ToolTip( DataGridViewCellEventArgs^ e, bool showTip )
{
DataGridViewImageCell^ cell = dynamic_cast<DataGridViewImageCell^>(dataGridView1->Rows[ e->RowIndex ]->Cells[ e->ColumnIndex ]);
DataGridViewImageColumn^ imageColumn = dynamic_cast<DataGridViewImageColumn^>(dataGridView1->Columns[ cell->ColumnIndex ]);
if ( showTip )
cell->ToolTipText = imageColumn->Description;
else
{
cell->ToolTipText = String::Empty;
}
}
void dataGridView1_CellMouseLeave( Object^ sender, DataGridViewCellEventArgs^ e )
{
ToolTip( e, false );
dataGridView1->Cursor = Cursors::Default;
}
private void dataGridView1_CellMouseEnter(object sender,
DataGridViewCellEventArgs e)
{
Bitmap markingUnderMouse = (Bitmap)dataGridView1.
Rows[e.RowIndex].
Cells[e.ColumnIndex].Value;
if (markingUnderMouse == blank)
{
dataGridView1.Cursor = Cursors.Default;
}
else if (markingUnderMouse == o || markingUnderMouse == x)
{
dataGridView1.Cursor = Cursors.No;
ToolTip(e, true);
}
}
private void ToolTip(DataGridViewCellEventArgs e, bool showTip)
{
DataGridViewImageCell cell = (DataGridViewImageCell)
dataGridView1
.Rows[e.RowIndex].Cells[e.ColumnIndex];
DataGridViewImageColumn imageColumn =
(DataGridViewImageColumn)
dataGridView1.Columns[cell.ColumnIndex];
if (showTip)
{
cell.ToolTipText = imageColumn.Description;
}
else { cell.ToolTipText = String.Empty; }
}
private void dataGridView1_CellMouseLeave(object sender,
DataGridViewCellEventArgs e)
{
ToolTip(e, false);
dataGridView1.Cursor = Cursors.Default;
}
Private Sub dataGridView1_CellMouseEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dataGridView1.CellMouseEnter
Dim markingUnderMouse As Bitmap = _
CType(dataGridView1.Rows(e.RowIndex). _
Cells(e.ColumnIndex).Value, Bitmap)
If markingUnderMouse Is blank Then
dataGridView1.Cursor = Cursors.Default
ElseIf markingUnderMouse Is o OrElse markingUnderMouse Is x Then
dataGridView1.Cursor = Cursors.No
ToolTip(e)
End If
End Sub
Private Sub ToolTip( _
ByVal e As DataGridViewCellEventArgs)
Dim cell As DataGridViewImageCell = _
CType(dataGridView1.Rows(e.RowIndex). _
Cells(e.ColumnIndex), DataGridViewImageCell)
Dim imageColumn As DataGridViewImageColumn = _
CType(dataGridView1.Columns(cell.ColumnIndex), _
DataGridViewImageColumn)
cell.ToolTipText = imageColumn.Description
End Sub
Private Sub dataGridView1_CellMouseLeave(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dataGridView1.CellMouseLeave
dataGridView1.Cursor = Cursors.Default
End Sub
注解
有关如何处理事件的详细信息,请参阅 处理和引发事件。