2012-12-23 2 views
3

어떤 행을 선택하든 상관없이 마우스가 행 위에있을 때 밑줄 글꼴을 가져 오려고합니다. 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 

따라서는, (예상대로) 밑줄된다 :) 반, 나는 그 다음 행에 밑줄된다 다음 행으로 이동 때 이전에는 일반 글꼴로 돌아 가지 않습니다.

마우스 오버되는 텍스트가 밑줄로 표시됩니다.
마우스를 다른 행으로 이동하면 글꼴을 보통으로 재설정하는 방법은 무엇입니까?

답변

3

은 일반 Font 그냥

이 작동
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 
+0

은, 대단히 감사합니다 CellMouseLeave 이벤트를 사용하여 백업하려면! 행에서 왼쪽 또는 오른쪽 밑줄을 깜박이면 (텍스트가 없거나 무엇이 ??) 문제가됩니다. 이것을 피하는 방법에 대해 알고 있습니까? –

+0

내가 내 대답을 수정했습니다 :) – spajce

+0

미안하지만,해야 할 작업을 얻을 수 없습니다. 내 IDE에서 IsNullOrWhiteSpace가 Strings 멤버가 아니므로 가장 가까운 IsNullOrEmpty를 선택했기 때문에 String.IsNullOrWhiteSpace를 IsNullOrEmpty로 변경해야합니다. 지금보다 더 깜박입니다. 무엇을해야합니까? –

관련 문제