Sanmarie
asked on
Don't understand where to call MyBase.Paint
Using Windows Forms, .NET 1.1, VS 2003
I understand that you call the base class method in vb.net to do some initializing or setting things up normally before you call your derived method but I am missing something from my understanding
I inherited from a datagridtextboxcolumn and override the paint method to color cells depending on their value etc. The problem I am having is deciding where to call the base paint method as I don't think I really understand.
If I call the paint method in one place, then some cells get colored while others don't, or the formatting gets messed up in one place and not in others. Below is my paint method. I initially got the code online and modified it to do what I want. Basically, I have been calling MyBase.Paint() at different points in my code to get it working but I don't know why. Help.
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Dim e As DataGridFormatCellEventArg s
e = New DataGridFormatCellEventArg s(rowNum, Me._col, Me.DataGridTableStyle.Data Grid.Font, backBrush, foreBrush)
RaiseEvent SetCellFormat(Me, e)
'Note that UseBaseClassDrawing is the property of useBaseClassDrawing private data member
'It is set to false by default
If e.UseBaseClassDrawing Then
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
Else
g.FillRectangle(e.BackBrus h, bounds)
g.DrawString(Me.GetColumnV alueAtRow( source, rowNum).ToString, e.TextFont, e.ForeBrush, bounds.X, bounds.Y)
End If
If (e.TextFont Is Me.DataGridTableStyle.Data Grid.Font) = False Then
e.TextFont.Dispose()
End If
Try
'If the cell value is 0 or less than 0, color it red
Dim cellVal As String = Me.GetColumnValueAtRow(sou rce, rowNum)
If (Not cellVal Is Nothing) Then
Dim c As Integer
c = CType(cellVal, Integer)
If (c = 1 Or c < 1) Then
e.ForeBrush = New SolidBrush(Color.Red)
MyBase.Paint(g, bounds, source, rowNum, backBrush, e.ForeBrush, alignToRight)
Else
MyBase.Paint(g, bounds, source, rowNum, backBrush, e.ForeBrush, alignToRight)
End If
End If
'Check for the cell date value and if less than now, draw lines in the cell
cellDate = Me.DataGridTableStyle.Data Grid.Item( rowNum, 0)
If (Not hourMinString Is Nothing) Then
Dim cellToDisable As Boolean = CellDateLessToday(cellDate , DateTime.Now())
If (cellToDisable = True) And (_col = 1) Then
DrawLinesInCell(g, bounds)
Else
MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
End If
End If
'Change the background of the grids to blue to indicate the cell that was
'clicked in the mousedown event
If (Me.DataGridTableStyle.Dat aGrid.Curr entRowInde x = rowNum)_
And (Me.DataGridTableStyle.Dat aGrid.Curr entCell.Co lumnNumber = _col) And _
((rowNum <> 0) And (Me._col <> 0)) Then
If (cellToDisable = False Or _col <> 1) Then
e.BackBrush = New SolidBrush(Color.Blue)
e.ForeBrush = New SolidBrush(Color.White)
g.FillRectangle(e.BackBrus h, bounds)
'Make the font bigger in the clicked cell
e.TextFont = New Font("Arial", 24, FontStyle.Bold)
Dim intX As Integer = bounds.X + (bounds.Width / 3)
Dim intY As Integer = bounds.Y + (bounds.Height / 4)
g.DrawString(Me.GetColumnV alueAtRow( source, rowNum).ToString, e.TextFont, e.ForeBrush, intX, intY)
'MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
End If
End If
'MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
Catch ex As Exception
'Empty catch
Finally
'make sure the base class gets called to do the drawing with the possibly
'changed brushes
'MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
End Try
'MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
End Sub
I understand that you call the base class method in vb.net to do some initializing or setting things up normally before you call your derived method but I am missing something from my understanding
I inherited from a datagridtextboxcolumn and override the paint method to color cells depending on their value etc. The problem I am having is deciding where to call the base paint method as I don't think I really understand.
If I call the paint method in one place, then some cells get colored while others don't, or the formatting gets messed up in one place and not in others. Below is my paint method. I initially got the code online and modified it to do what I want. Basically, I have been calling MyBase.Paint() at different points in my code to get it working but I don't know why. Help.
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Dim e As DataGridFormatCellEventArg
e = New DataGridFormatCellEventArg
RaiseEvent SetCellFormat(Me, e)
'Note that UseBaseClassDrawing is the property of useBaseClassDrawing private data member
'It is set to false by default
If e.UseBaseClassDrawing Then
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
Else
g.FillRectangle(e.BackBrus
g.DrawString(Me.GetColumnV
End If
If (e.TextFont Is Me.DataGridTableStyle.Data
e.TextFont.Dispose()
End If
Try
'If the cell value is 0 or less than 0, color it red
Dim cellVal As String = Me.GetColumnValueAtRow(sou
If (Not cellVal Is Nothing) Then
Dim c As Integer
c = CType(cellVal, Integer)
If (c = 1 Or c < 1) Then
e.ForeBrush = New SolidBrush(Color.Red)
MyBase.Paint(g, bounds, source, rowNum, backBrush, e.ForeBrush, alignToRight)
Else
MyBase.Paint(g, bounds, source, rowNum, backBrush, e.ForeBrush, alignToRight)
End If
End If
'Check for the cell date value and if less than now, draw lines in the cell
cellDate = Me.DataGridTableStyle.Data
If (Not hourMinString Is Nothing) Then
Dim cellToDisable As Boolean = CellDateLessToday(cellDate
If (cellToDisable = True) And (_col = 1) Then
DrawLinesInCell(g, bounds)
Else
MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
End If
End If
'Change the background of the grids to blue to indicate the cell that was
'clicked in the mousedown event
If (Me.DataGridTableStyle.Dat
And (Me.DataGridTableStyle.Dat
((rowNum <> 0) And (Me._col <> 0)) Then
If (cellToDisable = False Or _col <> 1) Then
e.BackBrush = New SolidBrush(Color.Blue)
e.ForeBrush = New SolidBrush(Color.White)
g.FillRectangle(e.BackBrus
'Make the font bigger in the clicked cell
e.TextFont = New Font("Arial", 24, FontStyle.Bold)
Dim intX As Integer = bounds.X + (bounds.Width / 3)
Dim intY As Integer = bounds.Y + (bounds.Height / 4)
g.DrawString(Me.GetColumnV
'MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
End If
End If
'MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
Catch ex As Exception
'Empty catch
Finally
'make sure the base class gets called to do the drawing with the possibly
'changed brushes
'MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
End Try
'MyBase.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight)
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
cmjwebservices I tried your approach and I still had some problems.
Bob, I tried your way and everything worked fine but now my font is messed up. I probably deleted something
Thanks for your help.