none
Intercepting/Overriding Paste behavior on ComboBox

    Question

  • In C#.

    I need to intercept and override the default behavior when pasting text into a system.Windows.Forms.ComboBox control. I need to translate the text before it gets put into the combobox (replacing some characters with others, for example).

    I am unable to find any relevant Paste events or overridable methods.

    Anyone have any cool tricks/ sugestions on how to do this.  

    I tried grabbing the WM_PASTE (0x0302)  message in a WndProc override, but iI never see the message. Evidently pressing Ctrl+V in a ComboBox does not generate this message (or it's getting consumed before the WndProc on the custom ComboBox ever sees it.'

    Thanks
    Monday, June 11, 2007 10:38 PM

Answers

  • You have get hold of the textbox embedded in the combo for this.

    Try this out-

    public partial class Form1 : Form

    {

    [DllImport("user32.dll", SetLastError = true)]

    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    public Form1()

    {

    InitializeComponent();

    IntPtr lhWnd = FindWindowEx(comboBox1.Handle, IntPtr.Zero, "EDIT", null);

    DisallowPaste p = new DisallowPaste();

    p.AssignHandle(lhWnd);

    ....

    }

     

    public class DisallowPaste : NativeWindow

    {

    protected override void WndProc(ref Message m)

    {

    if (m.Msg == (0x0302))

    {

    return;

    }

    base.WndProc(ref m);

    }

    }

     

    You can break on the return statement in the WndProc and see if code comes in there.

    Cheers!

    Tuesday, June 12, 2007 5:01 AM

All replies

  • Do you have a menu bar? If so, it might be intercepting the ctrl-v. If you have used the feature in VS that adds all the default menus for you, it adds an Edit menu that contains all the copy, paste type of items. It assigns ctrl-v as a shortcut to the paste menu item.
    Tuesday, June 12, 2007 4:18 AM
  • You have get hold of the textbox embedded in the combo for this.

    Try this out-

    public partial class Form1 : Form

    {

    [DllImport("user32.dll", SetLastError = true)]

    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    public Form1()

    {

    InitializeComponent();

    IntPtr lhWnd = FindWindowEx(comboBox1.Handle, IntPtr.Zero, "EDIT", null);

    DisallowPaste p = new DisallowPaste();

    p.AssignHandle(lhWnd);

    ....

    }

     

    public class DisallowPaste : NativeWindow

    {

    protected override void WndProc(ref Message m)

    {

    if (m.Msg == (0x0302))

    {

    return;

    }

    base.WndProc(ref m);

    }

    }

     

    You can break on the return statement in the WndProc and see if code comes in there.

    Cheers!

    Tuesday, June 12, 2007 5:01 AM
  • Thanks. This was basically what I needed. 
    Wednesday, June 13, 2007 8:21 PM
  • Hi,
    thanks for sharing this code!! I've spend the last two days to get a paste-event in a combobox.
    You saved my day! :-)

    Greets,
    Scotty
    Wednesday, July 16, 2008 9:06 AM