在鼠标悬停时更改 datagridview 上的行字体,vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14015216/
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
Change row font on datagridview on mouse over, vb.net
提问by Wine Too
I want to get underline font on datagridview row when mouse is over a row no matter which row is selected.
无论选择哪一行,当鼠标悬停在一行上时,我都想在 datagridview 行上获得下划线字体。
I get that - a half :)
我明白了 - 一半 :)
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
So, when I come over row text in that row becomes underlined (as expected), when I move to next row then those next row becomes underlined but previously don't back to normal font.
因此,当我遇到该行中的行文本时会带下划线(正如预期的那样),当我移动到下一行时,下一行会带下划线但以前不会恢复正常字体。
What to do that only text in row on which is mouseover become underlined.
How to reset font to normal when mouse goes to other row?
怎么做,只有鼠标悬停在行中的文本才带有下划线。
鼠标移到另一行时如何将字体重置为正常?
采纳答案by spajce
To back the normal Fontjust use the CellMouseLeaveevent
要恢复正常,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

