0

I am trying to remove the newline and replace with whitespace before pasting to comboBox as it ignores anything beyond a line. I am trying this:

If e.Modifiers = Keys.Control AndAlso e.KeyValue = Keys.V Then Then
            Clipboard.SetText(Regex.Replace(Clipboard.GetText(TextDataFormat.UnicodeText), "\n", " "))
            e.Handled = True
        End If

I am performing this inside KeyDown event but it is able to capture either Ctrl or V but not both. I tried Capture CTRL+V or paste in a textbox in .NET and http://social.msdn.microsoft.com/Forums/windows/en-US/096540f4-4ad4-4d24-ae12-cfb3e1b246f3/interceptingoverriding-paste-behavior-on-combobox but no results as desired. May be there is something i am missing in my code. Please help me out.

I am getting the needed value with this Clipboard.GetText().Replace(vbCrLf, " ") when i debug but i am not able to set it. I tried using a variable to set it but even then no change. I also tried clearing the clipboard and then resetting with this variable holding the modified value.

I am using Winforms and i tried this but still no change to my clipboard:

Private Const WM_PASTE As Integer = &H302
    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_PASTE Then
            Dim returnText As String = Nothing
            If (Clipboard.ContainsText()) Then
                returnText = Clipboard.GetText().Replace(vbCrLf, " ")
                Clipboard.Clear()
                Clipboard.SetText(returnText)
            End If
        End If
        MyBase.WndProc(m)
    End Sub
Community
  • 1
  • 1
Milee
  • 1,191
  • 1
  • 11
  • 29

3 Answers3

0

Handling keyboard only events for intercepting pasting doesn't solve the problem, because pasting can also be done using mouse or touch interface.

Thus, if you are using WPF, then simply add the DataObject.Pasting event handler to your ComboBox, so definition of the control in XAML will look like:

    <ComboBox Name="comboBox1" IsEditable="true" DataObject.Pasting="comboBox1_Pasting" ... />

And, finally, in your code handle it as (I'm adding a method here to the code-behind, which is not as nice, as using commands):

    private void comboBox1_Pasting(object sender, DataObjectPastingEventArgs e)
    {
        // modify the clipboard content here
    }

If you're using WinForms, then look here: hook on default “Paste” event of WinForms TextBox control

Community
  • 1
  • 1
ashepetko
  • 1
  • 1
0

This piece of code worked for me:

Private Const WM_PASTE As Integer = &H302
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
        If keyData = (Keys.Control Or Keys.V) Or msg.Msg = WM_PASTE Then
            If (Clipboard.ContainsText()) Then
                Clipboard.SetText(Clipboard.GetText().Replace(vbCrLf, " "))
            End If
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
Milee
  • 1,191
  • 1
  • 11
  • 29
-1

Use the keydown event and alter the clipboard like this

Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    If e.KeyCode = Keys.V AndAlso (e.Modifiers And Keys.Control) <> 0 Then
        My.Computer.Clipboard.SetText(My.Computer.Clipboard.GetText().Replace(vbCrLf, " "))
    End If
End Sub

But this example is going to alter the clipboard contents. Modify it to paste or insert yourself as you wish

Srinidhi
  • 469
  • 6
  • 8