can you please aid me writing a code for copy,cut,paste,undo and redo in vb6

Dani AI

Generated

For : below is a compact, practical starter you can drop into a VB6 form. As said, show what you have — this is a minimal working approach — and as suggested, it uses the Forms clipboard object for copy/cut/paste. It includes a quick Windows undo call and a simple undo/redo stack you can adapt. Add a reference to "Microsoft Forms 2.0 Object Library" or use CreateObject("Forms.DataObject") if you prefer late binding.

Copy / Cut / Paste (uses DataObject)

' Requires "Microsoft Forms 2.0 Object Library" or use CreateObject("Forms.DataObject")
Private Sub CopySelection(tb As TextBox)
    Dim cb As Object
    Set cb = CreateObject("Forms.DataObject")
    If tb.SelLength = 0 Then Exit Sub
    cb.SetText tb.SelText
    cb.PutInClipboard
End Sub

Private Sub CutSelection(tb As TextBox)
    CopySelection tb
    tb.SelText = ""
End Sub

Private Sub PasteClipboard(tb As TextBox)
    Dim cb As Object
    Set cb = CreateObject("Forms.DataObject")
    On Error Resume Next
    cb.GetFromClipboard
    tb.SelText = cb.GetText
End Sub

Quick undo (Windows edit control)

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const EM_UNDO = &HC7

Private Sub UndoOnce(tb As TextBox)
    SendMessage tb.hWnd, EM_UNDO, 0&, 0&
End Sub

Simple undo/redo stack (reliable across programmatic changes)

  • Save previous text+selection in Text_Change; push to Undo stack.
  • Pop to Redo when undoing; clear Redo on new user edit.
  • Limit stack size for memory.
' Declarations
Private UndoStack As Collection, RedoStack As Collection
Private prevText As String, prevStart As Long, prevLen As Long

Private Sub Form_Load()
    Set UndoStack = New Collection: Set RedoStack = New Collection
    prevText = Text1.Text: prevStart = Text1.SelStart: prevLen = Text1.SelLength
End Sub

Private Sub Text1_Change()
    If Text1.Text <> prevText Then
        UndoStack.Add Array(prevText, prevStart, prevLen)
        If UndoStack.Count > 100 Then UndoStack.Remove 1
        Set RedoStack = New Collection
        prevText = Text1.Text: prevStart = Text1.SelStart: prevLen = Text1.SelLength
    End If
End Sub

Notes: bind Ctrl+C/X/V/Z/Y in KeyDown to call these helpers, test right-click paste and programmatic edits, and tune stack size. The SendMessage undo is quick but not always granular; use the stack approach for full undo/redo behavior.

Recommended Answers

All 2 Replies

Unfortunately we can not. See our RULES here. Show us what you have so far and we will gladly help.

google clipboard object for cut copy and paste.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.