-1

I made an ButtonEditor usercontrol with one TextBox and one Button. How to disable TextBox's paste operation from Right Click Menu "Paste" ? Right now, my method is to create a Class called "CustomMenuTextBox.cs" which Subclassed msg "WM_INITMENU" and "WM_INITMENUPOPUP" to call Win32 API EnableMenuItem. But I don't know how to implement two inherits.

The ButtonEditor.cs look like:

internal partial class ButtonEditor : UserControl
{  
   //add a textbox and a button from toolbox and some codes...
}   

The CustomMenuTextBox.cs look like:

internal partial class CustomMenuTextBox : TextBox
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0093 /*WM_UAHINITMENU*/ || m.Msg == 0x0117 /*WM_INITMENUPOPUP*/ || m.Msg == 0x0116 /*WM_INITMENU*/)
        {
            IntPtr menuHandle = m.Msg == 0x0093 ? Marshal.ReadIntPtr(m.LParam) : m.WParam;

            // MF_BYPOSITION and MF_GRAYED
            mAPI.EnableMenuItem(menuHandle, 4, 0x00000400 | 0x00000001);
        }

        base.WndProc(ref m);
    }
}          

I got an error:

internal partial class ButtonEditor : CustomMenuTextBox, UserControl

I know I may make totally wrong way for disable Paste operation for TextBox right click menu. Please show me the right way.

qtg
  • 125
  • 1
  • 11
  • It is not clear what you're asking. Why are you trying to make your editor _inherit_ the `CustomMenuTextBox` class? The editor _has a_ `TextBox`, so if you want to customize that `TextBox`, it stands to reason the editor should instead _have a_ `CustomMenuTextBox`, rather than for it to _be a_ `CustomMenuTextBox` as you're trying to do here. – Peter Duniho Sep 27 '17 at 00:39
  • OK. How to Subclass TextBox (disable right click menu "Paste" item) inside ButtonEditor? – qtg Sep 27 '17 at 00:44
  • _"How to Subclass TextBox...inside ButtonEditor"_ -- what don't you understand how to do? Your question already shows a class that subclasses `TextBox`. So all that seems to be left is _"...inside ButtonEditor"_, and the answer to that is simply to put an instance of `CustomMenuTextBox` in the `ButtonEditor` instead of a `TextBox` as you appear to have now. The way you put a `TextBox` in your `ButtonEditor`, just do that except use `CustomMenuTextBox` instead. – Peter Duniho Sep 27 '17 at 00:51
  • I seems to understand what you teach me. But I am not so clear. I have many codes for txtEditor inside ButtonEditor. How to modify? For example: private void txtEditor_KeyPress(object sender, KeyPressEventArgs e) { //...} – qtg Sep 27 '17 at 01:02
  • Why do you have to modify any of that? Since your `CustomMenuTextBox` _is a_ `TextBox`, all of the code that worked with a `TextBox` will still work with a `CustomMenuTextBox`. – Peter Duniho Sep 27 '17 at 01:12
  • I understand now. I have changed the ButtonEditor.designer.cs. //private System.Windows.Forms.TextBox txtEditor; private CustomMenuTextBox txtEditor; this.txtEditor = new CustomMenuTextBox();//System.Windows.Forms.TextBox(); – qtg Sep 27 '17 at 01:24

1 Answers1

0

I have tested the code snippets on stackoverflow.com, it does work to disable "Paste" menu item for a textbox. However,I just tested on Windows 10 32 bits. The Subclassed message is WM_UAHINITMENU (0x0093), but I couldn't find any info about Windows message "WM_UAHINITMENU" on MSDN.

Credit to @demidov and @CodeCaster and thanks Peter Duniho for proper implementation.

put some codes inside you control and may modify your xxx.designer.cs:

internal partial class CustomMenuTextBox : TextBox
    {
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0093 /*WM_UAHINITMENU*/ || m.Msg == 0x0117 /*WM_INITMENUPOPUP*/ || m.Msg == 0x0116 /*WM_INITMENU*/)
            {
                IntPtr menuHandle = m.Msg == 0x0093 ? Marshal.ReadIntPtr(m.LParam) : m.WParam;

                // MF_BYPOSITION and MF_GRAYED
                mAPI.EnableMenuItem(menuHandle, 4, 0x00000400 | 0x00000001);
            }

            base.WndProc(ref m);
        }
    } 
qtg
  • 125
  • 1
  • 11