[2005] Paste from clipboard.
I am trying to paste negative values copied from Excel formatted with the (123) into a DataGrid. When I copy it with the column formatted to a decimal it rejects the value because it reads it as a string "(123)" instead of "-123". Is it possible to catch the text from the clipboard and replace the ( with a - and then get rid of the second ) ..
Re: [2005] Paste from clipboard.
I can do it using a button just calling this sub routine. But i need to capture the paste event, anyone know how to pull that off.
vb.net Code:
Private Sub PasteRoutine()
Dim objClipboard As IDataObject = Clipboard.GetDataObject()
Dim myString As String = objClipboard.GetData(DataFormats.Text).ToString
Dim first As Integer = myString.IndexOf("(")
Dim second As Integer = myString.IndexOf(")")
If first > -1 And second > -1 Then
If IsNumeric(myString.Substring(first, (myString.Length - second) + second)) Then
myString = myString.Replace("(", "-")
myString = myString.Replace(")", "")
formulaTB.Text = myString
End If
End If
End Sub
Re: [2005] Paste from clipboard.
There is no event specifically associated with data being pasted. I'm guessing that there's a Windows message associated with a paste action so you could inherit the control, override the WndProc method and test to find what message(s) that might be. Once you know that you can trap that message and take the appropriate action.
Re: [2005] Paste from clipboard.
Re: [2005] Paste from clipboard.
I just did a quick test with a TextBox and the TextChanged event was raised before the WM_PASTE message was received, so that would mean that the value would already have been rejected before you became aware that a paste operation was taking place.
What you could do is instead of using Ctrl+V, select a different key combination (e.g. Ctrl+Alt+V) and trap it in the KeyDown event of the target control. You can then call your PasteRoutine method to process the value that is currently on the clipboard.
Re: [2005] Paste from clipboard.
Alright, I changed the code a little but, this could work perfectly if there was a way to check if the clipboard data has changed. Any event handler to do that ?
Re: [2005] Paste from clipboard.
Hello Guys... (In the same vein)
I am doing a Clipboard.SetDataObject(WorkSheetObj.Range("A6:A9").Copy().ToString)
and am going to OUTLOOK
and do a control V
What's being pasted is "TRUE"
not the contents of those cells in Excel..
Any ideas how to do this right?
Thanks in advance
Re: [2005] Paste from clipboard.
Looks like you are not using the proper type of the expected data.
Clipboard.SetText(WorkSheetObj.Range("A6:A9"))
Or such