在文字框中不能使用粘貼

來源:互聯網
上載者:User

 

設定剪貼簿的字元無法向TextBox粘貼的方法。

設定「Ctrl + V」和文本菜單無效的方法

設定鍵盤的「Ctrl + V」無效方法,在「Windows Forms FAQ - 27.2 How do I disable pasting into a TextBox (via Ctrl + V and the context menu)?」有詳細的介紹。設定TextBox的文本菜單不表示的方法,這裡有介紹。將下面的方法一起使用作成類,就可以代替TextBox的使用方法了

[C#]

//using System.Windows.Forms; //上面一行寫在代碼的最上面 /// <summary> ///使滑鼠和鍵盤的粘貼無效的TextBox /// </summary> public class MyTextBox : TextBox { const Keys pasteKeys = Keys.V | Keys.Control; public MyTextBox() : base() { //設定文本菜單不表示 this.ContextMenu = new ContextMenu(); } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { //設定Ctrl+V無效 if ((keyData | pasteKeys) == pasteKeys) return true; else return base.ProcessCmdKey(ref msg, keyData); } }

 

無視WM_PASTE訊息的方法

另外,下面介紹的代碼就是設定WM_PASTE訊息無視的方法。

[C#]

//using System.Windows.Forms; //上面一行寫在代碼的最上面 /// <summary> ///使滑鼠和鍵盤的粘貼無效的TextBox /// </summary> public class MyTextBox : TextBox { const int WM_PASTE = 0x302; protected override void WndProc(ref Message m) { if (m.Msg == WM_PASTE) return; base.WndProc(ref m); } } .NET Framework 2.0以後版本,使用ShortcutsEnabled屬性的方法

從.NET Framework 2.0版本開始,TextBox的類裡追回了ShortcutsEnabled屬性。當屬性設定為false時,在TextBox中使用的快速鍵都無法使用。而且,文本菜單也無法表示。所以,字串粘貼也無法使用。

但是,所有的捷徑也都無效,複製等操作也無法使用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.