Restrict Clipboard Shortcuts in TextBox in Silverlight

0 comments

In this article we will see how we can restrict Clipboard shortcuts such as CTRL+C, CTRL+X, and CTRL+V in Silverlight 3 for TextBox Control.

Creating Silverlight Project
Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as RestrictCBShortcutSL3.


RestrictFig1.gif

Let's have one or more TextBoxes that will fulfill our sample application.

RestrictFig2.gif

Now for disabling Clipboard shortcuts such as Ctrl+C, Ctrl+X, and Ctrl+V we need to have Custom TextBox control.

So Create a class as CustomTextBox.cs .


RestrictFig3.gif

Now inherit TextBox class to it.
1public class CustomTextBox: TextBox
2{
3 
4}

Now we will have a boolean property that will decide whether CTRL key is pressed.
1public bool IsControlKeyDown { getset; }

To override default shortcuts we need to override the base methods such as OnKeyDown and OnkeyUp.

Add the following override methods.

01protected override void OnKeyDown(KeyEventArgs e)
02{
03    if (e.Key== Key.Ctrl)
04    {
05        IsControlKeyDown = true;
06    }
07 
08    if (IsControlKeyDown && (e.Key==Key.C || e.Key==Key.X || e.Key ==Key.V))
09    {
10        e.Handled = true;
11    }
12    else
13    {
14        base.OnKeyDown(e);
15    }
16}
17 
18protected override void OnKeyUp(KeyEventArgs e)
19{
20    if (e.Key == Key.Ctrl)
21    {
22        IsControlKeyDown = false;
23    }
24    base.OnKeyUp(e);
25}

Now go back to the XAML code behind and add the Namespace (Your application's namespace) as follows:



READ MORE >>
http://www.dotnetspark.com/kb/1961-restrict-clipboard-shortcuts-textbox-silverlight.aspx

0 comments: