TableLayoutPanel CellPaint事件反复引发 [英] TableLayoutPanel CellPaint Event Repeatedly raises
问题描述
大家好
我有一个奇怪的调试问题。 我创建了一个TableLayoutPanel控件,在其中我创建了列并使用了FillRectangle 使用用户输入的数据更新TableLayoutPanel中的单元格的函数。
I have a curious debugging problem. I have creates a TableLayoutPanel Control in which I create columns and use the FillRectangle function to update a cell in the TableLayoutPanel with data the user enters.
在进行degugging时,我注意到paint函数反复出现。即7或8次,而不是只有一次。 这对于Cellpaint功能是否正常。 我附上了代码示例。
While degugging, I noticed that the paint function is raised repeatedly. i.e. 7 or 8 times, instead of only once. Is this normal for the Cellpaint function. I have attached a sample of the code.
我正常不使用绘画功能所以我不确定这是否常见。 效率似乎很低。
I normal don't use the paint function so I am not sure if this is common. It seems inefficient.
Public Sub TableUserLayout_Paint(sender As Object, e As TableLayoutCellPaintEventArgs) Handles TableLayoutPanel1.CellPaint
TableLayoutPanel1_CellPaint(sender, e)
End Sub
Private Sub TableLayoutPanel1_CellPaint(ByVal sender As Object, ByVal e As TableLayoutCellPaintEventArgs) _
Handles TableLayoutPanel1.CellPaint
Dim g As Graphics = e.Graphics
Dim numberofRects As Integer = TableLayoutPanel1.RowCount
Dim tempdebugtest As Integer = (EditCoaxLine.SectionNo_NumericUpDown.Value - 1)
MyTemprectangles.Clear() 'Array of calculated data for the user rectangles
Dim i As Integer = 0
For Each ctrl As Control In TableLayoutPanel1.Controls
If TypeOf ctrl Is LayoutUserControl Then
Dim cellPos As TableLayoutPanelCellPosition = TableLayoutPanel1.GetCellPosition(ctrl) 'Debug check of row/column location
ctrl.Visible = False
For Each K As Myrectangle In Myrectangles 'Data for each column which is different depending on user input
If (i + 1) = K.TableRowPosition Then 'Check if the right column
MyTemprectangles.Add(New Myrectangle(i, K.myPen, K.RectangleToDraw)) 'Add data to a temp array
End If
Next
For Each L As Myrectangle In MyTemprectangles
'Draw recyangles to screen.
g.FillRectangle(L.myPen, L.RectangleToDraw) 'Draw rectangles in cell
Next
MyTemprectangles.Clear() 'clears temp array
i += 1
Else
ctrl.Visible = True
ctrl.Text = i 'Label for each column
End If
Next
End Sub
推荐答案
我相信您会发现TableLayoutPanel中的每个单元格都会调用一次CellPaint事件。
I believe you will find that the CellPaint event is called once for each cell in the TableLayoutPanel.
如果您检查 TableLayoutCellPaintEventArgs Class 将
传递给CellPaint事件的(e)参数,你会发现行和列p每次调用时,性能都会发生变化。 CellBounds属性也是如此。
If you check the properties in the TableLayoutCellPaintEventArgs Class that
is passed to the CellPaint event's (e) parameter you will find that the Row and Column properties change each time it is called. So does the CellBounds property too.
您应该可以使用行和列 TableLayoutCellPaintEventArgs中的属性,用于检测当前正在绘制的单元格。 然后你可以根据Row
和Column调整你需要的任何东西。 你也会发现CellBounds属性也很方便。
You should be able to just use the Row and Column properties from the TableLayoutCellPaintEventArgs to detect which cell is currently being painted. Then you can adjust whatever you need according to the Row
and Column. You would also find the CellBounds property to be handy too.
例如, 我有一个带有4个单元格的TableLayoutPanel,并使用"行"和"列"属性来确定要填充它们的颜色。 请注意,我没有使用循环遍历所有单元格。
For example, I have a TableLayoutPanel with 4 cells and use the Row and Column properties to determine what color to fill them with. Notice I am not using a loop to iterate through all the cells.
Public Class Form1
Private Sub TableLayoutPanel1_CellPaint(sender As Object, e As TableLayoutCellPaintEventArgs) Handles TableLayoutPanel1.CellPaint
Dim brsh As Brush = Nothing
Select Case e.Column
Case 0
If e.Row = 0 Then
brsh = Brushes.Red 'if it is Column 0, Row 0
Else
brsh = Brushes.Green 'if it is Column 0, Row 1
End If
Case 1
If e.Row = 0 Then
brsh = Brushes.Blue 'if it is Column 1, Row 0
Else
brsh = Brushes.Yellow 'if it is Column 1, Row 1
End If
End Select
e.Graphics.FillRectangle(brsh, e.CellBounds) 'fills the (CellBounds) with the calculated brush color
End Sub
End Class
这篇关于TableLayoutPanel CellPaint事件反复引发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!