Manu
Manu

Reputation: 33

How to disable copy paste into password box in Silverlight

Is there a way to prevent the user from pasting the data into the password box. The requirement is that the user should not copy password from the password field to the confirm password field. Key down events doesnt seem to be helping me as it fires only when pressing the ctrl key and does not fire on ctrl + V.

Upvotes: 3

Views: 1957

Answers (1)

Matthew Keelan
Matthew Keelan

Reputation: 536

One solution may be to extend the TextBox control to imitate the PasswordBox and override the KeyUp/KeyDown events to prevent copy/paste. It appears that someone has already written this in order to support East Asian characters:

Allowing input of east asian characters to PasswordBox

You should be able to modify the OnKeyDown handler to disable paste as well:

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

    if (CtrlKeyDown && (e.Key == Key.C || e.Key == Key.X || e.Key == Key.Z || e.Key == Key.Y || e.Key == Key.V))
        e.Handled = true;
    else
        base.OnKeyDown(e);
}

Upvotes: 1

Related Questions