.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Restrict Clipboard Shortcuts in TextBox in Silverlight

Posted By:Diptimaya Patra       Posted Date: May 12, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

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

Introduction

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.
public class CustomTextBox: TextBox
{

}

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

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

Add the following override methods.

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.Key== Key.Ctrl)
    {
        IsControlKeyDown = true;
    }

    if (IsControlKeyDown && (e.Key==Key.C || e.Key==Key.X || e.Key ==Key.V))
    {
        e.Handled = true;
    }
    else
    {
        base.OnKeyDown(e);
    }
}

protected override void OnKeyUp(KeyEventArgs e)
{
    if (e.Key == Key.Ctrl)
    {
        IsControlKeyDown = false;
    }
    base.OnKeyUp(e);
}

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


RestrictFig7.gif

After adding the Namespace Change the TextBox Control that you need to restrict the Clipboard shortcuts. In our case we will do it for the About User TextBox.

RestrictFig8.gif
Initialize the IsControlKeyDown property to False.

RestrictFig9.gif
Now we are ready to test our application.


RestrictFig10.gif

Try pressing Ctrl+C/Ctrl+X/Ctrl+V the clipboard shortcuts are handled.

Hope this article helps.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend