---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting '列ヘッダーかどうか調べる If e.ColumnIndex < 0 And e.RowIndex >= 0 Then 'セルを描画する e.Paint(e.ClipBounds, DataGridViewPaintParts.All) '←ここで発生 コメントアウトするとフォーム拡大縮小時にゴミが残る…
'行番号を描画する範囲を決定する 'e.AdvancedBorderStyleやe.CellStyle.Paddingは無視しています Dim indexRect As Rectangle = e.CellBounds indexRect.Inflate(-2, -2) '行番号を描画する TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), e.CellStyle.Font, indexRect, e.CellStyle.ForeColor, TextFormatFlags.Right Or TextFormatFlags.VerticalCenter) '描画が完了したことを知らせる e.Handled = True End If
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray End Sub ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
呼出し元 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Private Sub DGVDataAdd(workStr As List(Of String())) DataGridView1.Rows.Clear() On Error GoTo ErrorNext System.Threading.Monitor.Enter(workStr) Dim ws As List(Of String()) = workStr System.Threading.Monitor.Exit(workStr) System.Threading.Monitor.Enter(ws) Dim x As Integer = 0 For Each work In ws System.Threading.Monitor.Enter(work) DataGridView1.Rows.Add(work) DataGridView1(LogData.LogKinds, x).Style.BackColor = LogKind_ColorChange(work) DataGridView1(LogData.Operation, x).Style.ForeColor = Operation_ColorChange(work) x += 1 System.Threading.Monitor.Exit(work) Next System.Threading.Monitor.Exit(ws) ErrorNext: DataGridView1.Refresh() End Sub ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- いろんなスレッドに呼ばれるので、エラーが出て最終的に「On Error Resume Next」を付けました^^;
呼出し元の呼出し元 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Private Delegate Sub DGVAddInvoke(DataStr As List(Of String()))
''' <summary> ''' AlertLog変更イベント ''' </summary> Private Sub _LogStock__AlartLogEvent(sender As Object, e As EventArgs) Handles _LogStock._AlertLogEvent If DispLevel = LogEventArgs.Kinds.Alart Then Invoke(New DGVAddInvoke(AddressOf DGVDataAdd), _LogStock.AlertLog) End If End Sub
''' <summary> ''' OperateLog変更イベント ''' </summary> Private Sub _LogStock__OperateLogEvent(sender As Object, e As EventArgs) Handles _LogStock._OperateLogEvent If DispLevel = LogEventArgs.Kinds.Operate Then Invoke(New DGVAddInvoke(AddressOf DGVDataAdd), _LogStock.OperateLog) End If End Sub
''' <summary> ''' SystemLog変更イベント ''' </summary> Private Sub _LogStock__SystemLogEvent(sender As Object, e As EventArgs) Handles _LogStock._SystemLogEvent If DispLevel = LogEventArgs.Kinds.System Then Invoke(New DGVAddInvoke(AddressOf DGVDataAdd), _LogStock.SystemLog) End If End Sub
''' <summary> ''' DebugLog変更イベント ''' </summary> Private Sub _LogStock__DebugLogEvent(sender As Object, e As EventArgs) Handles _LogStock._DebugLogEvent If DispLevel = LogEventArgs.Kinds.Debug Then Invoke(New DGVAddInvoke(AddressOf DGVDataAdd), _LogStock.DebugLog) End If End Sub ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 以上3つのメソッドはLogDispクラスで、イベントはLogクラスから飛んできます。
Logクラス ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Public Delegate Sub LogEventHandler(sender As Object, e As EventArgs) Public Event _AlertLogEvent As LogEventHandler Public Event _OperateLogEvent As LogEventHandler Public Event _SystemLogEvent As LogEventHandler Public Event _DebugLogEvent As LogEventHandler
Private _AlertLog As New List(Of String()) Private _OperateLog As New List(Of String()) Private _SystemLog As New List(Of String()) Private _DebugLog As New List(Of String())
Public Sub Write(sender As Object, e As LogEventArgs)
〜〜〜 (中略) 〜〜〜
If e.LogKinds = LogEventArgs.Kinds.System Then _DebugLog.Insert(0, WriteData.Split(",")) _SystemLog.Insert(0, WriteData.Split(",")) If _SystemLog.Count > _MaxDispLog Then _SystemLog.RemoveAt(_MaxDispLog) RaiseEvent _SystemLogEvent(Me, New EventArgs()) RaiseEvent _DebugLogEvent(Me, New EventArgs()) Exit Sub End If End Sub ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
最終形 ----------------------------------------------------------------------------------------------------------------------------------------------------- Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting On Error Resume Next '列ヘッダーかどうか調べる If e.ColumnIndex < 0 And e.RowIndex >= 0 Then 'セルを描画する e.Paint(e.ClipBounds, DataGridViewPaintParts.All)
'行番号を描画する範囲を決定する 'e.AdvancedBorderStyleやe.CellStyle.Paddingは無視しています Dim indexRect As Rectangle = e.CellBounds indexRect.Inflate(-2, -2) '行番号を描画する TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), e.CellStyle.Font, indexRect, e.CellStyle.ForeColor, TextFormatFlags.Right Or TextFormatFlags.VerticalCenter) '描画が完了したことを知らせる e.Handled = True End If
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray End Sub
Private Sub DGVDataAdd(workStr As List(Of String())) DataGridView1.Rows.Clear() On Error GoTo ErrorNext Dim ws As List(Of String()) = workStr Dim x As Integer = 0 For Each work In ws DataGridView1.Rows.Add(work) DataGridView1(LogData.LogKinds, x).Style.BackColor = LogKind_ColorChange(work) DataGridView1(LogData.Operation, x).Style.ForeColor = Operation_ColorChange(work) x += 1 Next ErrorNext: DataGridView1.Refresh() End Sub