活动单元格未使用 CellPainting 突出显示
Active Cell not highlighting with CellPainting
(第一次发帖,请轻喷)
我的表单上有一个 DataGridView,它使当前行和列中的单元格突出显示。我还添加了一个 CellPainting 部分,为活动单元格提供更粗的边框。代码如下:
Private Sub DGV_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting
Try
Dim CRow As Integer = DGV.CurrentCell.RowIndex
Dim CCol As Integer = DGV.CurrentCell.ColumnIndex
If e.RowIndex = CRow AndAlso e.ColumnIndex = CCol Then
Using BBrush As Brush = New SolidBrush(Color.Black)
Using BPen As Pen = New Pen(BBrush, 2)
Dim CBRect As Rectangle = e.CellBounds
CBRect.Width -= 1
CBRect.Height -= 1
CBRect.X = rectDimensions.Left
CBRect.Y = rectDimensions.Top
e.Graphics.DrawRectangle(BPen, CBRect)
e.Handled = True
End Using
End Using
End If
Catch
End Try
End Sub
在为单元格添加边框的意义上,上面的代码工作正常,但是活动单元格没有显示为突出显示,只是显示为白色。我不明白,因为除了黑色边框没有 FillRectangle() 或任何其他颜色的规范,所以我假设它会保持单元格颜色相同,但它不会像其行和列中的其他单元格一样变蓝.
任何帮助将不胜感激:)
您的代码行 e.Handled = True
告诉 VB.NET 不要继续任何未来的事件处理,因此,单元格不会被涂成蓝色。如果删除该行,它应该可以正常工作。
编辑:您的边框没有完全应用,因为在您的代码中,您使用了事件提供的 Graphics
对象,它表示单元格 的图形 ,但不是整个 DataGridView
。因此,您不能使用事件获得的图形对象在单元格外部绘制。而您尝试绘制的边框恰好部分位于单元格之外。所以这是一个应该按预期工作的示例:
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If DataGridView1.CurrentCell Is Nothing Then Return
Dim graphics As Graphics = DataGridView1.CreateGraphics()
Dim CRow As Integer = DataGridView1.CurrentCell.RowIndex
Dim CCol As Integer = DataGridView1.CurrentCell.ColumnIndex
If e.RowIndex = CRow AndAlso e.ColumnIndex = CCol Then
Using BBrush As Brush = New SolidBrush(Color.Black)
Using BPen As Pen = New Pen(BBrush, 3)
Dim CBRect As Rectangle = e.CellBounds
CBRect.Width -= 1
CBRect.Height -= 1
graphics.DrawRectangle(BPen, CBRect)
End Using
End Using
End If
graphics.Dispose()
End Sub
这里,我们使用CreateGraphics()
方法获取一个Graphics
对象,代表整个DataGridView
。而且,只是另一条建议,如果可能,请使用逻辑语句而不是 Try
/Catch
,就像我在上面的代码(第一行)中所做的那样。此外,您不应将 Catch
块留空,因为它很难解决运行时错误。希望这对您有所帮助!
(第一次发帖,请轻喷)
我的表单上有一个 DataGridView,它使当前行和列中的单元格突出显示。我还添加了一个 CellPainting 部分,为活动单元格提供更粗的边框。代码如下:
Private Sub DGV_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting
Try
Dim CRow As Integer = DGV.CurrentCell.RowIndex
Dim CCol As Integer = DGV.CurrentCell.ColumnIndex
If e.RowIndex = CRow AndAlso e.ColumnIndex = CCol Then
Using BBrush As Brush = New SolidBrush(Color.Black)
Using BPen As Pen = New Pen(BBrush, 2)
Dim CBRect As Rectangle = e.CellBounds
CBRect.Width -= 1
CBRect.Height -= 1
CBRect.X = rectDimensions.Left
CBRect.Y = rectDimensions.Top
e.Graphics.DrawRectangle(BPen, CBRect)
e.Handled = True
End Using
End Using
End If
Catch
End Try
End Sub
在为单元格添加边框的意义上,上面的代码工作正常,但是活动单元格没有显示为突出显示,只是显示为白色。我不明白,因为除了黑色边框没有 FillRectangle() 或任何其他颜色的规范,所以我假设它会保持单元格颜色相同,但它不会像其行和列中的其他单元格一样变蓝.
任何帮助将不胜感激:)
您的代码行 e.Handled = True
告诉 VB.NET 不要继续任何未来的事件处理,因此,单元格不会被涂成蓝色。如果删除该行,它应该可以正常工作。
编辑:您的边框没有完全应用,因为在您的代码中,您使用了事件提供的 Graphics
对象,它表示单元格 的图形 ,但不是整个 DataGridView
。因此,您不能使用事件获得的图形对象在单元格外部绘制。而您尝试绘制的边框恰好部分位于单元格之外。所以这是一个应该按预期工作的示例:
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If DataGridView1.CurrentCell Is Nothing Then Return
Dim graphics As Graphics = DataGridView1.CreateGraphics()
Dim CRow As Integer = DataGridView1.CurrentCell.RowIndex
Dim CCol As Integer = DataGridView1.CurrentCell.ColumnIndex
If e.RowIndex = CRow AndAlso e.ColumnIndex = CCol Then
Using BBrush As Brush = New SolidBrush(Color.Black)
Using BPen As Pen = New Pen(BBrush, 3)
Dim CBRect As Rectangle = e.CellBounds
CBRect.Width -= 1
CBRect.Height -= 1
graphics.DrawRectangle(BPen, CBRect)
End Using
End Using
End If
graphics.Dispose()
End Sub
这里,我们使用CreateGraphics()
方法获取一个Graphics
对象,代表整个DataGridView
。而且,只是另一条建议,如果可能,请使用逻辑语句而不是 Try
/Catch
,就像我在上面的代码(第一行)中所做的那样。此外,您不应将 Catch
块留空,因为它很难解决运行时错误。希望这对您有所帮助!