This is a discussion on disable COPY/Paste function on a textbox within the C# Programming forums, part of the Software Development category; Hi, I want to make disable COPY/Paste function on a textbox. DO u know how to do it if ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi, I want to make disable COPY/Paste function on a textbox. DO u know how to do it if so please let me know asap? And it is required to be done at client side. The text box can be HTML or ASP:TextBox. Please reply asap.
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
| Sponsored Links |
| |||
| You will need two javascript functions for this: function noCopyMouse(e) { var isRight = (e.button) ? (e.button == 2) : (e.which == 3); if(isRight) { alert('You are prompted to type this twice for a reason!'); return false; } return true; } function noCopyKey(e) { var forbiddenKeys = new Array('c','x','v'); var keyCode = (e.keyCode) ? e.keyCode : e.which; var isCtrl; if(window.event) isCtrl = e.ctrlKey else isCtrl = (window.Event) ? ((e.modifiers & Event.CTRL_MASK) == Event.CTRL_MASK) : false; if(isCtrl) { for(i = 0; i < forbiddenKeys.length; i++) { if(forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) { alert('You are prompted to type this twice for a reason!'); return false; } } } return true; } And a wee bit of code-behind to handle the two events for the textbox(es): Textbox1.Attributes.Add("onmousedown", "return noCopyMouse(event);") Textbox1.Attributes.Add("onkeydown", "return noCopyKey(event);")
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Hi, Your code is very helpful, but without alert it's not working. Is there any other simple way to do this.
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
| |||
| Hi, I have just tried it without the alerts, and it still works great for me. If you take out the alerts, make sure you leave "return false;" in there. Hope this helps!
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Hi, I had copied your code and just commented the alert line. function noCopyMouse(e) {var isRight = (e.button) ? (e.button == 2) : (e.which == 3); if(isRight) { //alert('You are prompted to type this twice for a reason!'); return false; } return true; } And in page load event of the page i have registered the function like this: TextBox1.Attributes.Add("onmousedown", "return noCopyMouse(event);"); Now if i uncomment the alert then it's working fine. Would you please juggest any other way arround.
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
| |||
| Hi, Maybe this will work for you.... TextBox1.Attributes.Add("oncontextmenu", "return false;"); Hope this helps!
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Hi, your method is working fine. How can i restrict drag and drop. if i drag some words from one textbox to other. its like copy paste. Can we restrict that too...
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
| |||
| Hi , Try following class PreventClipboardOperationsTextBox : TextBox { // Default dummy context menu private System.Windows.Forms.ContextMenu _contextMenu; // Constants private const Keys CopyKeys = Keys.Control | Keys.C; private const Keys CutKeys = Keys.Control | Keys.X; private const Keys PasteKeys = Keys.Control | Keys.V; public PreventClipboardOperationsTextBox() { // Create a dummy context menu to prevent the // default Copy/Cut/Paste context menu from showing up _contextMenu = new ContextMenu(); this.ContextMenu = _contextMenu; } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { // If any of the Copy/Cut/Paste shortcut keys are pressed, // then just return true to indicate that the command key has already been processed if((keyData == CopyKeys) || (keyData == CutKeys) || (keyData == PasteKeys)) { return true; } else { return base.ProcessCmdKey(ref msg, keyData); } } }; As shown above, we subclass the TextBox by creating our PreventClipboardOperationsTextBox that is derived from the Windows Forms TextBox class. We then associate a dummy context menu with the ContextMenu property. Finally, we override the ProcessCmdKey to pre-process keystrokes to the TextBox and return true for any of the clipboard shortcut keys. This indicates that our subclassed PreventClipboardOperationsTextBox has already handled those keys and that we do not want the base class to provide the standard functionality for it. To use the PreventClipboardOperationsTextBox, simply drag and drop a TextBox to your Windows Forms designer from the VS.NET ToolBox. Rename the TextBox class to PreventClipboardOperationsTextBox for the textbox instance that is added. The TextBox in the designer now has the behavior that PreventClipboardOperationsTextBox dictates.
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Hi, simplest way is... <asp:TextBox ID="TextBox1" runat="server" onpaste="return false" oncut="return false"> </asp:TextBox> hope it helps./.
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Disable COPY/Paste function on a textbox with out using javascript we can disable copy/paste .use like following <textarea oncopy="return false" onpaste="return false" oncut="return false"></textarea> ThankQ KiruthikaSambandam |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Catch Paste Event in Javascript | velhari | HTML, CSS and Javascript Coding Techniques | 0 | 12-04-2007 06:33 AM |
| How to create textbox without border? | ramkumaraol | PHP Programming | 2 | 09-07-2007 06:13 AM |
| string copy (strcpy) and a memory copy (memcpy) | vigneshgets | C and C++ Programming | 1 | 05-30-2007 11:40 AM |
| Diff inline function and ordinary function | vigneshgets | C and C++ Programming | 1 | 05-24-2007 11:34 AM |
| How to create html from the content of the textbox? | Balasubramanian.S | C# Programming | 4 | 03-29-2007 08:30 AM |