鼠标悬停在vb.net上的datagridview上更改行字体

时间:2012-12-23 22:30:10

标签: vb.net

我希望当鼠标在一行上时,无论选择哪一行,都会在datagridview行上显示下划线字体。

我得到了 - 一半:)

Private Sub aDgv_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles aDgv.MouseMove

    Dim hit As DataGridView.HitTestInfo = aDgv.HitTest(e.X, e.Y)
    If hit.Type = DataGridViewHitTestType.Cell Then
        aDgv.Rows(hit.RowIndex).DefaultCellStyle.Font = New Font(aDgv.DefaultCellStyle.Font, FontStyle.Underline)
    End If
End Sub

所以,当我来到该行的行文本变为下划线(如预期的那样)时,当我移动到下一行时,那些下一行变为下划线但以前不会回到正常字体。

该怎么办只有鼠标悬停的行中的文字变为下划线 如何在鼠标转到其他行时将字体重置为正常状态?

1 个答案:

答案 0 :(得分:3)

支持正常Font只需使用CellMouseLeave事件

Private Sub DataGridView1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseMove
    Dim normalFont = New Font(DataGridView1.DefaultCellStyle.Font, FontStyle.Regular)
    Dim hit As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)
    If hit.Type = DataGridViewHitTestType.Cell Then
        If DataGridView1.Rows(hit.RowIndex).Cells(hit.ColumnIndex).FormattedValue.ToString().Trim().Length > 0 Then
            DataGridView1.Rows(hit.RowIndex).DefaultCellStyle.Font = New Font(DataGridView1.DefaultCellStyle.Font, FontStyle.Underline)
        Else
            DataGridView1.Rows(hit.RowIndex).DefaultCellStyle.Font = normalFont
        End If
    End If
End Sub

Private Sub DataGridView1_CellMouseLeave(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave
    Dim normalFont = New Font(DataGridView1.DefaultCellStyle.Font, FontStyle.Regular)
    If (e.ColumnIndex > -1) Then
        If e.RowIndex > -1 Then
            If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).FormattedValue.ToString().Trim().Length > 0 Then
                DataGridView1.Rows(e.RowIndex).DefaultCellStyle.Font = normalFont
            Else
                DataGridView1.Rows(e.RowIndex).DefaultCellStyle.Font = normalFont
            End If
        End If
    End If
End Sub
相关问题