Stephany/Patrick...
Here's the code that I have for cut, copy, paste. delete, selectall, undo
and the strip. As mentioned in my prior post, I'm trying to control how a
user can paste text back into a textbox by setting my textbox tags
(suggested by Patrick) to be phonenumber, zipcode, alpha, alphanumeric, or
numeric and then use the tag to set the format when cutting or pasting back
to the original textbox or a different textbox. I think I fixed the problem
with how the items in the popup menu are enabled/disabled by moving all code
into the opening event instead of in each of the click events.
Not sure if I'm doing this right or if the code can be tighter. Having some
problems with Delete/Undo. tb.CanUndo doesn't seem to work when working
with the Delete event.
Anyway, if you have any time to review/test the code and validate/improve
it, I would appreciate a lot. Its the last thing I need to do before
completing the app.
Thanks
Steve
Private Sub tsmiCut_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tsmiCut.Click
If (TypeOf Me.ActiveControl Is TextBox) Then
Dim tb As TextBox = Me.ActiveControl
'Is any text selected to cut?
If tb.SelectionLength > 0 Then
' Cut the selected text to the clipboard
Clipboard.SetData(tb.Tag, tb.Text)
' Initialize the textbox
tb.Text = ""
' Set the action to 'Cut' so that the items are properly
enabled/disabled
' during the next time the Context Menu is displayed.
CMS_Action = "Cut"
End If
End If
End Sub
Private Sub tsmiCopy_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tsmiCopy.Click
If (TypeOf Me.ActiveControl Is TextBox) Then
Dim tb As TextBox = Me.ActiveControl
'Is any text selected to copy?
If tb.SelectionLength > 0 Then
'Copy the selected text to the clipboard
Clipboard.SetData(tb.Tag, tb.Text)
' Set the action to 'Cut' so that the items are properly
enabled/disabled
' during the next time the Context Menu is displayed.
CMS_Action = "Copy"
End If
End If
End Sub
Private Sub tsmiPaste_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tsmiPaste.Click
If (TypeOf Me.ActiveControl Is TextBox) Then
Dim tb As TextBox = Me.ActiveControl
If (My.Computer.Clipboard.ContainsData(CStr(tb.Tag)) = True)
Then
CType(tb, TextBox).Text =
CStr(My.Computer.Clipboard.GetData(CStr(tb.Tag)))
CMS_Action = "Paste"
Else
MsgBox("Invalid Format", MsgBoxStyle.Information, "Paste")
End If
End If
End Sub
Private Sub tsmiUndo_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tsmiUndo.Click
If (TypeOf Me.ActiveControl Is TextBox) Then
Dim tb As TextBox = Me.ActiveControl
If (My.Computer.Clipboard.ContainsData(CStr(tb.Tag)) = True)
Then
' Can we Undo the last action?
If tb.CanUndo = True Then
' Undo the last thing the user did.
tb.Undo()
' Clear the undo buffer to make sure that clicking Undo
again doesn't redo the last thing undone.
tb.ClearUndo()
End If
CMS_Action = "Undo"
End If
End If
End Sub
Private Sub tsmiDelete_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tsmiDelete.Click
If (TypeOf Me.ActiveControl Is TextBox) Then
Dim tb As TextBox = Me.ActiveControl
'Is any text selected to delete?
If tb.SelectionLength > 0 Then
' Delete the selected text to the clipboard
Clipboard.SetData(tb.Tag, tb.Text)
' Initialize the textbox
tb.Text = ""
' Set the action to 'Delete' so that the items are properly
enabled/disabled
' during the next time the Context Menu is displayed.
CMS_Action = "Delete"
End If
End If
End Sub
Private Sub tsmiSelectAll_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tsmiSelectAll.Click
If (TypeOf Me.ActiveControl Is TextBox) Then
Dim tb As TextBox = Me.ActiveControl
With tb
.SelectionStart = 0
.SelectionLength = tb.Text.Length
End With
CMS_Action = "SelectAll"
End If
End Sub
Private Sub ContextMenuStrip1_Opening(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
If Me.ActiveControl Is Nothing Then
Exit Sub
End If
Select Case CMS_Action
Case "Undo"
tsmiUndo.Enabled = False
tsmiCut.Enabled = False
tsmiCopy.Enabled = False
tsmiPaste.Enabled = False
tsmiDelete.Enabled = True
tsmiSelectAll.Enabled = True
Exit Sub
Case "Cut"
tsmiUndo.Enabled = True
tsmiCut.Enabled = False
tsmiCopy.Enabled = False
tsmiPaste.Enabled = True
tsmiDelete.Enabled = False
tsmiSelectAll.Enabled = False
Exit Sub
Case "Copy"
tsmiUndo.Enabled = False
tsmiCut.Enabled = False
tsmiCopy.Enabled = False
tsmiPaste.Enabled = True
tsmiDelete.Enabled = False
tsmiSelectAll.Enabled = False
Exit Sub
Case "Paste"
tsmiUndo.Enabled = True
tsmiCut.Enabled = False
tsmiCopy.Enabled = False
tsmiPaste.Enabled = True
tsmiDelete.Enabled = False
tsmiSelectAll.Enabled = True
Exit Sub
Case "Delete"
tsmiUndo.Enabled = True
tsmiCut.Enabled = False
tsmiCopy.Enabled = False
tsmiPaste.Enabled = True
tsmiDelete.Enabled = False
tsmiSelectAll.Enabled = False
Exit Sub
Case "SelectAll"
tsmiUndo.Enabled = False
tsmiCut.Enabled = True
tsmiCopy.Enabled = True
If
(My.Computer.Clipboard.ContainsData(CStr(Me.ActiveControl.Tag)) = True) And
(TypeOf Me.ActiveControl Is TextBox) Then
tsmiPaste.Enabled = True
Else
tsmiPaste.Enabled = False
End If
tsmiDelete.Enabled = True
tsmiSelectAll.Enabled = False
Exit Sub
End Select
Dim tb As TextBox = Me.ActiveControl
If tb.Text = "" Then
tsmiUndo.Enabled = False
tsmiCut.Enabled = False
tsmiCopy.Enabled = False
If
(My.Computer.Clipboard.ContainsData(CStr(Me.ActiveControl.Tag)) = True) And
(TypeOf Me.ActiveControl Is TextBox) Then
tsmiPaste.Enabled = True
Else
tsmiPaste.Enabled = False
End If
tsmiDelete.Enabled = False
tsmiSelectAll.Enabled = False
End If
If tb.SelectionLength > 0 Then
tsmiCut.Enabled = True
tsmiCopy.Enabled = True
If
(My.Computer.Clipboard.ContainsData(CStr(Me.ActiveControl.Tag)) = True) And
(TypeOf Me.ActiveControl Is TextBox) Then
tsmiPaste.Enabled = True
Else
tsmiPaste.Enabled = False
End If
tsmiDelete.Enabled = True
If tb.SelectionLength = Len(tb.Text) Then
tsmiSelectAll.Enabled = False
Else
tsmiSelectAll.Enabled = True
End If
Else
tsmiCut.Enabled = False
tsmiCopy.Enabled = False
If
(My.Computer.Clipboard.ContainsData(CStr(Me.ActiveControl.Tag)) = True) And
(TypeOf Me.ActiveControl Is TextBox) Then
tsmiPaste.Enabled = True
Else
tsmiPaste.Enabled = False
End If
tsmiDelete.Enabled = False
tsmiSelectAll.Enabled = True
End If
CMS_Action = ""
End Sub