1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
Imports System.IO
Public Class RichTextColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New RichTextCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a CalendarCell.
If (value IsNot Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(RichTextCell)) _
Then
Throw New InvalidCastException("Must be a RichTextCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class RichTextCell
'Inherits DataGridViewCell
'MODIF mauvais heritage
Inherits DataGridViewTextBoxCell
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As RichTextEditingControl =
CType(DataGridView.EditingControl, RichTextEditingControl)
'If (Me.Value Is Nothing) Then
' ctl.Rtf = CType(Me.DefaultNewRowValue, String)
'Else
' ctl.Rtf = CType(Me.Value, String)
'End If
'MODIF
' s'il n y a pas de contenu rtf on se branche en mode texte
If String.IsNullOrEmpty(ctl.Rtf) And Me.Value Is Nothing Then
ctl.Text = CType(Me.DefaultNewRowValue, String)
End If
' Sinon on laisse Base RichTextBox se charger en interne
' de recuperer le contenu rtf charge apres un appel RichTextBox.Loadfile...
End Sub
'------------AJOUT: !!!!!! => tres important car elle est charge----
'de cloner la cellule dans tous les rows(sinon on ne voit rien)
Public Overrides Function Clone() As Object
Return MyBase.Clone()
End Function
Public Overrides ReadOnly Property EditType As System.Type
Get
Return GetType(RichTextEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType As System.Type
Get
Return GetType(String)
End Get
' A SUPPRIMER
'Set(ByVal value As System.Type)
'End Set
End Property
Public Overrides ReadOnly Property DefaultNewRowValue As Object
Get
Return String.Empty
End Get
End Property
End Class
Public Class RichTextEditingControl
Inherits RichTextBox
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
' MENUS CONTEXTUEL POUR GERER RICHTEXTBOX
' avec des boites Dialog
Private ctx As ContextMenu = New ContextMenu
Public Sub New()
ctx.MenuItems.Add("Select Font")
ctx.MenuItems.Add("Select TextForeColor")
ctx.MenuItems.Add("Copy Selection")
ctx.MenuItems.Add("Paste Selection")
ctx.MenuItems.Add("Load RTF")
ctx.MenuItems.Add("Save RTF")
Me.ContextMenu = ctx
AddHandler ctx.MenuItems(0).Click, AddressOf SelFont
AddHandler ctx.MenuItems(1).Click, AddressOf SelColor
AddHandler ctx.MenuItems(2).Click, AddressOf Me.Copy
AddHandler ctx.MenuItems(3).Click, AddressOf Me.Paste
AddHandler ctx.MenuItems(4).Click, AddressOf Me.LoadRTF
AddHandler ctx.MenuItems(5).Click, AddressOf Me.SaveRTF
End Sub
'prop fichier charge rtf qui initialisera
'la prop base RichTextBox.rtf
Private m_FileRTFLoad As String = String.Empty
Public Property FileRTFLoad() As String
Get
Return m_FileRTFLoad
End Get
Set(ByVal value As String)
m_FileRTFLoad = value
If Not String.IsNullOrEmpty(m_FileRTFLoad) Then
Me.LoadFile(m_FileRTFLoad, RichTextBoxStreamType.RichText)
End If
End Set
End Property
'prop sauve rtf
Private m_FileRTFSave As String = String.Empty
Public Property FileRTFSave() As String
Get
Return m_FileRTFSave
End Get
Set(ByVal value As String)
m_FileRTFSave = value
If Not String.IsNullOrEmpty(m_FileRTFSave) Then
Me.SaveFile(m_FileRTFSave, RichTextBoxStreamType.RichText)
End If
End Set
End Property
Public Property EditingControlFormattedValue As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Rtf
End Get
Set(ByVal value As Object)
'Me.Rtf = value
'MODIF
If TypeOf value Is String Then
'modif
Me.Rtf = CStr(value)
End If
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Rtf
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
End Sub
Public Property EditingControlRowIndex As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
Select Case keyData And Keys.KeyCode
Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
Return True
Case Else
Return Not dataGridViewWantsInputKey
End Select
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
'MANQUANT : AJOUT
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingPanelCursor As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnTextChanged(e)
End Sub
'METHODES POUR SUB POUR CONTEXTMENU
Private Sub SelColor()
Dim MyDialog As New ColorDialog()
MyDialog.AllowFullOpen = False
MyDialog.ShowHelp = True
MyDialog.Color = Me.ForeColor
' Update if the user clicks OK
If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Me.SelectionColor = MyDialog.Color
End If
End Sub
Private Sub SelFont()
Dim MyFont As New FontDialog()
MyFont.FontMustExist = True
MyFont.ShowHelp = True
MyFont.Font = Me.Font
' Update if the user clicks OK
If (MyFont.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Me.SelectionFont = MyFont.Font
End If
End Sub
Private Sub LoadRTF()
Dim openDlg As New OpenFileDialog()
openDlg.Title = "RTF files"
openDlg.ShowHelp = True
openDlg.Multiselect = False
openDlg.Filter = "RTF Files(*.rtf)|*.rtf|Files Textes(*.txt)|*.txt"
If (openDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then
FileRTFLoad = openDlg.FileName
End If
End Sub
Private Sub SaveRTF()
Dim saveDlg As New SaveFileDialog()
saveDlg.Title = "RTF files"
saveDlg.ShowHelp = True
saveDlg.CheckFileExists = True
saveDlg.Filter = "RTF Files(*.rtf)|*.rtf|Files Textes(*.txt)|*.txt"
If (saveDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then
FileRTFSave = saveDlg.FileName
End If
End Sub
End Class |
Partager