我怎样才能禁止vb.Net中文本框的复制/粘贴选项?

时间:2015-06-09 09:58:58

标签: wpf vb.net visual-studio-2013

我有一个由keyDown事件管理的文本框,它限制插入4个字符,但是当用户复制/粘贴它时,DePace 4字符!所以可以禁止这个选项。

提前谢谢

1 个答案:

答案 0 :(得分:2)

<TextBox CommandManager.PreviewExecuted="textBox_PreviewExecuted"/>

<强> C#

private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Command == ApplicationCommands.Copy ||
        e.Command == ApplicationCommands.Cut  ||
        e.Command == ApplicationCommands.Paste)
    {
        e.Handled = true;
    }
}

<强> VB.NET

Private Sub textBox_PreviewExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    If e.Command Is ApplicationCommands.Copy 
         OrElse e.Command Is ApplicationCommands.Cut 
         OrElse e.Command Is ApplicationCommands.Paste Then
        e.Handled = True
    End If
End Sub

要使用键禁用(Ctrl + C,Ctrl + V),请使用以下代码:

<强> C#

DataObject.AddPastingHandler(control, this.OnCancelCommand);
DataObject.AddCopyingHandler(control, this.OnCancelCommand);

private void OnCancelCommand(object sender, DataObjectEventArgs e)
{
    e.CancelCommand();
}

<强> VB.NET

DataObject.AddPastingHandler(control, AddressOf Me.OnCancelCommand)
DataObject.AddCopyingHandler(control, AddressOf Me.OnCancelCommand)

private void OnCancelCommand(Object sender, DataObjectEventArgs e)
    e.CancelCommand()