Goal
To be able to paint rows in my DataGridViews in order to help a user better understand what is going on in the application. Take this image for an example:
- Light red = Deleted
- Yellow = Relationship to the Option Type Select (top left)
- Bright Red = Deleted & Related to the Option Type Selected (top left)
Problem
When I go up / down in my Option Types DataGridView, it will paint the Mandatory and Additional sections depending on which Option Type I choose. After doing this for a while (70 or so times), I get the generic error in the GDI.
As you can see in the screenshot above, one of the additional edit buttons are not displayed correctly (this is where the error occurs). It is always on the following line:
ico = Icon.FromHandle(bmpFind.GetHicon)
See complete code below:
Code
Private Sub dgvAdditionalOptions_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvAdditionalOptions.CellPainting
'Dim bmpFind As Bitmap
Dim drRow As HunterManagement.dtConveyorFunctionAdditionalOptionsRow
Dim myOptn As clsOptions
If dgvAdditionalOptions.Columns(e.ColumnIndex).Name = "ColBtnEditAdditional" AndAlso e.RowIndex >= 0 Then
drRow = dsHunterManagement.dtConveyorFunctionAdditionalOptions.FindByPK_ConveyorFunctionAdditionalOption(dgvAdditionalOptions.Rows(e.RowIndex).Cells("PK_ConveyorFunctionAdditionalOption").Value)
myOptn = New clsOptions(CInt(drRow.FK_Option))
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
If Not drRow Is Nothing AndAlso myOptn.InDrawing Then
Using bmpFind As Bitmap = My.Resources.Edit_16x16_2 'Use 16x16 PNG / BitMap images
Using ico As Icon = Icon.FromHandle(bmpFind.GetHicon)
e.Graphics.DrawIcon(ico, e.CellBounds.Left + 3, e.CellBounds.Top + 2.5)
e.Handled = True
End Using
End Using
Else
Using bmpFind As Bitmap = My.Resources.Edit_Disabled_16x16_2 'Use 16x16 PNG / BitMap images
Using ico As Icon = Icon.FromHandle(bmpFind.GetHicon)
e.Graphics.DrawIcon(ico, e.CellBounds.Left + 3, e.CellBounds.Top + 2.5)
e.Handled = True
End Using
End Using
End If
ElseIf dgvAdditionalOptions.Columns(e.ColumnIndex).Name = "ColBtnDeleteAdditional" AndAlso e.RowIndex >= 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
Using bmpFind As Bitmap = My.Resources.Delete_16x16 'Use 16x16 PNG / BitMap images
Using ico As Icon = Icon.FromHandle(bmpFind.GetHicon)
e.Graphics.DrawIcon(ico, e.CellBounds.Left + 2, e.CellBounds.Top + 2.5)
e.Handled = True
End Using
End Using
End If
End Sub
I tried searching this problem and it is all related to 'saving' an image... I am not saving an image though, I am only getting the icon of a BMP image. I must be forgetting to dispose something.
Any ideas?