Copy, Cut, Paste - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Copy, Cut, Paste

ScottkScottk Posts: 4Member
Hello! I am trying to write the Copy, Cut, Paste functions in my application (like the Generic Windows Copy, Cut, Paste). It was quite simple with VB6 but I am having more difficulty with VB.Net. Does anyone have the code snipit(s) that does this same thing in .net?

Thanks in advance.

Comments

  • BattleGuardBattleGuard Posts: 166Member
    Hi,
    Are you doing this by "ctrl+c" function or a drop down meny, for a right click???

    I may have some questions

    BattleGuard

    : Hello! I am trying to write the Copy, Cut, Paste functions in my application (like the Generic Windows Copy, Cut, Paste). It was quite simple with VB6 but I am having more difficulty with VB.Net. Does anyone have the code snipit(s) that does this same thing in .net?
    :
    : Thanks in advance.
    :

  • ScottkScottk Posts: 4Member
    I am trying to do this with a drop down menu. The problem I am having is that I want to be able to select any text in any text box and have the cut and paste menu feature work, without having to write code for each text box, if that is possible.

    Scott

    : Hi,
    : Are you doing this by "ctrl+c" function or a drop down meny, for a right click???
    :
    : I may have some questions
    :
    : BattleGuard
    :
    : : Hello! I am trying to write the Copy, Cut, Paste functions in my application (like the Generic Windows Copy, Cut, Paste). It was quite simple with VB6 but I am having more difficulty with VB.Net. Does anyone have the code snipit(s) that does this same thing in .net?
    : :
    : : Thanks in advance.
    : :
    :
    :

  • Chris__BChris__B Posts: 51Member
    Hi Scott,

    Perhaps you could try this code for the three menu items Cut, Copy and Paste. Using the Clipboard and determining which is the active control on the form you should be able to tweak this code to meet your needs.

    [code]
    Private Sub mnuEditCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCut.Click
    'Remove Text from text box and place it in Clipboard
    Clipboard.SetDataObject(CType(ActiveControl, TextBox).SelectedText)
    ActiveControl.Text = String.Empty
    End Sub

    Private Sub mnuEditCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCopy.Click
    'Copy Text from text box and place it in clipboard
    Clipboard.SetDataObject(CType(ActiveControl, TextBox).SelectedText)
    End Sub

    Private Sub mnuEditPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditPaste.Click
    'Retrieve data from clipboard and place it in text box
    Dim oDataObject As IDataObject
    oDataObject = Clipboard.GetDataObject()
    If oDataObject.GetDataPresent(DataFormats.Text) Then
    CType(ActiveControl, TextBox).SelectedText = CType(oDataObject.GetData(DataFormats.Text), String)
    End If
    End Sub

    [/code]


    Hope this helps.
    Cheers,

    Chris


    : Hello! I am trying to write the Copy, Cut, Paste functions in my application (like the Generic Windows Copy, Cut, Paste). It was quite simple with VB6 but I am having more difficulty with VB.Net. Does anyone have the code snipit(s) that does this same thing in .net?
    :
    : Thanks in advance.
    :

  • ScottkScottk Posts: 4Member
    Chris,

    That was what I was looking for. Thank you!!!

    Scott

    : Hi Scott,
    :
    : Perhaps you could try this code for the three menu items Cut, Copy and Paste. Using the Clipboard and determining which is the active control on the form you should be able to tweak this code to meet your needs.
    :
    : [code]
    : Private Sub mnuEditCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCut.Click
    : 'Remove Text from text box and place it in Clipboard
    : Clipboard.SetDataObject(CType(ActiveControl, TextBox).SelectedText)
    : ActiveControl.Text = String.Empty
    : End Sub
    :
    : Private Sub mnuEditCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCopy.Click
    : 'Copy Text from text box and place it in clipboard
    : Clipboard.SetDataObject(CType(ActiveControl, TextBox).SelectedText)
    : End Sub
    :
    : Private Sub mnuEditPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditPaste.Click
    : 'Retrieve data from clipboard and place it in text box
    : Dim oDataObject As IDataObject
    : oDataObject = Clipboard.GetDataObject()
    : If oDataObject.GetDataPresent(DataFormats.Text) Then
    : CType(ActiveControl, TextBox).SelectedText = CType(oDataObject.GetData(DataFormats.Text), String)
    : End If
    : End Sub
    :
    : [/code]
    :
    :
    : Hope this helps.
    : Cheers,
    :
    : Chris
    :
    :
    : : Hello! I am trying to write the Copy, Cut, Paste functions in my application (like the Generic Windows Copy, Cut, Paste). It was quite simple with VB6 but I am having more difficulty with VB.Net. Does anyone have the code snipit(s) that does this same thing in .net?
    : :
    : : Thanks in advance.
    : :
    :
    :

  • ergumoergumo Posts: 1Member
    Hey !
    Your code works great !

    Is there a way to modify it so that it only clears the selected text in the Cut Function?

    I mean when a user selects only part of the text in a textbox... in the current function we obtain the selected portion and send it to the clipboard but then we clear the whole text...


    Thanks!
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    This post is from 6 years ago... I doubt they will give you a response... or even remember... But here is something I whipped up for you:

    [code]
    'Try this

    Private Sub mnuEditCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCut.Click
    'Remove Text from text box and place it in Clipboard
    Clipboard.SetDataObject(CType(ActiveControl, TextBox).SelectedText)

    Dim S As String = CType(ActiveControl, TextBox).SelectedText
    Dim SelStart As Integer = CType(ActiveControl, TextBox).SelectionStart
    Dim SelLen As Integer = CType(ActiveControl, TextBox).SelectionLength
    S = S.Remove(SelStart, SelLen)

    ActiveControl.Text = S
    End Sub
    [/code]
Sign In or Register to comment.