Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1480

How I enable Cut, Copy and Paste buttons and menu items

$
0
0
I like the way I did this. Basically it's set up on a Timer set to 250 milliseconds. So every quarter second it's getting checked. You can see the slight delay but I don't want to make the Timer too frequent for fear of making the program sluggish doing that kind of thing.

Code:

Private Sub Timer1()

If Not Visible Then Exit Sub

With tbrCustomers

  .Buttons("Cut").Enabled = CanCut(ActiveControl)
  .Buttons("Copy").Enabled = CanCopy(ActiveControl)
  .Buttons("Paste").Enabled = CanPaste(ActiveControl)

End With

End Sub

Public Function CanCopy(ByRef Control As Control) As Boolean
Dim f As Boolean

' Determines if text can be Copied from a Control.
' ComboBoxes must have their Style set to 0 (DropDownCombo)

On Error GoTo errHandler

CanCopy = False

If Control Is Nothing Then Exit Function

f = TypeName(Control) = "ComboBox"

If f Then

  If Control.Style <> 0 Then f = False

End If

If Control.Enabled Then

  If Not IsTextControl(Control) And Not f Then Exit Function
  If Not TextSelected(Control) Then Exit Function
 
  CanCopy = True

End If

Exit Function


errHandler:
Dim nErrorHandlerResult As Long

nErrorHandlerResult = ErrorHandler(Error, Err, "Control.Name = " & Control.NAME, NAME & ".CanCopy(Public Function)")

End Function
Public Function CanCut(ByRef Control As Control) As Boolean
Dim f As Boolean

' Determines if text can be Cut from a Control.
' ComboBoxes must have their Style set to 0 (DropDownCombo)

On Error GoTo errHandler

CanCut = False

If Control Is Nothing Then Exit Function

f = TypeName(Control) = "ComboBox"

If f Then

  If Control.Style <> 0 Then f = False

End If

If Control.Enabled Then

  If Not IsTextControl(Control) And Not f Then Exit Function

  If Not TextSelected(Control) Then Exit Function

  If TypeName(Control) = "TextBox" Or TypeName(Control) = "RichTextBox" Then

    If Control.Locked Then Exit Function

  End If

  CanCut = True

End If

Exit Function


errHandler:
Dim nErrorHandlerResult As Long

nErrorHandlerResult = ErrorHandler(Error, Err, "Control.Name = " & Control.NAME, NAME & ".CanCut(Public Function)")

End Function
Public Function CanPaste(ByRef Control As Control) As Boolean
Dim f As Boolean

' Determines if text can be Pasted into a Control.
' ComboBoxes must have their Style set to 0 (DropDownCombo)

On Error GoTo errHandler

CanPaste = False

If Control Is Nothing Then Exit Function

If Control.Enabled = False Then Exit Function

If Len(Clipboard.GetText) = 0 Then Exit Function

If TypeName(Control) = "ComboBox" Then

  f = (Control.Style = 0)

End If

If Not IsTextControl(Control) And Not f Then Exit Function

If TypeName(Control) = "TextBox" Or TypeName(Control) = "RichTextBox" Then

  If Control.Locked Then Exit Function

End If

CanPaste = True

Exit Function


errHandler:
Dim nErrorHandlerResult As Long

If Err = 521 Then Exit Function ' "Another application is using the Clipboard and won't release it to your application."

nErrorHandlerResult = ErrorHandler(Error, Err, "Control.Name = " & Control.NAME, NAME & ".CanPaste(Public Function)")

End Function

Public Function IsTextControl(ByRef Control As Control) As Boolean

On Error Resume Next

IsTextControl = False

If TypeName(Control) = "TextBox" Or TypeName(Control) = "RichTextBox" Then

  IsTextControl = True

End If

End Function

Public Function TextSelected(ByRef Control As Control) As Boolean

' Returns whether Text is Selected or not.

On Error GoTo errHandler

TextSelected = Len(Control.SelText)

Exit Function


errHandler:

TextSelected = False

End Function


Viewing all articles
Browse latest Browse all 1480

Trending Articles