在我的应用程序中,我有2个潜艇似乎阻止了用户界面,RowPrePaint和CellFormatting。这两个子节点根据单元格内的值绘制我的DataGridView单元格的某些颜色。
基于另一个question我问我得出结论我需要另一个线程。 (Hans Passant推荐了一个BackgroundWorker)我当时还是一个新手,所以我只是找到解决问题的方法,现在他们已经赶上了我,因为像DateTimePicker这样的工具在我的用户界面上无法正常运行。
我一直在寻找示例,所有这些示例似乎都包含涉及进度条或类似内容的后台工作者。此外,我还遇到了一条规则,即规则永远不会在未创建它们的线程上访问UI对象。所以,如果这些潜艇甚至应该放在背景工作者身上,我会感到困惑吗?
所以有人知道任何类似的例子我可以学习吗?或者对如何将这些潜艇放在不同的线程上有任何建议?提前致谢。我已经包含了2个麻烦制造者潜艇
Private Sub DataGridView1_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
Dim Row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
If Row.Cells(3).Value = 7 Then
Row.Cells(0).Style.BackColor = Color.LightSkyBlue
Row.Cells(1).Style.BackColor = Color.LightSkyBlue
Row.Cells(2).Style.BackColor = Color.LightSkyBlue
Row.Cells(3).Style.BackColor = Color.LightSkyBlue
Row.Cells(4).Style.BackColor = Color.LightSkyBlue
ElseIf Row.Cells(3).Value = 8 Then
Row.Cells(0).Style.ForeColor = Color.White
Row.Cells(1).Style.ForeColor = Color.White
Row.Cells(2).Style.ForeColor = Color.White
Row.Cells(3).Style.ForeColor = Color.White
Row.Cells(4).Style.ForeColor = Color.White
End If
Dim value As Integer = Convert.ToInt32(Row.Cells(5).Value)
Select Case value
Case "0"
Row.Cells(5).Style.BackColor = Color.Empty
Row.Cells(5).Style.ForeColor = Color.White
Case "1"
Row.Cells(0).Style.BackColor = Color.LightSkyBlue
Row.Cells(1).Style.BackColor = Color.LightSkyBlue
Row.Cells(2).Style.BackColor = Color.LightSkyBlue
Row.Cells(4).Style.BackColor = Color.LightSkyBlue
Row.Cells(5).Style.BackColor = Color.LightSkyBlue
Row.Cells(5).Style.ForeColor = Color.LightSkyBlue
Case "2"
Row.Cells(0).Style.BackColor = Color.Chartreuse
Row.Cells(1).Style.BackColor = Color.Chartreuse
Row.Cells(2).Style.BackColor = Color.Chartreuse
Row.Cells(4).Style.BackColor = Color.Chartreuse
Row.Cells(5).Style.BackColor = Color.Chartreuse
Row.Cells(5).Style.ForeColor = Color.Chartreuse
Case "4"
Row.Cells(0).Style.BackColor = Color.Gainsboro
Row.Cells(1).Style.BackColor = Color.Gainsboro
Row.Cells(2).Style.BackColor = Color.Gainsboro
Row.Cells(4).Style.BackColor = Color.Gainsboro
Row.Cells(5).Style.BackColor = Color.Gainsboro
Row.Cells(5).Style.ForeColor = Color.Gainsboro
Case "5"
Row.Cells(0).Style.BackColor = Color.RoyalBlue
Row.Cells(1).Style.BackColor = Color.RoyalBlue
Row.Cells(2).Style.BackColor = Color.RoyalBlue
Row.Cells(4).Style.BackColor = Color.RoyalBlue
Row.Cells(5).Style.BackColor = Color.RoyalBlue
Row.Cells(5).Style.ForeColor = Color.RoyalBlue
End Select
For colval As Integer = 7 To 51 Step 2
Dim value6 As Integer = Row.Cells(colval).Value
Select Case value6
Case "0"
Row.Cells(colval).Style.BackColor = Color.Empty
Row.Cells(colval).Style.ForeColor = Color.White
Case "1"
Row.Cells(colval).Style.BackColor = Color.LightSkyBlue
Row.Cells(colval).Style.ForeColor = Color.LightSkyBlue
Case "2"
Row.Cells(colval).Style.BackColor = Color.Chartreuse
Row.Cells(colval).Style.ForeColor = Color.Chartreuse
Case "3"
Row.Cells(colval).Style.BackColor = Color.Orange
Row.Cells(colval).Style.ForeColor = Color.Orange
Case "4"
Row.Cells(colval).Style.BackColor = Color.Gainsboro
Row.Cells(colval).Style.ForeColor = Color.Gainsboro
Case "5"
Row.Cells(colval).Style.BackColor = Color.RoyalBlue
Row.Cells(colval).Style.ForeColor = Color.RoyalBlue
Case "6"
Row.Cells(colval).Style.BackColor = Color.Red
Row.Cells(colval).Style.ForeColor = Color.Red
End Select
Next colval
Dim value53 As Integer = Row.Cells(53).Value
Select Case value53
Case "0"
Row.Cells(53).Style.BackColor = Color.Empty
Row.Cells(53).Style.ForeColor = Color.White
Case "1"
Row.Cells(53).Style.BackColor = Color.Chartreuse
Row.Cells(53).Style.ForeColor = Color.Chartreuse
Case "4"
Row.Cells(53).Style.BackColor = Color.Gainsboro
Row.Cells(53).Style.ForeColor = Color.Gainsboro
End Select
End Sub
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
For k = 8 To 52 Step 2
Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex) 'Declare part of the code to equal row (runs the code faster)
If row.Cells(k).Value Is DBNull.Value Or row.Cells(k).Value Is Nothing Then Continue For 'If the database has a null cell or the cell is nothing continue
Dim firstDate As Date = DirectCast(row.Cells(k).Value, Date) 'Declare firstdate as a cell value and convert to date type
Dim N As Date = Now 'Declare N as the date today
Dim diff As TimeSpan = N - firstDate 'Declare diff as the tome between N and firstdate
If diff.Days > 14 AndAlso row.Cells(k + 1).Value = 0 Then 'If the difference between 'diff' is over 14 and col in front of k equals 0
row.Cells(k - 1).Value = 6 'Make col behind k = 6
ElseIf diff.Days > 14 AndAlso row.Cells(k + 1).Value <> 0 Then 'Or if the difference between 'diff' is over 14 and col in front of k does not equal 0
row.Cells(k - 1).Value = row.Cells(k - 3).Value 'Make col behind k equal the value of the col 3 rows behind k
End If
Next k
End Sub