Disable copy-paste in textbox

  • 14 years ago
     

    Hi,

    How do I disable that the user can't paste data into a textbox, and how do I disable the contextmenustrip of the textbox the one that always appears with the copy-paste options two.

  • 14 years ago

    Dear Lexian

     

    Click here for disabling copy and paste in a textbox using ctl+c and ctl+v

    About stopping the contextmenustrip just ignore the right click button of the mouse above the textbox

     

    Regards
    DoctorMahdi

  • 14 years ago

    Hi doctormahdi,

    That code is in vb6 I use vb2005, how do I ignore the right click?

    Thanks.

  • 14 years ago

    Lexian,

    I tried suppressing the key event if a user press ctrl-c or ctrl-v and it worked nicely. That will take care of people pasting in that manner. Just incorporate this code into your keydown event

    'Suppress ctrl-c and ctrl-v
    If e.Control AndAlso (e.KeyCode = Keys.V OrElse e.KeyCode = Keys.C) Then e.SuppressKeyPress = True
    I was able to suppress the context menu using this code.
       Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
          'Suppress the message to display the context menu
          If m.Msg <> 517 Then MyBase.WndProc(m)
       End Sub

  • 14 years ago

    I just figured out another way to suppress the normal context menu which allows copying and pasting.  Just add your own contextmenu but don't add any options to it.  You could do this in your custom control in the sub new procedure like so.  I think this method is better because this way you could later add a context menu if you desired.  With the method in my previouse no context menu would ever be displayed even if you wanted one.

       Public Sub New()
          Dim cm As New ContextMenu
          Me.ContextMenu = cm
       End Sub

  • 14 years ago

    Hi,

    TwoFaced, look what I found if the property ShortcutEnable is set to False it doesn't let the user paste data into the control and dont show the context menu strip. Everithing was in a property. Arghhhhhhhhh............

  • 14 years ago

    Nice find lexian.  I guess we were just over thinking it :)  Glad to here you've got everything working though.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold