L
Lisa
Still trying to get rich text from my dataset into a datagrid. So far, my
strategy has been to derive a custom column style which owns a rich text
box:
Public Class RTColumn
Inherits DataGridColumnStyle
Dim WithEvents rtb As RichTextBox = New RichTextBox()
Dim PHeight As Integer = 0
Dim PSize As Size
Dim MHeight As Integer = 0
Dim ioMemoryStream As MemoryStream
Dim rtbg As Graphics
Dim mfMetaFile As Metafile
Dim ptrHDC As IntPtr
I want the rich text box with events, because I'd like to know the size of
the contents when the text is assigned to rtb:
Private Sub rtb_ContentsResized(ByVal sender As Object, ByVal e As
System.Windows.Forms.ContentsResizedEventArgs) Handles rtb.ContentsResized
If e.NewRectangle.Height > MHeight Then
MHeight = e.NewRectangle.Height
End If
If e.NewRectangle.Height > PHeight Then
PHeight = e.NewRectangle.Height
End If
PSize = e.NewRectangle.Size
End Sub
To draw the contents of the rtb, I create a metafile in the Paint override:
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal [source] As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)
Try
Dim rt As String = Me.GetColumnValueAtRow([source], rowNum)
g.FillRectangle(backBrush, bounds) 'this has the effect
of erasing the cell
If rt <> "" Then
rtb.Rtf = rt
ioMemoryStream = New MemoryStream()
' Get a graphics context from the RichTextBox
Dim rtbg As Graphics = rtb.CreateGraphics
' Get the device context from the graphics context
ptrHDC = rtbg.GetHdc()
' Create a new Enhanced Metafile from the device context
mfMetaFile = New Metafile(ioMemoryStream, ptrHDC)
' Release the device context
rtbg.ReleaseHdc(ptrHDC)
' Draw the metafile
g.DrawImage(mfMetaFile, bounds.X, bounds.Y, PSize.Width,
PSize.Height)
End If
Catch
MsgBox(Err.Description)
End Try
End Sub 'Paint
Not only does this fail to draw the rich text in the datagrid cell, but I
get a 'Cast from type 'DBNull' to type 'String' is not valid' error after
scrolling the grid to the last row.
I know about the dotnet.leadit.be/extendeddatagrid. It looks intruiging,
but I'd really rather understand what I'm doing, and not have to include
another dll with my project. If anyone can point out the error of my ways,
I'd very much appreciate it.
strategy has been to derive a custom column style which owns a rich text
box:
Public Class RTColumn
Inherits DataGridColumnStyle
Dim WithEvents rtb As RichTextBox = New RichTextBox()
Dim PHeight As Integer = 0
Dim PSize As Size
Dim MHeight As Integer = 0
Dim ioMemoryStream As MemoryStream
Dim rtbg As Graphics
Dim mfMetaFile As Metafile
Dim ptrHDC As IntPtr
I want the rich text box with events, because I'd like to know the size of
the contents when the text is assigned to rtb:
Private Sub rtb_ContentsResized(ByVal sender As Object, ByVal e As
System.Windows.Forms.ContentsResizedEventArgs) Handles rtb.ContentsResized
If e.NewRectangle.Height > MHeight Then
MHeight = e.NewRectangle.Height
End If
If e.NewRectangle.Height > PHeight Then
PHeight = e.NewRectangle.Height
End If
PSize = e.NewRectangle.Size
End Sub
To draw the contents of the rtb, I create a metafile in the Paint override:
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal [source] As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)
Try
Dim rt As String = Me.GetColumnValueAtRow([source], rowNum)
g.FillRectangle(backBrush, bounds) 'this has the effect
of erasing the cell
If rt <> "" Then
rtb.Rtf = rt
ioMemoryStream = New MemoryStream()
' Get a graphics context from the RichTextBox
Dim rtbg As Graphics = rtb.CreateGraphics
' Get the device context from the graphics context
ptrHDC = rtbg.GetHdc()
' Create a new Enhanced Metafile from the device context
mfMetaFile = New Metafile(ioMemoryStream, ptrHDC)
' Release the device context
rtbg.ReleaseHdc(ptrHDC)
' Draw the metafile
g.DrawImage(mfMetaFile, bounds.X, bounds.Y, PSize.Width,
PSize.Height)
End If
Catch
MsgBox(Err.Description)
End Try
End Sub 'Paint
Not only does this fail to draw the rich text in the datagrid cell, but I
get a 'Cast from type 'DBNull' to type 'String' is not valid' error after
scrolling the grid to the last row.
I know about the dotnet.leadit.be/extendeddatagrid. It looks intruiging,
but I'd really rather understand what I'm doing, and not have to include
another dll with my project. If anyone can point out the error of my ways,
I'd very much appreciate it.