|
| 1 | +Imports System.ComponentModel |
| 2 | + |
| 3 | +Namespace Hb.Windows.Forms |
| 4 | + ''' <summary> |
| 5 | + ''' Defines a build-in ContextMenuStrip manager for HexBox control to show Copy, Cut, Paste menu in contextmenu of the control. |
| 6 | + ''' </summary> |
| 7 | + <TypeConverterAttribute(GetType(ExpandableObjectConverter))> |
| 8 | + Public NotInheritable Class BuiltInContextMenu |
| 9 | + Inherits Component |
| 10 | + ''' <summary> |
| 11 | + ''' Contains the HexBox control. |
| 12 | + ''' </summary> |
| 13 | + Private ReadOnly _hexBox As Forms.HexBox |
| 14 | + |
| 15 | + ''' <summary> |
| 16 | + ''' Contains the ContextMenuStrip control. |
| 17 | + ''' </summary> |
| 18 | + Private _contextMenuStrip As ContextMenuStrip |
| 19 | + |
| 20 | + Private _copyMenuItemImage As Image = Nothing |
| 21 | + Private _copyMenuItemText As String |
| 22 | + |
| 23 | + ''' <summary> |
| 24 | + ''' Contains the "Copy"-ToolStripMenuItem object. |
| 25 | + ''' </summary> |
| 26 | + Private _copyToolStripMenuItem As ToolStripMenuItem |
| 27 | + |
| 28 | + Private _cutMenuItemImage As Image = Nothing |
| 29 | + Private _cutMenuItemText As String |
| 30 | + |
| 31 | + ''' <summary> |
| 32 | + ''' Contains the "Cut"-ToolStripMenuItem object. |
| 33 | + ''' </summary> |
| 34 | + Private _cutToolStripMenuItem As ToolStripMenuItem |
| 35 | + |
| 36 | + Private _pasteMenuItemImage As Image = Nothing |
| 37 | + Private _pasteMenuItemText As String |
| 38 | + |
| 39 | + ''' <summary> |
| 40 | + ''' Contains the "Paste"-ToolStripMenuItem object. |
| 41 | + ''' </summary> |
| 42 | + Private _pasteToolStripMenuItem As ToolStripMenuItem |
| 43 | + |
| 44 | + Private _selectAllMenuItemImage As Image = Nothing |
| 45 | + Private _selectAllMenuItemText As String = Nothing |
| 46 | + |
| 47 | + ''' <summary> |
| 48 | + ''' Contains the "Select All"-ToolStripMenuItem object. |
| 49 | + ''' </summary> |
| 50 | + Private _selectAllToolStripMenuItem As ToolStripMenuItem |
| 51 | + |
| 52 | + ''' <summary> |
| 53 | + ''' Initializes a new instance of BuildInContextMenu class. |
| 54 | + ''' </summary> |
| 55 | + ''' <paramname="hexBox">the HexBox control</param> |
| 56 | + Friend Sub New(hexBox As Forms.HexBox) |
| 57 | + _hexBox = hexBox |
| 58 | + AddHandler _hexBox.ByteProviderChanged, New EventHandler(AddressOf HexBox_ByteProviderChanged) |
| 59 | + End Sub |
| 60 | + |
| 61 | + ''' <summary> |
| 62 | + ''' Gets or sets the image of the "Copy" ContextMenuStrip item. |
| 63 | + ''' </summary> |
| 64 | + <Category("BuiltIn-ContextMenu")> |
| 65 | + Public Property CopyMenuItemImage As Image |
| 66 | + Get |
| 67 | + Return _copyMenuItemImage |
| 68 | + End Get |
| 69 | + Set(value As Image) |
| 70 | + _copyMenuItemImage = value |
| 71 | + End Set |
| 72 | + End Property |
| 73 | + |
| 74 | + ''' <summary> |
| 75 | + ''' Gets or sets the custom text of the "Copy" ContextMenuStrip item. |
| 76 | + ''' </summary> |
| 77 | + <Category("BuiltIn-ContextMenu"), Localizable(True)> |
| 78 | + Public Property CopyMenuItemText As String |
| 79 | + Get |
| 80 | + Return _copyMenuItemText |
| 81 | + End Get |
| 82 | + Set(value As String) |
| 83 | + _copyMenuItemText = value |
| 84 | + End Set |
| 85 | + End Property |
| 86 | + |
| 87 | + ''' <summary> |
| 88 | + ''' Gets or sets the image of the "Cut" ContextMenuStrip item. |
| 89 | + ''' </summary> |
| 90 | + <Category("BuiltIn-ContextMenu")> |
| 91 | + Public Property CutMenuItemImage As Image |
| 92 | + Get |
| 93 | + Return _cutMenuItemImage |
| 94 | + End Get |
| 95 | + Set(value As Image) |
| 96 | + _cutMenuItemImage = value |
| 97 | + End Set |
| 98 | + End Property |
| 99 | + |
| 100 | + ''' <summary> |
| 101 | + ''' Gets or sets the custom text of the "Cut" ContextMenuStrip item. |
| 102 | + ''' </summary> |
| 103 | + <Category("BuiltIn-ContextMenu"), Localizable(True)> |
| 104 | + Public Property CutMenuItemText As String |
| 105 | + Get |
| 106 | + Return _cutMenuItemText |
| 107 | + End Get |
| 108 | + Set(value As String) |
| 109 | + _cutMenuItemText = value |
| 110 | + End Set |
| 111 | + End Property |
| 112 | + |
| 113 | + ''' <summary> |
| 114 | + ''' Gets or sets the image of the "Paste" ContextMenuStrip item. |
| 115 | + ''' </summary> |
| 116 | + <Category("BuiltIn-ContextMenu")> |
| 117 | + Public Property PasteMenuItemImage As Image |
| 118 | + Get |
| 119 | + Return _pasteMenuItemImage |
| 120 | + End Get |
| 121 | + Set(value As Image) |
| 122 | + _pasteMenuItemImage = value |
| 123 | + End Set |
| 124 | + End Property |
| 125 | + |
| 126 | + ''' <summary> |
| 127 | + ''' Gets or sets the custom text of the "Paste" ContextMenuStrip item. |
| 128 | + ''' </summary> |
| 129 | + <Category("BuiltIn-ContextMenu"), Localizable(True)> |
| 130 | + Public Property PasteMenuItemText As String |
| 131 | + Get |
| 132 | + Return _pasteMenuItemText |
| 133 | + End Get |
| 134 | + Set(value As String) |
| 135 | + _pasteMenuItemText = value |
| 136 | + End Set |
| 137 | + End Property |
| 138 | + |
| 139 | + ''' <summary> |
| 140 | + ''' Gets or sets the image of the "Select All" ContextMenuStrip item. |
| 141 | + ''' </summary> |
| 142 | + <Category("BuiltIn-ContextMenu")> |
| 143 | + Public Property SelectAllMenuItemImage As Image |
| 144 | + Get |
| 145 | + Return _selectAllMenuItemImage |
| 146 | + End Get |
| 147 | + Set(value As Image) |
| 148 | + _selectAllMenuItemImage = value |
| 149 | + End Set |
| 150 | + End Property |
| 151 | + |
| 152 | + ''' <summary> |
| 153 | + ''' Gets or sets the custom text of the "Select All" ContextMenuStrip item. |
| 154 | + ''' </summary> |
| 155 | + <Category("BuiltIn-ContextMenu"), Localizable(True)> |
| 156 | + Public Property SelectAllMenuItemText As String |
| 157 | + Get |
| 158 | + Return _selectAllMenuItemText |
| 159 | + End Get |
| 160 | + Set(value As String) |
| 161 | + _selectAllMenuItemText = value |
| 162 | + End Set |
| 163 | + End Property |
| 164 | + |
| 165 | + ''' <summary> |
| 166 | + ''' Gets the text of the "Copy" ContextMenuStrip item. |
| 167 | + ''' </summary> |
| 168 | + Friend ReadOnly Property CopyMenuItemTextInternal As String |
| 169 | + Get |
| 170 | + Return If(Not String.IsNullOrEmpty(CopyMenuItemText), CopyMenuItemText, "Copy") |
| 171 | + End Get |
| 172 | + End Property |
| 173 | + |
| 174 | + ''' <summary> |
| 175 | + ''' Gets the text of the "Cut" ContextMenuStrip item. |
| 176 | + ''' </summary> |
| 177 | + Friend ReadOnly Property CutMenuItemTextInternal As String |
| 178 | + Get |
| 179 | + Return If(Not String.IsNullOrEmpty(CutMenuItemText), CutMenuItemText, "Cut") |
| 180 | + End Get |
| 181 | + End Property |
| 182 | + |
| 183 | + ''' <summary> |
| 184 | + ''' Gets the text of the "Paste" ContextMenuStrip item. |
| 185 | + ''' </summary> |
| 186 | + Friend ReadOnly Property PasteMenuItemTextInternal As String |
| 187 | + Get |
| 188 | + Return If(Not String.IsNullOrEmpty(PasteMenuItemText), PasteMenuItemText, "Paste") |
| 189 | + End Get |
| 190 | + End Property |
| 191 | + |
| 192 | + ''' <summary> |
| 193 | + ''' Gets the text of the "Select All" ContextMenuStrip item. |
| 194 | + ''' </summary> |
| 195 | + Friend ReadOnly Property SelectAllMenuItemTextInternal As String |
| 196 | + Get |
| 197 | + Return If(Not String.IsNullOrEmpty(SelectAllMenuItemText), SelectAllMenuItemText, "SelectAll") |
| 198 | + End Get |
| 199 | + End Property |
| 200 | + |
| 201 | + ''' <summary> |
| 202 | + ''' Before opening the ContextMenuStrip, we manage the availability of the items. |
| 203 | + ''' </summary> |
| 204 | + ''' <paramname="sender">the sender object</param> |
| 205 | + ''' <paramname="e">the event data</param> |
| 206 | + Private Sub BuildInContextMenuStrip_Opening(sender As Object, e As CancelEventArgs) |
| 207 | + _cutToolStripMenuItem.Enabled = _hexBox.CanCut() |
| 208 | + _copyToolStripMenuItem.Enabled = _hexBox.CanCopy() |
| 209 | + _pasteToolStripMenuItem.Enabled = _hexBox.CanPaste() |
| 210 | + _selectAllToolStripMenuItem.Enabled = _hexBox.CanSelectAll() |
| 211 | + End Sub |
| 212 | + |
| 213 | + ''' <summary> |
| 214 | + ''' Assigns the ContextMenuStrip control to the HexBox control. |
| 215 | + ''' </summary> |
| 216 | + Private Sub CheckBuiltInContextMenu() |
| 217 | + If Forms.Util.DesignMode Then Return |
| 218 | + |
| 219 | + If _contextMenuStrip Is Nothing Then |
| 220 | + Dim cms As New ContextMenuStrip() |
| 221 | + _cutToolStripMenuItem = New ToolStripMenuItem(CutMenuItemTextInternal, CutMenuItemImage, New EventHandler(AddressOf CutMenuItem_Click)) |
| 222 | + cms.Items.Add(_cutToolStripMenuItem) |
| 223 | + _copyToolStripMenuItem = New ToolStripMenuItem(CopyMenuItemTextInternal, CopyMenuItemImage, New EventHandler(AddressOf CopyMenuItem_Click)) |
| 224 | + cms.Items.Add(_copyToolStripMenuItem) |
| 225 | + _pasteToolStripMenuItem = New ToolStripMenuItem(PasteMenuItemTextInternal, PasteMenuItemImage, New EventHandler(AddressOf PasteMenuItem_Click)) |
| 226 | + cms.Items.Add(_pasteToolStripMenuItem) |
| 227 | + |
| 228 | + cms.Items.Add(New ToolStripSeparator()) |
| 229 | + |
| 230 | + _selectAllToolStripMenuItem = New ToolStripMenuItem(SelectAllMenuItemTextInternal, SelectAllMenuItemImage, New EventHandler(AddressOf SelectAllMenuItem_Click)) |
| 231 | + cms.Items.Add(_selectAllToolStripMenuItem) |
| 232 | + AddHandler cms.Opening, New CancelEventHandler(AddressOf BuildInContextMenuStrip_Opening) |
| 233 | + |
| 234 | + _contextMenuStrip = cms |
| 235 | + End If |
| 236 | + |
| 237 | + If _hexBox.ByteProvider Is Nothing AndAlso _hexBox.ContextMenuStrip Is _contextMenuStrip Then |
| 238 | + _hexBox.ContextMenuStrip = Nothing |
| 239 | + ElseIf _hexBox.ByteProvider IsNot Nothing AndAlso _hexBox.ContextMenuStrip Is Nothing Then |
| 240 | + _hexBox.ContextMenuStrip = _contextMenuStrip |
| 241 | + End If |
| 242 | + End Sub |
| 243 | + |
| 244 | + ''' <summary> |
| 245 | + ''' The handler for the "Copy"-Click event |
| 246 | + ''' </summary> |
| 247 | + ''' <paramname="sender">the sender object</param> |
| 248 | + ''' <paramname="e">the event data</param> |
| 249 | + Private Sub CopyMenuItem_Click(sender As Object, e As EventArgs) |
| 250 | + _hexBox.Copy() |
| 251 | + End Sub |
| 252 | + |
| 253 | + ''' <summary> |
| 254 | + ''' The handler for the "Cut"-Click event |
| 255 | + ''' </summary> |
| 256 | + ''' <paramname="sender">the sender object</param> |
| 257 | + ''' <paramname="e">the event data</param> |
| 258 | + Private Sub CutMenuItem_Click(sender As Object, e As EventArgs) |
| 259 | + _hexBox.Cut() |
| 260 | + End Sub |
| 261 | + |
| 262 | + ''' <summary> |
| 263 | + ''' If ByteProvider |
| 264 | + ''' </summary> |
| 265 | + ''' <paramname="sender">the sender object</param> |
| 266 | + ''' <paramname="e">the event data</param> |
| 267 | + Private Sub HexBox_ByteProviderChanged(sender As Object, e As EventArgs) |
| 268 | + CheckBuiltInContextMenu() |
| 269 | + End Sub |
| 270 | + Private Sub InitializeComponent() |
| 271 | + |
| 272 | + End Sub |
| 273 | + |
| 274 | + ''' <summary> |
| 275 | + ''' The handler for the "Paste"-Click event |
| 276 | + ''' </summary> |
| 277 | + ''' <paramname="sender">the sender object</param> |
| 278 | + ''' <paramname="e">the event data</param> |
| 279 | + Private Sub PasteMenuItem_Click(sender As Object, e As EventArgs) |
| 280 | + _hexBox.Paste() |
| 281 | + End Sub |
| 282 | + |
| 283 | + ''' <summary> |
| 284 | + ''' The handler for the "Select All"-Click event |
| 285 | + ''' </summary> |
| 286 | + ''' <paramname="sender">the sender object</param> |
| 287 | + ''' <paramname="e">the event data</param> |
| 288 | + Private Sub SelectAllMenuItem_Click(sender As Object, e As EventArgs) |
| 289 | + _hexBox.SelectAll() |
| 290 | + End Sub |
| 291 | + End Class |
| 292 | +End Namespace |
0 commit comments