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
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.
:
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.
: :
:
:
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.
:
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.
: :
:
:
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!
[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]